Change google-analytics TrackingId after creating

2019-07-08 02:24发布

Description:

I have a singlepage application where I use googleanalytics code. The application is working for 4 different country top level domains e.g. www.example.de, www.example.at, www.example.be etc.. It is also possible to change the country without change the domain name. For example if you are working unter the domain www.example.co.uk you can change the country to germany without reloading the page.

Problem:

I need to use different trackingID for each country without reloading the page.

If i load the page with .co.uk toplevel domain tracking id. I can not later change it without reloading the page. How is that possible?

I read How Analytics Works document from google. And used Creating Trackers document to create the tracking code.

Is it possible to change tracking ID after creating it ?

screen shot of mockup

2条回答
迷人小祖宗
2楼-- · 2019-07-08 03:09

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.

查看更多
3楼-- · 2019-07-08 03:14

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.

Mor info on: https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers#working_with_multiple_trackers

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

enter image description here

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

查看更多
登录 后发表回答