Domain name migration with no data loss in Google Analytics
We have a client who is going to change the domain of their site from thewebsite.co.uk to thewebsite.com.
After the migration from one top level domain name to another, you typically end up with a surge of new visitors.
This happens because GA drops a 1st-party visitor cookie on the domain that you are currently on, e.g. thewebsite.co.uk and it can’t drop a cookie onto a different domain e.g. thewebsite.com.
Why? Without going into deep technical detail, this is how browsers work and it’s to do with security, so there isn’t much we can do about web and protocol standards.
It’s bad in many ways from simple user trends across large date ranges to skewed recency & frequency metrics, especially for businesses where it takes 6-9 months for a user to convert.
But don’t worry, there is a solution (I’m sure there might be many other permutations, but I found this one as the simplest one for me) – iframe.
Iframes cause a lot of headaches, but – I will say that to some of my colleagues’ bemusement – I like iframes (for this type of job only, though)!
The solution is relatively simple – while the website lives on the old domain, e.g. thewebsite.co.uk (we will call it parent), we will inject the iframe from the new domain with the parent’s GA cookie information, e.g. thewebsite.com?ga=123325435.1321321 (child). This will allow us to copy over the cookie from the old site to the new one.
If this is done in advance, let’s say 1 month before the migration to the new domain, you copy over the cookies for a large number of your returning users.
Let’s move on to the actual implementation. You will need the following:
- Parent GTM container script on thewebsite.co.uk
- A simple publicly available blank page on thewebsite.com, e.g. thewebsite.com/gtm
- Child GTM container script on this simple blank page mentioned above. You can use the same container for both parent and child, but for simplicity, I have 2 GTM containers.
Step 1 – Create GA Cookie variable which in the parent GTM:
Step 2 – Inject the iframe with GA cookie value as a query string parameter using Custom HTML tag and get it to fire on all pages of your old domain:
Tag type: Custom HTML
Trigger type: DOM Ready to fire on the parent domain name only.
Step 3 – Create a variable in parent GTM to consume GA cookie value from the parent.
We’ve added this data as a query string parameter to the iframe embed (in step 2 above):
Step 4 – Create the _ga cookie with the value captured on the parent. You can set
Tag type: Custom HTML
Trigger type: Pageview – All pages, or if you share the GTM container, make it specific to your new domain name
Step 5 – Test using debug view and console.
Step – 6 – Publish and enjoy!
P.S. I’ve been avoiding writing blog posts for over 7 years, but this topic and the solution inspired me to write this one. Hopefully, this is the first one of many to come.