This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 11 replies, has 2 voices.

Last updated by Marcel 4 months, 1 week ago.

Assigned support staff: Marcel.

Author Posts
June 5, 2019 at 8:02 am #3957835

helleO

Hi,

I have previously written about this, but the topic seems to have been closed without the issue has been resolved.

So now I try again.

I have added Google Analytics tracking for multi language sites following this thread: https://wpml.org/forums/topic/tracking-google-analytics-for-multi-language-sites/, but it doesn't guite work. What have I done wrong?
It's on this site: hidden link

Best regards,
Helle Østergaard

June 5, 2019 at 12:24 pm #3960365

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+02:00)

Hi Helle,

Thank you for contacting WPML Support.

I don't see any old tickets in your account? Did you maybe create it with another (end) user account?

Could you please post me the code from your site you already got, which your language codes? Please also update WPML to the newest version.

Best Marcel

June 5, 2019 at 5:41 pm #3963133

helleO

Hi Marcel,

Here's the code I added to the footer script:

<!-- ANALYTICS TRACKING CODE -->
<?php
// DA language
if ( defined( 'ICL_LANGUAGE_CODE' ) && 'da' == ICL_LANGUAGE_CODE ) {
//Just for debug, you can remove this input later
echo('<input type="hidden" value="language active: DA">'); ?>

<!-- Content for DA language here -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="hidden link"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-109101998-1');
</script>

<?php }
// DE language
else if ( defined( 'ICL_LANGUAGE_CODE' ) && 'de' == ICL_LANGUAGE_CODE ) {
//Just for debug, you can remove this input later
echo('<input type="hidden" value="language active: DE">'); ?>

<!-- Content for DE language here -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="hidden link"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-109101998-3');
</script>

<?php }
// EN language
else if ( defined( 'ICL_LANGUAGE_CODE' ) && 'en' == ICL_LANGUAGE_CODE ) {
//Just for debug, you can remove this input later
echo('<input type="hidden" value="language active: EN">'); ?>

<!-- Content for EN language here -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="hidden link"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-109101998-4');
</script>

<?php }
// NO language
else if ( defined( 'ICL_LANGUAGE_CODE' ) && 'no' == ICL_LANGUAGE_CODE ) {
//Just for debug, you can remove this input later
echo('<input type="hidden" value="language active: NO">'); ?>

<!-- Content for NO language here -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="hidden link"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-109101998-2');
</script>

<?php }
// Just for debug, you can remove this
else {
echo('Something is wrong');
}
?>
<!--/ ANALYTICS TRACKING CODE -->

Best regards,
Helle

June 6, 2019 at 8:50 am #3967197

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+02:00)

Hi Helle,

The code mixes Google Analytics and Google Tag Manager. These are two different systems. You can also integrate Google (Universal) Analytics with the Tag Manager.

- Google Analytics uses the ID's with "UA-"
- A Google Tag Manager ID starts with "GTM-" and requires additional code in the <body> element.

The code would work if it only refers to 1 system. For each language code, please copy the correct snippet from the Google interface into your code.
https://wpml.org/forums/topic/how-to-add-the-google-analytics-for-2-languages-domains/#post-891674

Best Marcel

June 6, 2019 at 11:20 am #3968583

helleO

Hi Marcel,

The code I'm using is the one Google Analytics provide me. I don't know which else code to use.

Right now the code are working to track the traffic but not separately for each language.

What can I do?

Best regards,
Helle

June 6, 2019 at 2:09 pm #3969797

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+02:00)

OK thanks, In the previous link with the example it was running in the <head> of the site. Your code is running in the Footer. Did you try that too?

June 6, 2019 at 5:16 pm #3971177

helleO

Tes, I have also tried running the code in the head. That didn't work either.

June 11, 2019 at 8:24 am #3993269

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+02:00)

Could you please try it again?

