Search code examples
javascriptgoogle-analyticssingle-page-applicationgoogle-tag-managergatsby

Google analytics splits sessions on single page application (Rogue Referral Problem)


We are using gatsby to develop our website and I am using the gatsby-plugin-google-tagmanager plugin in order to fire google analytic events..

One issue we face is that when the user visits our site from utm links the session seems to split the exact same second he lands on the page.

What I do so far

Fire a Page View Google Analytics: Universal Analytics tag using the gatsby-route-change trigger.

GA debug report

One thing that seems abnormal is that on every route change, using the GA Debug tool, a new Creating new tracker log is created.

enter image description here

Ways I tried to fix this

Read an article that on single page applications you might get faulty values for page, location and referrer properties, so this fools google analytics to create a new session each time, so that might be the reason why the session breaks.

What I tried to do was to override these values in the GA tag. However, this does not seem to fix the issue.

// Location override gtm variable
function () {
    return window.document.location.protocol + '//' +
      window.document.location.hostname +
      window.document.location.pathname +
      window.document.location.search
}

// Referrer override gtm variable
function () {
    return window.history.state.referrer
}

// Page override gtm variable
function() {
  var path = window.location.pathname + window.location.search + window.location.hash
  var index = path.indexOf('?');
  if(index > -1){
     path = path.substring(0, index);
  }
  return path;
}

Got any idea on this? Is it possible that this behavior splits our session? Is there anything else you recommend?


Solution

  • https://www.simoahava.com/gtm-tips/fix-rogue-referral-problem-single-page-sites/

    This article answers the question.

    With Google Tag Manager, every single Universal Analytics Tag that fires on the site creates a new, unique tracker object. This means that the Document Location field is updated with every Tag you fire, which is a problem if the URL changes due to browser history manipulation. Thus you can end up with a situation where the first Universal Analytics Tag has gclid in the URL, attributing the session to AdWords, but the next pageview doesn’t have this in the URL anymore, as you would not include it in the “virtual” pageview path names. Instead, since gclid is no longer in the URL, GA looks at the HTTP referrer of the page to see what the previous page was for attribution. It finds google.com, as you came from the search engine (HTTP referrer is not updated when manipulating the URL with the browser History API). Thus a new session starts with attribution to Google Organic! I’ve dubbed this as the Rogue Referral problem.

    Solution

    Manually Set Document Location To Prevent Rogue Referrals

    1. Create a script to save the landing URL in the dataLayer

      window.dataLayer = window.dataLayer || [];
      window.dataLayer.push({
      originalLocation: document.location.protocol + '//' +
                      document.location.hostname +
                      document.location.pathname +
                      document.location.search
      });
      
    2. Create a script variable to get the current page. (if you don't need hash remove it)

      function() {
        return window.location.pathname + window.location.search + 
          window.location.hash
      }
      

    Add variables to all you Universal Analytics by manually setting the fields location and page

    enter image description here