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:

Don't miss business advice by email

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

[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!

Leave a Reply

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

 

What are the best (and worst) meats for healthy longevity? This is the subject of the new chapter devoted to the natural diet of ruminants, and the modifications made by humans, for better and for worse 👉 https://blooness.com/alimentation-ruminants/

Load more

Don't miss business advice by email

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

en_USEN