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: Custom Work
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 // Output the corresponding Google Analytics tracking code based on the language So for instance I have this google analytics infor for .ie; Ireland Tag G-LW6VP1VQX6 <!-- Google tag (gtag.js) --> gtag('config', 'G-LW6VP1VQX6'); 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) --> gtag('config', 'G-LW6VP1VQX6'); 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' 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) --> gtag('config', 'G-6VNPELT58E'); and Italy Tag G-VVWGPYL9C6 <!-- Google tag (gtag.js) --> gtag('config', 'G-VVWGPYL9C6'); Would the below be the correct way of implementing these within the PHP?; add_action( 'wp_head', function(){ gtag('config', 'G-6VNPELT58E'); gtag('config', 'G-VVWGPYL9C6'); 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: 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(){ if( $my_current_lang == 'en' ){ ?> <!-- Google tag (gtag.js) --> gtag('config', 'G-6VNPELT58E'); <?php } elseif( $my_current_lang == 'it' ){ ?> <!-- Google tag (gtag.js) --> gtag('config', 'G-VVWGPYL9C6'); <?php } elseif( $my_current_lang == 'ga' ){ ?> <!-- Google tag (gtag.js) --> gtag('config', 'G-LW6VP1VQX6'); <?php } elseif( $my_current_lang == 'ar' ){ ?> <!-- Google tag (gtag.js) --> gtag('config', 'G-LW6VP1VQX6'); <?php } elseif( $my_current_lang == 'us' ){ ?> <!-- Google tag (gtag.js) --> gtag('config', 'G-RQM80S1S7N'); <?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. |
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 |
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 🙂 |