在Google Analytics.js事件跟踪发送中使用.Net变量

Using .Net Variables in Google Analytics.js Event Tracking Send

本文关键字:Net 变量 Analytics Google js 事件 跟踪      更新时间:2023-09-26

我正在尝试使用代码隐藏变量设置的标签发送事件跟踪值。它在页面上正确呈现,但实际上并没有跟踪这些事件。动态事件不会显示在事件区域下的GA中,但硬编码的"下载"事件会显示。

如果我尝试这样做,只有第一个"下载"事件显示在GA:中

$('#DownloadButton').on('click', function () {
      var s1 = "<%= Var1 %>";
      var s2 = "<%= Var2 %>";
      var s3 = "<%= Var1 %> | <%= Var2 %>";
      ga('send', 'event', 'button', 'click', 'Download');
      //these next variable labels never are received in GA
      ga('send', 'event', 'cat1', 'v1', s1);
      ga('send', 'event', 'cat2', 'v2', s2);
      ga('send', 'event', 'cat3', 'both', s3);
});

如果我将服务器vars内联,则会发送"下载"和Var1,但不会发送最后两个:

$('#DownloadButton').on('click', function () {
      ga('send', 'event', 'button', 'click', 'Download');
      ga('send', 'event', 'cat1', 'v1', "<%= Var1 %>");
      //these last two never get received by GA
      ga('send', 'event', 'cat2', 'v2', "<%= Var2 %>");
      ga('send', 'event', 'cat3', 'both', "<%= Var1 %> | <%= Var2 %>");
});

它似乎是在达到"<%=Var1%>"的末尾后断开的。

Var1是一个类似于"Gala Apple"的名称,并在页面上正确渲染:

ga('send', 'event', 'sheet', 'Side1', "Gala Apple"); 

GA在chrome商店上有这个工具:

https://chrome.google.com/webstore/detail/google-analytics-debugger/jnkmfdileelhofjcijamephohjechhna?hl=en

它允许您查看它向GA服务器发送的内容。要使用它,请打开chrome,启用调试器,然后在开发工具中打开控制台选项卡。当您加载页面并触发事件时,它应该将它们记录到窗口中。

如果你在点击#DownloadButton时观看它,它应该会对正在发生的事情有所了解。我的猜测是,它没有到达这些行,因为你的按钮上附加了另一个click处理程序,它可以更改页面位置,或者做一些事情来停止执行。由于js是单线程的,这取决于它需要多长时间,它可能会在发送完其他跟踪信标之前离开页面。