'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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source