Change Google-analytics TrackingId After Creating It
Solution 1:
You can do a workaround:
- Create a cookie, store the country name
- When the user clicks to change the country, push a dataLayer Object and change the value of the cookie with that country.
- Create a custom variable and the set value of that variable to your tracking id based on the cookie value.
So you will be landed on two scenarios:
If the user does not change the country, fire the tag with UA id value equal to the variable of the cookie.
If the user changes the country, fire the tag with UA id value equal to the variable of the cookie triggering on the data layer push event.
PS: It's little trick and I can not try it anyway as I don't have the website. Please do check thoroughly.
Solution 2:
Wow, this is very interesting. But it can be risky to take action without know that technology works. Maybe you need to do a Crossdomain for example. to have the same id on both account. But I will try to keep it simple:
Yes, it's possible executing the command create, to do this when the user reach you domain track the the regular tracking code
<!-- Google Analytics -->
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
But when the user changes the language or country code you have to create a new tracking object (after the new URL is on the broswer).
ga('create', 'UA-XXXXX-Y', 'auto', 'trackercountryXX');
So, on the SPA you have you push the Pageviews manually, with the command
ga('send', 'pageview'); //to you original loaded tracking code
or
ga('trackercountryXX.send', 'pageview'); //If the interaction goes to the new tracking code.
Bonus track, Use GTM:
With the Google Tag Manager and the dataLayer you can set the country on every hit. You can create for example a Vlookup Table
dataLayer.push({'country' : 'xxxx'})
For example, change the UA for the variable that read your value of the country on the dataLayer.push and return the needed UA on the Tags. It's the best way. But this answers is getting unnecessary long. Read more information https://developers.google.com/tag-manager/quickstart
Post a Comment for "Change Google-analytics TrackingId After Creating It"