Let’s say you have two completely different primary domains, say, example.com, and purchase.com, and you want to track them as a single property on Google Analytics. For example, if a user goes from one site to the other, you want that visitor to show up on the same Google Analytics report and not count as two visitors, but rather as a single visitor who crossed domains. How do you do it?
Update for Google Analytics 4
Since I first wrote this article, Google has introduced Google Analytics 4, which makes cross-domain tracking MUCH easier. In a nutshell:
- Go to Admin Settings (gear icon in lower left corner)
- Go to Data Streams and click on your site’s stream
- Click “Configure tag settings”
- Click “Configure your domains”
- Add a “condition” for each site you want to track.
- That’s basically it!
This is SO much easier than the old way!
I’m keeping the legacy article below because it has a technique for removing query strings in the URL bar of the browser.
LEGACY ARTICLE: The Problem
The problem is that the cookies (or cookie, if you are using Universal Analytics) used by Google Analytics to track visitors won’t get passed across domain names. This isn’t Google’s fault; it’s just the way it is for security reasons.
The solution that Google has come up with is to pass the cookie information through the link in the form of a query string variable. Let’s say you want to have a link on example.com to purchase.com. You would add a query string to the links to purchase.com so they would look something like this if you are using Universal Analytics:
This URL would appear in the user’s browser after they reach the site. Personally, I thought this was REALLY UGLY and unacceptable! Imagine users sharing this link on Twitter! The query string is even more convoluted and has more parameters if you use Classic Analytics.
In all of my research online, I was unable to find a method to do cross-domain analytics without an ugly parameter in the URL. So, I came up with my own trick.
In this article, I’ll briefly explain how to set up cross-domain Google Analytics using their latest Universal Tracking code, then I’ll describe by trick to get rid of the ugly query string.
How to Set Up Cross-Domain Universal Analytics
Google has done a pretty good job of documenting how to set up cross-domain analytics, so I won’t repeat the individual steps here.
If you’re using Google Analytics 4, check out this page for setting up cross-domain analytics (the video is very good).
If you’re using gtag.js code, see this page for setting up cross-domain analytics.
If you’re using analytics.js, see this page for setting up cross-domain analytics.
The one major bump I ran into occurred when I tried to set up the new View and Filters in Google Analytics, as described in the article. Make sure you have full Edit permissions, not only on the Google Analytics property but also on the account to do this. If you own the GA property, you probably already have permissions. Otherwise, get the owner to give you full edit permissions everywhere.
Another thing you’ll want to do which is not mentioned in the Google article is to exclude self-referrals from your reports. This article tells you how to exclude self-referrals.
If you are using Google Tag Manager and want to set up cross-domain analytics there, check out this article and excellent video on Cross Domain Tracking in Google Tag Manager.
Testing and Debugging
If you’ve done the steps correctly and you click on a cross-domain link that you want to track, you should see the “decorated” URL in the browser window.
I recommend leaving your site in this state and checking your Google Analytics report in a day or two, just to make sure the data is being gathered properly. You should be able to log into Google Analytics and see your separate domains tracked as if they were part of the same site. The Behavior Flow diagrams should show traffic going from one domain to the other.
If you are not seeing separate domains in your reports, the problem could be that you have not set up your filters correctly. By default, Google Analytics does not show the domain name in its reports – it only shows the part after the slash. So, if you are going from example.com/buy to purchase.com/cart, then you would see visits from “/buy” to “/cart”. But, if you are going from example.com to purchase.com, all of the visits will show up as “/”, which is not helpful. If that happens, go back and set up your filters.
Don’t proceed unless you have cross-domain analytics working.
Removing the Ugly Query String Variable
For our purposes, there are many settings that would probably work just fine. These are just the settings that I used which worked.
Next, click the “Build!” button and copy the code into a file called uri.js and upload it to your website.
On all of the destination pages that you have cross-domain links to, add this code to the bottom of the page before the closing <body> tag:
Now, when you visit that page from one of your other domains, the ugly query string should disappear after the page finishes loading!!!
Let me say that this technique is still experimental and not endorsed by Google by any means. Before trying this on a production site, do offline experiments!!
Google’s tracking code is asynchronous, which means that there could be timing issues where this code removes the query string variable before Google is done looking at it. But, it has worked fine in my experiments. I’ll post any new findings I have.
If you try this, let me know how it works for you!! – Brian