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 ?
You can do a workaround:
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.
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
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).
So, on the SPA you have you push the Pageviews manually, with the command
or
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
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