如何使用新的gtag代码在Google Analytics中跟踪基于js的链接

时间:2018-01-15 15:22:02

标签: google-analytics

我之前在谷歌分析中跟踪点击以打开标签/模态等。我必须更改为新的gTag代码,现在没有跟踪js链接。我该如何解决这个问题?

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxx-5"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
    // line below previously pushed js links events
    gtag('event', 'pageview', { 'page': location.pathname + location.search + location.hash});
    gtag('config', 'UA-xxxxxxx-5', {
  'linker': {
    'domains': ['example.org.au']
  }
});

1 个答案:

答案 0 :(得分:1)

我修好了。

要获取包含页面加载时锚点的完整路径:

<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

    gtag('config', 'UA-xxxxxxx-5', {
    'page_path': location.pathname + location.search + location.hash,
  'linker': {
    'domains': ['example.org.au']
  }
});
</script>

然后跟踪JS中触发的事件(例如标签打开)

 link =  location.pathname.replace(/^[\/]/, '') + location.search + location.hash
 gtag('event', 'page_view', {'page_path': link});