[GA4] Set up cross-domain measurement

This article is for website owners who need unified measurement across more than one domain, e.g. across a customer website and a separate shopping cart domain. This article pertains to Google Analytics 4 properties.

Cross-domain measurement allows activity to be accurately attributed to a single user as they cross domains. Outbound clicks that would ordinarily trigger an event via enhanced measurement are ignored when the outbound link leads to a domain that you've listed as part of cross-domain measurement.

  Gain customer journey insights, at no cost

By getting started, you can explore how customers interact across your websites using features like cross-domain measurement.

 

Video overview

Cookies and cross-domain measurement

When there is consent, Analytics uses first-party cookies to set IDs for each unique user and each unique session.

  • Without cross-domain measurement, new cookies with new IDs are created for each domain a user visits. As a result, a single user visiting different root domains (e.g. example.com and anotherexample.com) on the same device will be identified separately (two users and two sessions instead of one user and one session).
  • With cross-domain measurement, the cookies retain the same IDs as they are passed from one domain to another via a URL parameter (_gl) when the user navigates between domains through a link or a form. As a result, Analytics identifies just one user and one session.
Note: This setup is recommended for subdomains because when the users navigate from one subdomain on your site to another subdomain that uses a different cookie domain, self-referrals can appear. Self-referrals may mean that traffic to your site is being attributed incorrectly.

Ways to prevent self-referrals:

  • Ensure that all pages on your site are tagged with the Analytics measurement code
  • Make sure that cross-domain measurement is configured correctly on all your pages
  • Check your cookie domain settings in your measurement code to be sure that all subdomains in a domain are using the same cookie domain
  • Add the subdomains to the exclusion list

Set up cross-domain measurement via Admin

Check your tag (e.g. the Google tag on each of your HTML pages) for each domain that you want to include in your cross-domain measurement. The tag on each page must use the same tag ID (i.e., the same "ignored" ID) from the same web data stream.

  1. In Admin, under Data collection and modification, click Data streams.
    Note: The previous link opens to the last Analytics property you accessed. You can change the property using the property selector. You must be an Editor or above to set up cross-domain measurement, and you can create up to 100 conditions.
  2. Click Web, then click a web data stream.
  3. In the web stream details, click Configure tag settings (at the bottom).
  4. In the Settings section, click Configure your domains.
  5. If you use the same Google tag across domains, they are automatically detected and show up in the Recommendations section. To accept a recommendation, click Add.
    To manually add a domain, click Add condition under Include domains that match the following conditions:
    • Choose a match type.
    • Under Domain, enter the identifier for the domain you want to match (e.g., example.com).
    • Add each domain you want to include in cross-domain measurement.
    Conditions are evaluated using OR logic.
  6. Click Save.

Manual setup

The recommended approach is to configure cross-domain measurement via the Analytics interface. However, there might be scenarios where you need to implement a custom solution via the measurement code. In these cases, you can set it up manually by using the get and set commands of the gtag.js API to persist IDs across domains. Learn more about setting up cross-domain measurement manually.

Example

To retrieve a client ID on the source domain, you use the get method:

  
gtag('get', 'TAG_ID', 'client_id', (client_id) => {
  // Store the client ID in a variable.
});
gtag('get', 'TAG_ID', 'session_id', (session_id) => {
  // Store the session ID in a variable.
});

  

Once you have the client and session IDs, add them to the link that points to the destination domain:

<a href="https://example.com/?clientId=XXXXX&sessionId=YYYYY">example.com</a>

 

On the destination page, read the IDs from the URL and set them with the config command:


gtag('config','TAG_ID', {
'client_id': getClientIdFromUrl(),
'session_id': getSessionIdFromUrl()
});
  

Verify that cross-domain measurement is working properly

Cross-domain measurement works by appending parameters to the URLs on your website. In rare cases, your web server might run into an error, such as returning a 5xx error response code or failing to start a download.

To verify cross-domain measurement works:

  1. Open a page of your site that contains a link or a form that points to a domain you configured for cross-domain measurement.
  2. Click the link or submit the form to navigate to the destination domain.
  3. Verify that the page loads correctly.
  4. Verify that the URL in the destination domain contains the linker parameter _gl. For example: https://example.com/?_gl=1*abcde5*.
  5. If your website provides any downloads: Navigate to a page with the linking parameter in the URL and start a download. Verify that the download starts successfully.

If you encounter an error, try Troubleshooting cross-domain measurement.

Troubleshooting

If you can't confirm the presence of the parameter _gl in the URL of the destination domain per the steps above, double check that the domain you configured in the interface matches the domain your site is pointing to.

It's also possible that your website is configured in a way that prevents cross-domain measurement from working correctly. With the help of an experienced web developer, investigate whether your website is being affected by the following issues.

URL redirects

If the destination page redirects or doesn't support arbitrary query parameters, the parameter _gl may end up being removed from the URL even if it was appended by Analytics in the previous page. (This usually happens too quickly to observe by eye.) To confirm if this is happening to your site, check the network requests made by the previous page using the Chrome Developer Tools. To fix this issue, configure your site to preserve the parameter _gl in redirects.

Conflicts with other scripts

Cross-domain measurement works by creating an event listener on the document node of a page. When users click a link, the click event bubbles up to the document node and the parameter _gl is added to the link URL. This process cannot work properly in the following scenarios:

  • When the navigation is triggered by JavaScript as opposed to a direct response to a user action.
  • When competing scripts in the page stop the event propagation before it reaches the document node; for example, when Event.stopPropagation() is used.

Was this helpful?

How can we improve it?
Search
Clear search
Close search
Google apps
Main menu
16164407320820804097
true
Search Help Center
true
true
true
true
true
69256
true
false