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 3 replies, has 2 voices.

Last updated by Dražen 1 year, 5 months ago.

Assisted by: Dražen.

Author Posts
September 19, 2023 at 10:46 pm #14425619

julieC-6

Tell us what you are trying to do? I would like to get language information in my GA4 analytics. I use the variable appended to the url as my language switcher - lang=pt-br etc.
I remember seeing an article on your site about this exact issue but I can no longer find it. It had PHP code that needed to be added and then instructions about the GA4 dimension. Could you please help me?

Is there any documentation that you are following? https://wpml.org/faq/how-to-get-current-language-with-wpml/

What is the link to your site? interrainternational.com

September 20, 2023 at 7:20 am #14426877

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

I assume you mean on the next forum tickets. It is suggested to use 2 different codes/tags for each language:

- https://wpml.org/forums/topic/setting-google-analytics-with-2-domains/
- https://wpml.org/forums/topic/google-analytics-tag-im-multi-languages/

Hope it helps,
Regards,
Drazen

September 21, 2023 at 9:47 pm #14442547

julieC-6

Hi there - Thanks for your response, however, the links you sent are old and are not for GA4 which collects very differently. is there anyway that this would work instead - this is to create an event called language_viewed?

function track_language_in_ga4() {

if (function_exists('gtag')) {
// Get the language parameter from the URL
$language = isset($_GET['lang']) ? sanitize_text_field($_GET['lang']) : 'en';
?>
<script>
gtag('event', 'language_viewed', {
'language': '<?php echo $language; ?>'
});
</script>
<?php
}
}
add_action('wp_head', 'track_language_in_ga4');

Do you have information specific for GA4 (not in a woocommerce store)?

September 22, 2023 at 6:12 am #14443455

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

we do not have specific information about this, nor can we provide help with custom coding, fur such I advise hiring a WPML contractor:

- https://wpml.org/contractors/

From my side, as I am not sure exactly how are you implementing it, I suggest either using if method or using 2 different tags for each language. If you need to get the current language you can also use our hook wpml_current_language.

- https://wpml.org/faq/how-to-get-current-language-with-wpml/

Let me know if any other specific questions and I will be glad to help.

Regards,
Drazen