How to set up cross-domain tracking in Google Analytics

Author:
Setting up cross-domain tracking in Google Analytics allows you to track user sessions that span multiple domains, providing a more accurate view of user behavior as they navigate through different sites or subdomains. Here’s a step-by-step guide on how to set up cross-domain tracking:

Prerequisites

Before you begin, ensure you have:

  • Access to Google Analytics: You should have administrator access to the Google Analytics account where you want to set up cross-domain tracking.
  • Edit Access to Website Code: Ability to edit the Google Analytics tracking code (or use Google Tag Manager) on all domains/subdomains you want to track.

Step-by-Step Guide to Set Up Cross-Domain Tracking

1. Update Google Analytics Tracking Code

  1. Determine Domains/Subdomains:
    • Identify all domains and subdomains that you want to track as part of a single user journey.
  2. Modify Tracking Code:
    • Locate your Google Analytics tracking code snippet (usually found in the <head> section of your website’s HTML).
  3. Add allowLinker Parameter:
    • Add the allowLinker parameter to the tracking code on all pages of all domains/subdomains.
    • This parameter allows cookies to be shared across domains/subdomains.

    Example for Universal Analytics (gtag.js):

    html

    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXX-Y"></script>
    <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'UA-XXXXX-Y', {
    'linker': {
    'domains': ['example.com', 'subdomain.example.com']
    }
    });
    </script>

    • Replace UA-XXXXX-Y with your Google Analytics property ID.
    • Replace example.com and subdomain.example.com with your actual domains/subdomains.

    Example for Classic Analytics (ga.js):

    html

    <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
    _gaq.push(['_setDomainName', 'example.com']);
    _gaq.push(['_setAllowLinker', true]);
    _gaq.push(['_trackPageview']);

    (function() {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
    })();
    </script>

    • Replace UA-XXXXX-Y with your Google Analytics property ID.
    • Replace example.com with your primary domain.

2. Set Up Referral Exclusions (if needed)

  1. Navigate to Admin Settings in Google Analytics:
    • Click on the Admin cogwheel icon (bottom left).
  2. Select Property Settings:
    • Under the Property column, click on “Property Settings.”
  3. Exclude Referrals:
    • Add domains/subdomains to the “Referral Exclusion List” if you want sessions from these domains/subdomains to be treated as part of the same user journey.
    • This prevents new sessions from being created when users move between specified domains/subdomains.

3. Test and Verify

  1. Verify Implementation:
    • Use Google Analytics Real-Time reports or Tag Assistant (Chrome extension) to verify that sessions are tracked correctly across domains/subdomains.
    • Ensure that session continuity is maintained as users navigate between different domains/subdomains.

4. Monitor and Adjust (if needed)

  1. Review Reports:
    • Monitor Google Analytics reports (e.g., Audience, Acquisition, Behavior) to ensure cross-domain tracking is functioning as expected.
    • Check for any discrepancies or issues in session data across domains/subdomains.
  2. Troubleshoot:
    • If there are issues, review the implementation steps, check for correct parameter usage (allowLinker), and ensure cookies are being shared correctly.
    • Adjust tracking code or settings as necessary based on testing and monitoring results.

Additional Tips

  • Use Google Tag Manager: If you prefer using Google Tag Manager, configure cross-domain tracking using GTM by setting up Cross-Domain Tracking variables and tags.
  • Multiple Subdomains: For multiple subdomains, ensure each subdomain’s tracking code includes the allowLinker parameter with the parent domain/subdomain listed in the domains array.
  • Data Sampling: Depending on your Google Analytics account type, cross-domain tracking may lead to data sampling if traffic volumes are high. Monitor sampling levels in reports.

By following these steps and ensuring correct implementation of cross-domain tracking in Google Analytics, you can accurately track user behavior across multiple domains and subdomains, gaining comprehensive insights into user journeys and interactions across your digital properties.