简体   繁体   中英

change browser language code with javascript for google analytics

I am using google analytics code but in my google analytics there are languages of many users not correctly identified - because in section area of our intranet webapp there are many users with other languages but the main language is the same, so I need to record this "correct" language. I would like to use my php script to add some variable to analytics tracking code to record right language.

What should I change/add in ga code?

<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', 'auto');
  ga('send', 'pageview');
</script>

According to the documentation that should be the user language field

<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', 'auto');
ga('set', 'language', <?php echo $userLang; ?>');
  ga('send', 'pageview');
</script>

where $userLang would be the PHP var with the language, and the "set" call takes care that this is applied to all subsequent hits (pageviews, events, transactions etc) on the page.

Expected format is a IETF language tag , eg "en-us".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM