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

Nouveau short sur Blooness 👉 Relancer la libido, en vrac -> Soleil, vitamine D, diète céto-méditerranéenne, protéines, livres, badinage, marche, HIIT, et pas de régime hypocalorique.
https://buff.ly/445PWvc

Nouveau short sur Blooness 👉 Améliorer son sommeil -> s’exposer à la lumière le matin, limiter les lunettes de soleil, mouiller la chemise au sport, boire du déca, manger maximum 3h avant de dormir, faire une marche digestive et prendre de la mélaton
https://buff.ly/440weAJ

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