Tulip Media

Entrepreneurial thoughts

How to integrate a Google Analytics tag by domain with WPML

Hello everyone!

I had a bit of trouble inserting the famous Google Analytics tracking TAG on multilingual sites that use the WPML extension, so I would like to share with those who use WordPress this little hack which will allow you to quickly have the solution.

Indeed, for integrate a Google Analytics tag by domain name using WPML, you must insert a function in the functions.php file, which will display the correct tag depending on the language displayed. Here is the famous piece of code:

[code]/*———————————————————————————–*/ /* Analytis tag per language /*——————— —————————————————————–*/

function ga_analytics_ln() { if(ICL_LANGUAGE_CODE=='en') { ?>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-XXX');

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-XXX');
<?php } } add_action(‘wp_head’,’ga_analytics_ln’,11);[/code]

You simply replace the “XXX” with your Google Analytics ID.

On the other hand, if you use a tool like My Custom Functions for WordPress, the latter will surely give you a “fatal error”, as was the case for me, because it will consider that there are two functions with the same name, which it will refuse to apply.

It will therefore be necessary to slightly modify the Google Analytics tag of the second language, to simply give it another name:

[code]/*———————————————————————————–*/ /* Analytis tag per language /*——————— ————————————————————–*/ function ga_analytics_ln() { if(ICL_LANGUAGE_CODE=='en') { ?>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-XXX');

window.dataLayer = window.dataLayer || [];
function gtagfr(){dataLayer.push(arguments);}
gtagfr('js', new Date());

gtagfr('config', 'G-XXX');
<?php } } add_action(‘wp_head’,’ga_analytics_ln’,11);[/code]

In the example above, I simply renamed the gtag function to gtagfr, so that each Google Analytics tag has a unique function in the eyes of the plugin My Custom Functions. And the advantage of using such a plugin is that no matter the updates or theme changes, the code will remain active on your site and will continue to work.

Once this is done, remember to exclude your IP from Google Analytics by following this tutorial reserved for Google Analytics 4 🙂

Also learn how to configure a domain name per language with the WPML extension here!

 

My latest tweets

New shorts on Blooness 👉 Vitality, longevity, weight loss: the ultimate guide to proteins in the keto-Mediterranean diet is online on Blooness. https://buff.ly/3xxNvpj
https://buff.ly/4cG2GM4

New shorts on Blooness 👉 The new premium content comes out in 48 hours, and what content! This will be the most comprehensive guide on the amount of protein to consume on a keto-Mediterranean diet. See you soon !
https://buff.ly/3L2KbFP

New Post: Combo, the best solution to put an end to the headache of scheduling and HR management https://buff.ly/4eE6JtT

Load more

Don't miss business advice by email

No spam, just an email when new content is published.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEN