Google 分析在单页应用程序上拆分会话(流氓推荐问题)

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

我们正在使用 gatsby 来开发我们的网站,我正在使用 gatsby-plugin-google-tagmanager 插件来触发 google analytic events..

我们面临的一个问题是,当用户从 utm 链接访问我们的网站时,session 似乎在他登陆页面的同一秒内分裂。

到目前为止我做了什么

使用 gatsby-route-change 触发器触发 Page View Google Analytics: Universal Analytics 标签。

GA调试报告

一件看起来不正常的事情是,在每次路由更改时,使用 GA Debug 工具,都会创建一个新的 Creating new tracker 日志。

我尝试解决此问题的方法

阅读一篇文章,在单页应用程序中,您可能会得到 pagelocationreferrer 属性的错误值,因此这会愚弄 google 分析来创建一个每次都有新的会话,所以这可能是会话中断的原因。

我试图做的是覆盖 GA 标签中的这些值。但是,这似乎并不能解决问题。

// 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;
}

对此有什么想法吗?这种行为是否有可能分裂我们的会话?您还有什么推荐的吗?

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

这篇文章回答了这个问题。

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.

解决方案

手动设置文档位置以防止恶意引用

  1. 创建脚本保存登陆URL在dataLayer

    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
    originalLocation: document.location.protocol + '//' +
                    document.location.hostname +
                    document.location.pathname +
                    document.location.search
    });
    
  2. 创建脚本变量以获取当前页面。 (如果你不需要哈希删除它)

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

通过手动设置字段位置和页面向所有 Universal Analytics 添加变量