AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Analytics
1.9K members online now
1.9K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

Google Analytics - Two codes for one website based on language

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

I have the below code in my header:-

    if ($_SESSION['lang'] == 'us') { ?>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-XXXXXXXX-2', 'auto');
      ga('send', 'pageview');
    
    </script>
    <?php } else { ?>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
      ga('create', 'UA-XXXXXXXX-1', 'auto');
      ga('send', 'pageview');
    
    </script>
    <?php } ?>


So depending on which language of the site you are using, a different UA code is used.

It's all working but on Google Analytics I get the following error message:-

Tracking Code Mismatch
Tracking code on page www.xxxxxxxxxx.com does not match property XXXX Website's tracking ID UA-xxxxxxxxx-1.

Any ideas how I can sort this?

Re: Google Analytics - Two codes for one website based on language

Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭
Instead of creating you must do this with subdomain as per the Language. Such as

for English - www.eg.yourdomain.com
for Arabic - www.ae.yourdomain.com

At Google Analytics create one account for - www.yourdomain.com.
Under that create profile as per language. Each profile can get different code and tracking will happen differently and accurately.

Hope this will help you.

Yashwant Naik

Re: Google Analytics - Two codes for one website based on language

[ Edited ]
Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

I've changed it to the following and it has seemed to fix the problem:-

//Analytics tracking for US
$language = $_SESSION['lang'];
if ($language == 'us') { 
    $UA_CODE = 'UA-XXXXXXXX-2'; //USA
} else {
    $UA_CODE = 'UA-XXXXXXXX-1'; //UK
}
?>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', <?php echo $UA_CODE; ?>, 'auto');
  ga('send', 'pageview');

</script>