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 short on Blooness 👉 Exposure to blue light from screens at night could have negative effects on reproductive function, by disrupting the circadian rhythm.
https://buff.ly/4ePGVKD

New short on Blooness 👉 One of the flagship posts of the Blooness guide is available in audio format. How to achieve full vitality, lose weight or gain muscle thanks to proteins in a Mediterranean diet and moderate in carbohydrates https://b
https://buff.ly/4dvcV5C

New short on Blooness 👉 Sugars to avoid on labels: glucose syrup, raffinose, maltose, maltodextrin, sucrose, wheat dextrose, glucose syrup, processed starch, corn syrup…
https://buff.ly/47OQXtd

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