Cross domain tracking with gtag.js
Cross-domain tracking makes it possible for Analytics to see sessions on two related sites (such as an ecommerce site and a separate shopping cart site) as a single session. This is sometimes called site linking.
Learn more about setting up your basic tracking code.
Cross-domain tracking overview
To track sessions, Analytics collects a Client-ID value in every hit. Client-ID values are stored in cookies. Cookies are stored on a per-domain basis, and websites on one domain cannot access cookies set for another domain. When tracking sessions across multiple domains, the Client-ID value has to be transferred from one domain to the other. To do this, the Analytics tracking code has linking features that allow the source domain to place the Client-ID in the URL parameters of a link, where the destination domain can access it.
Set up cross-domain tracking
To set up cross-domain tracking, refer to the gtag.js developer documentation.
Set up reporting views and add filters
By default, Analytics only includes the page path and page name, not the domains name. For example, you might see your pages appear in the Site Content report like this:
Because the domain names aren’t listed, it might be hard to tell which domain each page belongs to.
To get the domain names to appear in your reports you need to do two things: Create a copy of your reporting view that includes data from all your domains in it and add an advanced filter to that new view. The filter will tell Analytics to display domain names in your reports.
Follow this example to set up a view filter that displays domain names in your reports when you have cross domain tracking set up. For some fields, you need to select an item from the dropdown menu. For others, you need to input the characters here:
- Filter Type: Custom filter > Advanced
- Field A: Hostname Extract A: (.*)
- Field B: Request URI Extract: (.*)
- Output To: Request URI Constructor: $A1$B1
Click Save to create the filter.
You can validate that filters are working as you expect using Google Tag Assistant Recordings. Tag Assistant Recordings can show you exactly how your filters change your traffic.
Add domains to the Referral Exclusion List
When a user journey crosses from your first domain to your second domain, Analytics interprets that as the user having been referred by your first domain to your second domain, and Analytics creates a new session. If you want to be able to track a single session across multiple domains, you need to add your domains to the referral exclusion list.
Check that cross-domain Tracking Works
The best way to validate that cross-domain tracking is set up correctly is to use Google Tag Assistant Recordings. When you make a session that crosses domains, it can tell you instantly whether it worked or not.
Here’s an example Tag Assistant Recordings report that shows what it looks like when cross-domain tracking is not setup properly.