如何使用 Google Analytics 在 SquareSpace 站点上跟踪事件?

How do I track events using Google Analytics on a SquareSpace Site?

我正在尝试使用 Google Analytics 跟踪使用 SquareSpace. The pageviews are working, but for some reason there are no real-time events showing up. Based on the advice from this post, I removed the UI tracking ID from the "external services" section and used the "Code Injection" option to paste the analytics.js JavaScript tracking snippet 创建的网站上的按钮点击进入网站 header。

这是网站相关部分的示例 header(一般化以删除我的特定跟踪 ID):

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXXXXXX-Y', 'auto', 'mycoTracker');

  ga(function() {
  // Logs the tracker created above to the console.
  console.log('GA Tracker:');
  console.log(ga.getAll());
  });

  ga('mycoTracker.send', 'pageview');
</script>

上面控制台中记录的 ga object 没有明显的问题。

我尝试了两种不同的跟踪事件的方法:jQuery 和 onclick

  1. 跟踪事件 jQuery:

当尝试使用 jQuery 跟踪按钮点击时,我将以下内容添加到 SquareSpace 的页脚代码注入部分:

$("#myco-pricing-select-plan-bttn").on('click', function(i) {
    console.log('pricing button clicked.');
    ga('mycoTracker.send', { 
      eventCategory: 'button press',
      eventAction:'click',
      eventLabel: 'core pricing selected',
      transport: 'beacon'});
  console.log('sent pricing button click event to ga.');
});

请注意,我正在将一些信息记录到控制台,因此我知道它已被正确触发。我还发现 this SO 解决方案很有帮助,对其进行了测试,并且我成功地在回调中获得了 "Event Received" 警报。但是,Google Analytics 中没有显示任何事件(在 "REAL-TIME -> Events" 或 "BEHAVIOR -> Events" 部分)。但同样,"pageviews" 出现在 REAL-TIME 部分就好了。

  1. 使用 onclick 跟踪事件:

我还尝试使用 中描述的 HTML 标签中的 onclick 选项来跟踪事件。这是 HTML 的样子:

  <div class="top-button">
    <a class="tracking-href" href="https://example.com/register?plan=1" target="_blank" onclick="ga('mycoTracker.send', 'event', 'Link', 'Click', 'example.com');">
      <button class="special" id="myco-pricing-select-plan-bttn">Select Plan</button>
    </a>
    <p>
      <br />
    </p>
  </div>

目前这两个选项都不适用于 Google Analytics 中的跟踪事件。

FWIW,有两个独立的问题阻止事件出现在 google 分析中。

  1. 有人在高级选项中打开了过滤功能,以防止来自我们 IP 地址的事件出现在 GA 中( 有助于发现这一点)。

  2. 正如 nyuen 上面评论的那样,注入的页脚代码缺少 "event" hitType。这是函数 jQuery:

    $(".myco-pricing-select-plan-bttn").on('click', function(i) {
        ga('mycoTracker.send', {
        hitType: 'event',
        eventCategory: 'Select Plan Link',
        eventAction:'Click',
        eventLabel: this.id,
        transport: 'beacon'});
    });