繁体   English   中英

使用Google Analytics(分析)hitCallback在重定向到url之前发送事件

[英]using Google Analytics hitCallback to send event before redirection to url

单击特定链接时,我试图触发Google Analytics(分析)事件。 为了确保事件在重定向到url之前已发送,Google建议使用hitCallback函数。

我以这种方式尝试了一下,但由于链接立即打开而无法使用:

$('#service a').on("click",function(e){

  e.preventDefault();
  var title = $(this).data('title');
  var url   = $(this).attr('href');

  if ( typeof ga === 'function' ) {
    //console.log('started');
    ga('send', {
      hitType: 'event',
      eventCategory: 'header',
      eventAction: 'nav',
      eventLabel: 'service',
      eventValue: title,
      hitCallback : createFunctionWithTimeout(function() {
         //console.log('done')
         document.location = url;
      }, 100000)
    });
  }else{
    document.location = url;
  }

});

这是 Google建议的超时功能

function createFunctionWithTimeout(callback, opt_timeout) {
  var called = false;
    function fn() {
      if (!called) {
        called = true;
        callback();
      }
    }setTimeout(fn, 1000);
  return fn;
}

我想知道为什么它不起作用,或者是否有另一种方法可以实现此目的。

事件值应为整数类型。 您可以尝试对整数进行硬编码以查看其是否有效吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM