How to fix self-referrals in Google Analytics 4 | owntag.eu

How to fix self-referrals in Google Analytics 4

In Google Analytics 4's campaign tracking self-referrals can hide where your traffic is really from. Find out how to get rid of it.

Justus

Justus

owntag Founder

published February 22, 2021

Update March 5th 2021

Google Analytics 4 just got an update which makes sure that internal referrals a.k.a. self-referrals are automatically detected and excluded.

The official documentation for referral exclusion in GA4 puts it like this:

Automatic self-referral detection

A self-referral is referral traffic that can originate from pages within your own domains. By default, Analytics will not identify traffic as referral when:

The referrering website matched the same domain of the current page or any of its subdomains (e.g. your own website).

The referrering website is a result of a cross-domain measurement setup, e.g. when a user navigates across domains that you have configured in your domains list and the current page contains the linker parameter _gl.

Original post (which should no longer be relevant)

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

hume.dev / referral

is not a useful traffic source for the hume.dev 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 .*.googlemerchandisestore.com 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

Testing

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

Become a Server Side Tagging Pro with owntag

Take control of your digital data collection with Server Side Tagging and Server Side GTM – easily hosted with owntag.

App screenshot