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 👉 Ultimate top of the best proteins: egg / beef / veal / poultry / lamb / offal / fish / shellfish / mature cheese / legumes
https://buff.ly/3Si8kfu

New shorts on Blooness 👉 Adding collagen to protein meals or eating cartilage with meat is one of the best ways to maintain muscle mass in terms of longevity.
https://buff.ly/4f8kMbs

New shorts on Blooness 👉 Secret of longevity: find meat from animals that are not mainly fed soybean meal, sunflower, corn, and other cereals.
https://buff.ly/3YpbrX5

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