<!-- ANALYTICS TRACKING CODE -->
<?php
   // DA language
    if ( defined( 'ICL_LANGUAGE_CODE' ) && 'da' == ICL_LANGUAGE_CODE ) {
        //Just for debug, you can remove this input later
        echo('<input type="hidden" value="language active: DA">'); ?>
         
		 <!-- Content for DA language here -->
		<script async src="<em><u>hidden link</u></em>"></script>
		<script>
		window.dataLayer = window.dataLayer || [];
		function gtag(){dataLayer.push(arguments);}
		gtag('js', new Date());

		gtag('config', 'UA-109101998-1');
		</script>
          
    <?php }
    // DE language
    else if ( defined( 'ICL_LANGUAGE_CODE' ) && 'de' == ICL_LANGUAGE_CODE ) {
        //Just for debug, you can remove this input later
        echo('<input type="hidden" value="language active: DE">'); ?>
         
        <!-- Content for DE language here -->
		<!-- Global site tag (gtag.js) - Google Analytics -->
		<script async src="<em><u>hidden link</u></em>"></script>
		<script>
		window.dataLayer = window.dataLayer || [];
		function gtag(){dataLayer.push(arguments);}
		gtag('js', new Date());

		gtag('config', 'UA-109101998-3');
		</script>
			
         
    <?php }
    // EN language
    else if ( defined( 'ICL_LANGUAGE_CODE' ) && 'en' == ICL_LANGUAGE_CODE ) {
        //Just for debug, you can remove this input later
        echo('<input type="hidden" value="language active: EN">'); ?>
         
       <!-- Content for EN language here -->
		<!-- Global site tag (gtag.js) - Google Analytics -->
		<script async src="<em><u>hidden link</u></em>"></script>
		<script>
		window.dataLayer = window.dataLayer || [];
		function gtag(){dataLayer.push(arguments);}
		gtag('js', new Date());

		gtag('config', 'UA-109101998-4');
		</script>
			
         
    <?php }
    // NO language
    else if ( defined( 'ICL_LANGUAGE_CODE' ) && 'no' == ICL_LANGUAGE_CODE ) {
        //Just for debug, you can remove this input later
        echo('<input type="hidden" value="language active: NO">'); ?>
         
        <!-- Content for NO language here -->
		<!-- Global site tag (gtag.js) - Google Analytics -->
		<script async src="<em><u>hidden link</u></em>"></script>
		<script>
		window.dataLayer = window.dataLayer || [];
		function gtag(){dataLayer.push(arguments);}
		gtag('js', new Date());

		gtag('config', 'UA-109101998-2');
		</script>
         
    <?php }
    // Just for debug, you can remove this
    else {
       echo('Something is wrong');
    }
?>
<!--/ ANALYTICS TRACKING CODE -->
June 11, 2019 at 10:23 am #3994149

helleO

Now I have tried again to run the code in the head. It still doesn't work.

June 11, 2019 at 1:39 pm #3995785

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+02:00)

We can try the following:

#1 Please also update all WPML components to the newest version.

#2 Google recommends putting Analytics in the header. As far I can see from a Flatsome support comment, they advise their clients to add the standard Analytics code to Theme Options > HTML blocks, which will be triggered in wp_footer(). Could you please try again to add the new code to the footer?

#3 Can you try it with another Theme for test purpose (like Twenty Nineteen).

Please let me know

Best Marcel

June 11, 2019 at 7:14 pm #3998209

helleO

#1 All WPML components was updated to the newest version when you earlier advised me to update.

#2 I have now again placed the code in Flatsome > Advanced > Global settings > Footer script. There's no HTML blocks in Theme options.

#3 I can't just change the theme so that's not an option.

It's still the same. It doesn't work.

June 12, 2019 at 7:52 am #4001281

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+02:00)

Hi Helle,

Oh no, you understand me wrong. I was speaking about to add this to the footer of your Theme over PHP, as I don't know how Flatsome Theme will interpret the code inside their input fields.

As you see in the attached screenshot parts of the code are not active ( else if and echo ), because it's called from the Theme's input fields and this PHP is ignored. If you add a standard Analytics code over Javascript there, you will not have this problem.

I used this as a recommendation to know if Flatsome prefers the header of footer, which means header.php or footer.php in the Theme.

This code has to be added in your Theme after wp_footer() over PHP. If you don't know where to find this, I would recommend you to contact a certified WPML contractor to implement this code for you, as this is a custom code solution this is out of the scope of our support policy.

Best Marcel