Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client is trying to track data for both the primary English domain and the French domain separately in Google Analytics GA4, using a different domain per language in WPML settings.
Solution:
We recommend inserting the Google Analytics code into the site using a custom function with a filter hook to check the current language. Here's a step-by-step guide:

  1. Add the following code to your theme's
    functions.php

    file:

add_action( 'wp_head', function(){
    $current_language = apply_filters( 'wpml_current_language', null );
?>
  
    <?php if( $current_language == 'fr'){?>
      
    <!-- GA code for FR language here -->
      
    <?php } elseif( $current_language == 'en'){?>
      
    <!-- GA code for EN language here -->
  
<?php } ?>
  
<?php
});

  • Refer to the documentation for more information on how to get the current language with WPML.
  • Check out a similar example in another support ticket.

If this solution doesn't look relevant to your situation, please open a new support ticket and we will be happy to assist you further.

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 1 reply, has 2 voices.

Last updated by Long Nguyen 10 months, 1 week ago.

Assisted by: Long Nguyen.

Author Posts
January 26, 2024 at 2:31 am #15228571

johnM-88

Tell us what you are trying to do?
In WPML settings I have Language URL format set to "A different domain per language" where my primary domain is hidden link and the "different domain" for translated content is hidden link.

I would like the ability in Google Analytics GA4 to track data for both the primary English domain and the French domain *separately*. If french was simply in its own directory /fr/ then filtering in GA would be easy. But I need to use a custom domain for French content.

Is there any documentation that you are following?
No

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

January 26, 2024 at 6:55 am #15228750

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi John,

Thank you for contacting WPML support, I'd be happy to help you with this issue.

If you insert the Google Analytics (GA) to your site by code, you can use the filter hook "wpml_current_language" to check the current language and output the corresponding GA code for the domain. For example:

add_action( 'wp_head', function(){
	$current_language = apply_filters( 'wpml_current_language', null );
?>
 
	<?php if( $current_language == 'fr'){?>
	 
	<!-- GA code for FR language here -->
	 
	<?php } elseif( $current_language == 'en'){?>
	 
	<!-- GA code for EN language here -->
 
<?php } ?>
 
<?php
});

Refer to the documentation https://wpml.org/faq/how-to-get-current-language-with-wpml/
and another ticket https://wpml.org/forums/topic/with-some-code/

Look forward to your reply.
Thanks

January 30, 2024 at 5:04 am #15241455

johnM-88

Thanks so much for your quick reply. I created a new GA4 property for the French domain, added the code you provided to functions.php, and it appears to work. Thanks!