utm_source和utm_medium在SPA中丢失

时间:2019-04-25 12:07:24

标签: google-analytics analytics single-page-application google-tag-manager

我的SPA应用程序和分析出现了一个奇怪的问题。在会话期间,某些用户的源/介质参数丢失了。

我正在按照建议的方式使用历史记录API和Google跟踪代码管理器,并且可以正常工作。但是,对于大约30%的用户,他们松散了utm参数并成为了引荐。我不了解这种行为,以前有人看过吗?

所以,我知道100%是

(source/medium) = mySource / affiliate

但是在分析中,大约30%的用户失去了utm参数,它变成了:

(source/medium) = mySource / affiliate (70%)
(source/medium) = mysource.com / referral (30%)

有什么建议吗?我现在有点迷路了。

3 个答案:

答案 0 :(得分:0)

由于这些站点是单页应用程序(SPA),因此您很可能会遇到“恶意引荐”问题。

在这种情况下,会发生以下情况,即您覆盖了Analytics(分析)匹配中的location字段,丢失了原始的UTM参数,而引荐仍与匹配一起发送,因此Google Analytics(分析)将第二个匹配识别为一个新的流量来源。解决方案之一是存储原始页面URL并将其作为location发送,同时在page字段中发送实际访问的URL。

Simo Ahava撰写的关于此主题的很好的article带有更多提示,可以为您提供帮助。

答案 1 :(得分:0)

这个关于“流氓推荐”问题的 article 公开了一个仅适用于 Google Tag Manager 的解决方案,而且如果您按照文章进行操作,您将为整个应用程序发送相同的页面位置(如果我理解正确),这是我们不想要的。

此外,它没有考虑对参数的一些过滤(仅当存在有用的参数时才发送参数),因此我们提出了这个解决方案。

请注意,代码需要在页面第一次加载和每次路由更改时运行。

获取参数的函数

// return the whole parameters only if at least one of the desired parameters exists
const retrieveParams = () => {
  let storedParams;
  if ('URLSearchParams' in window) {
    // Browser supports URLSearchParams
    const url = new URL(window.location.href);
    const params = new URLSearchParams(url.search);
    const requestedParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'gclid'];
    const hasRequestedParams = requestedParams.some((param) => {
      // true if it exists
      return !!params.get(param);
    });
    if (hasRequestedParams) {
      storedParams = params;
    }
  }
  return storedParams;
}

创建完整网址

// look at existing parameters (from previous page navigations) or retrieve new ones
const storedParams = window.storedParams || retrieveParams();
let storedParamsUrl;
if (storedParams) {
  // update window value
  window.storedParams = storedParams;
  // create the url
  const urlWithoutParams = document.location.protocol + '//' + document.location.hostname + document.location.pathname;
  storedParamsUrl = `${urlWithoutParams}?${storedParams}`;
}

将值发送给分析(使用 gtag)

// gtag
gtag('config', 'YOUR_GA_ID', {
  // ... other parameters
  page_location: storedParamsUrl ?? window.location.href
});

gtag('event', 'page_view', {
  // ... other parameters
  page_location: storedParamsUrl ?? window.location.href,
  send_to: 'YOUR_GA_ID'
})

答案 2 :(得分:0)

答案是(可能)推荐人。当用户访问一个站点(url 中带有 utm)时,它具有引用,例如 xyz.com,当它点击站点时 SPA 不会重新加载页面,这意味着引用仍然是 xyz.com 但 url 现在不包括 utm

因此,对于谷歌来说,这样的第二次页面浏览是一个新的会话。要解决此问题,您需要在第一个页面视图(真实页面加载)上使用引用来提供正确的引用,而在所有其他点击中,它将提供 null 或域作为引用:)

详情: https://dronsom.com/2021/06/11/single-page-application-proper-pageview-measurement-solving-issue-with-google-and-referrer-traffic

相关问题