Skip Navigation

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.

Tagged: 

This topic contains 16 replies, has 2 voices.

Last updated by shulaR 1 year, 9 months ago.

Assisted by: Nicolas V..

Author Posts
July 14, 2023 at 4:11 pm #14040771

shulaR

I'm trying to add Google anlytics 4 to individual langiuages and domain names from my WPML set up

July 14, 2023 at 4:17 pm #14040797

shulaR

Hi there, I spoke with Ilyes the other day about this.

Ilyes gave me this php to add to th html header;

<?php
// Determine the language (e.g., based on a language parameter in the URL)
$language = $_GET['lang'];

// Output the corresponding Google Analytics tracking code based on the language
if ($language == 'en') {
// English tracking code
echo "<script>Google Analytics tracking code for English</script>";
} elseif ($language == 'fr') {
// French tracking code
echo "<script>Google Analytics tracking code for French</script>";
} elseif ($language == 'es') {
// Spanish tracking code
echo "<script>Google Analytics tracking code for Spanish</script>";
}
?>

So for instance I have this google analytics infor for .ie;

Ireland Tag G-LW6VP1VQX6

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

gtag('config', 'G-LW6VP1VQX6');
</script>

Where it says;

echo "<script>Google Analytics tracking code for French</script>";

in the php, should this have the goog tag only i.e 'G-LW6VP1VQX6'

or should it have the whole script;

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

gtag('config', 'G-LW6VP1VQX6');
</script>

If neither what part of the information that I have from GA4 should I be adding here?

Cheers

JOhn

July 14, 2023 at 5:25 pm #14041067

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hello,

You should add the entire script without echo for each language using an IF ELSE statement. Here is an example: https://wpml.org/forums/topic/google-analytics-tag-im-multi-languages/#post-5557661

Also, instead of modifying the header you can add a function into functions.php that will hook to 'wp_head'
Quick example:

add_action( 'wp_head', function(){
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
if( $my_current_lang == 'fr' ){?>
<!-- Google tag (gtag.js) -->
<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', 'FRENCH-SCRIPT-ID');
</script>
<?php } elseif( $my_current_lang == 'en' ){?>
<!-- Google tag (gtag.js) -->
<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', 'ENGLISH-SCRIPT-ID');
</script>
<?php } 
});

Does it make sense?

July 17, 2023 at 9:43 am #14047895

shulaR

Hi Nicolas, Thank you fro getting back to me.

So for instance I'm using English and Italian.

From Google Analytocs 4 I have;

UK Tag G-6VNPELT58E

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

gtag('config', 'G-6VNPELT58E');
</script>

and

Italy Tag G-VVWGPYL9C6

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

gtag('config', 'G-VVWGPYL9C6');
</script>

Would the below be the correct way of implementing these within the PHP?;

add_action( 'wp_head', function(){
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
if( $my_current_lang == 'en’ ){?>
<!-- Google tag (gtag.js) -->
<script async src="<u>hidden link</u>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
 
gtag('config', '<!-- Google tag (gtag.js) -->
<script async src="hidden link"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-6VNPELT58E');
</script>');
</script>
<?php } elseif( $my_current_lang == 'it’ ){?>
<!-- Google tag (gtag.js) -->
<script async src="<u>hidden link</u>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
 
gtag('config', '<!-- Google tag (gtag.js) -->
<script async src="hidden link"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-VVWGPYL9C6');
</script>');
</script>
<?php }
});

Cheers

John

July 17, 2023 at 5:15 pm #14052043

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hi John,

Please read my private answer above and let me know if it solves your issue.

Nico

July 17, 2023 at 6:51 pm #14053067

shulaR

Hi Nico,

Thank you very much for your help. I appreciate it as PHP isn't my thing!

I hav addd the PHP using the Code Snippts plugin https://wordpress.org/plugins/code-snippets/. Do you thnk this will be ok or do I need to manually edit the functions.php file?

Cheers

John

July 17, 2023 at 9:25 pm #14053627

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hello,

Sure, you can use a code snippet plugin but you have to take into consideration few things:
1. opening and closing PHP tags.
In my example, because it's meant to be inserted in to functions.php, there is no opening <?php (because it's already open) and at the end I let it opened.
2. Make sure that your snippet is triggered on wp_head and remove it from my code.

If you're not comfortable, I would recommend to simply copy/paste the code in your functions.php

July 18, 2023 at 7:44 am #14055429

shulaR

Hi Nicolas, Thanks for gettin back to me. I've put it in the functions.php for the theme;

hidden link

I'm assuming that's the right place?

Also here is the PHP for the 5 versions of the site, does it look good to you?;

add_action( 'wp_head', function(){
$my_current_lang = apply_filters( 'wpml_current_language', NULL );

if( $my_current_lang == 'en' ){ ?>

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

gtag('config', 'G-6VNPELT58E');
</script>

<?php } elseif( $my_current_lang == 'it' ){ ?>

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

gtag('config', 'G-VVWGPYL9C6');
</script>

<?php } elseif( $my_current_lang == 'ga' ){ ?>

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

gtag('config', 'G-LW6VP1VQX6');
</script>

<?php } elseif( $my_current_lang == 'ar' ){ ?>

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

gtag('config', 'G-LW6VP1VQX6');
</script>

<?php } elseif( $my_current_lang == 'us' ){ ?>

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

gtag('config', 'G-RQM80S1S7N');
</script>

<?php }
});

Thanks again for all your help, hopefully the client will be happy at some point! 🙂

Cheers

John

July 18, 2023 at 3:59 pm #14060867

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hello John,

1. The code structure looks good.
2. I'm not sure about your languages, specially 'us' because usually 'en' is for US English and en-GB for English from Great Britain
3. 'ga' and 'ar' are sharing the same tag ID. Not sure if it's correct.

July 18, 2023 at 4:18 pm #14060923

shulaR

Hi Nicolas,

Thank you for getting back to me. I diod wonder about the languages. Should I use the hreflang for languages rather than code? (image attached)

Cheers

John

Screenshot 2023-07-13 at 12.24.12.jpg
July 19, 2023 at 8:02 pm #14068601

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hello,

No, you should use the language code. So you were right with "en" and "us". It just that usually "en" is used for en_US but your configuration is correct!

July 26, 2023 at 8:20 am #14100853

shulaR

Can I keep this open for a little longer.

Thanks 🙂

July 26, 2023 at 7:36 pm #14106735

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hello,

Sure, this ticket will close after 14 days without any interaction. So, if you need more time, just post a quick update to keep the conversation alive.

In the event, the ticket gets closed, don't hesitate to open a new ticket with a link to this ticket in the description.

July 28, 2023 at 8:51 am #14115553

shulaR

Hi Nicolas,

I hope all's well.

Previously you mentioned that normally the codes for the USA would be 'en' and Britaing would be 'en_gb'. On the site the code is 'us' for USA and 'en' for GB.

Does this affect what a browser would detect? i.e in the US is a browser looking for a 'en_US' code? If it ses that the it has a 'en' could would it present the english site rather than the US version?

or are the codes just for internal use?

Cheers

John

July 28, 2023 at 8:58 am #14115563

shulaR

And the image of what I'm referring to 🙂

Screenshot 2023-07-28 at 09.43.16.png