February 22, 2021

How to fix self-referrals in Google Analytics 4

by Justus

A self-referral is the scenario in which the source of Google Analytics traffic is identical to the domain that this GA property is supposed to track. It’s pretty obvious that / referral

is not a useful traffic source for the website. This is traditionally caused by errors in the implementation, for example in the context of a single page application and related to the Rogue Referrer issue.

Self-referrals in GA4

In Google Analytics 4 however, self-referrals are a regular occurence. I don’t know why, because it obfuscates the true source of the traffic which you can verify yourself if you look at your raw data through BigQuery. But unless you’re a die-hard SQL fan, you probably want to enjoy your frontend reports as well.

Even Google’s own Merchandise Store has this issue where a significant amount of traffic is attributed to… itself?

An example variable reference in a Server Side GTM container

Before you start implementing a workaround for GA4, check your Universal Analytics (if you’re still running one) to make sure you can’t reproduce the self-referrals there. If you can, it’s probably not GA4’s fault.

I don’t seem to be alone in with this issue, judging from this /r/Google Analytics thread where ultimately no real answer came up.

Cross-domain tracking doesn’t affect self-referrals

The issue appears to be unrelated to cross domain tracking. To be specific, adding the property’s primary domain to the list of cross-domain measurement will not keep it from showing up in your acquisition reports. You can find that setting in the details of your Web Data Stream:

The web data stream of the Google Merchandise Store

It depends on the RegEx implementation whether this is a valid expression or if it should be .* with a leading dot, but for our self-referral issue that’s irrelevant, I tested multiple different options.

Fixing it during data collection

Usually the Referrer is automatically collected from the browser’s built-in document.referrer attribute, but you can override it with the page_referrer event parameter.

Create a custom JS variable

Use the following code to create a Custom JavaScript variable in Google Tag Manager

 * Overrides the Referrer with an empty string in case it matches the user's current
 * domain. This avoids self-referrals in Google Analytics 4 reports.
 * Set this variable as your GA4 page_referrer parameter.
function() {
  try {
    var referrer = new URL({{Referrer}}).host
    var hostname = new URL({{Page URL}}).host
    if (referrer === hostname) {
      return ""
    else return referrer
  catch(e) {
    return {{Referrer}}

This won’t work in Internet Explorer because it doesn’t support the URL interface, but it’s 2021 so we should ignore that.

A custom JS variable in GTM

Then, go to your Google Analytics 4 Configuration tag and manually set the page_referrer parameter to reference the {{js.referrer}} variable you just created.

Overriding the page_referrer in GTM


Before publishing the changes, make sure they work. Using GTM’s Preview Mode, make sure the following works using the Developer Tools of your favorite browser:

  • When navigating through your site, the dr parameter should remain empty on Google Analytics requests
  • When entering your site from another domain through a link, that referrer should remain intact and still appear in the dr parameter, e. g. if you arrive from Google

This is not working:

Self referral in Chrome DevTools

This looks better:

Self referral in Chrome DevTools

and regular referrers from other domains should stay, when arriving on your site from another domain (e. g. Google):

Self referral in Chrome DevTools