Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌分析发送事件回调函数_Javascript_Jquery_Google Analytics_Google Tag Manager - Fatal编程技术网

Javascript 谷歌分析发送事件回调函数

Javascript 谷歌分析发送事件回调函数,javascript,jquery,google-analytics,google-tag-manager,Javascript,Jquery,Google Analytics,Google Tag Manager,我试图在用户注册后,在他重定向之前,向Google Analytics发送一个事件。 我正在使用谷歌标签管理器和通用js 首先,我尝试使用数据层对象,如下所述: 我的函数是这样的: //Registering new user via ajax $.ajax('/register/', { success: function() { //Pushing event to dataLayer dataLayer.push({ 'Ca

我试图在用户注册后,在他重定向之前,向Google Analytics发送一个事件。 我正在使用谷歌标签管理器和通用js

首先,我尝试使用数据层对象,如下所述:

我的函数是这样的:

//Registering new user via ajax
$.ajax('/register/', {
    success: function() {

        //Pushing event to dataLayer
        dataLayer.push({
            'Category': 'Registration Process',
            'event': 'Registration Submit Btn'
        });

        //Logging in new user and redirecting the page with a timeout
        setTimeout(function(){
            loginAction();
        }, 500)
    }
})
问题是,我只收到了大约25%的活动,所有其他活动都丢失了。我不知道在向数据层添加对象后,事件是否以及何时真正发送到Google,我认为75%的事件根本没有发送

现在,我尝试实施另一种方法:

//Registering new user via ajax
$.ajax('/register/', {
    success: function() {

        //Sending event through ga('send')
        parent.ga('send', 'event', 'Registration Process', 'Registration Submit Btn');

        //Logging in new user and redirecting the page with a timeout
        setTimeout(function(){
            loginAction();
        }, 500)
    }
})
但是
ga('send')
没有任何回调函数

如何确保使用数据层或
ga('send')将事件实际发送到谷歌?
来自文档:

在本例中,field name对象配置页面参数和设置hitCallback。一旦跟踪器完成数据发送,将向用户显示一个警报框


您可以对事件、页面视图等使用hitCallback

终于拿到了。这相当复杂,文档中没有描述。 在我的例子中,我使用谷歌标签管理器,所以为了成功地触发事件并获得回调,我必须采取一些变通办法

首先,我们必须获取,这是发送到Google服务器的任何事件所必需的。事实上,它保存在cookies中,但谷歌不建议直接从那里获取

下面是Google推荐的方法,但是如果您使用Google Tag Manager,这个将不起作用

 ga(function(tracker) {
       var clientId = tracker.get('clientId');
 });
相反,您必须从getAll方法获取ClientId

 var clientId = ga.getAll()[0].get('clientId');
之后,您必须创建新的跟踪器

    ga('create', 'UA-XXX-YYY', {
        'clientId': clientId
    });
之后,我们可以发送一个事件:

 ga('send', 'event', {
   'eventCategory': 'YOUR Category Name', //required
   'eventAction': 'YOUR Action name', //required
   'eventLabel': 'YOUR Label',
   'eventValue': 1,
   'hitCallback': function() {
       console.log('Sent!!');
      //callback function
    },
   'hitCallbackFail' : function () {
      console.log("Unable to send Google Analytics data");
      //callback function
   }
});
来自谷歌分析文档

您可以编辑自己的
hitCallback
函数

在这里,您可以在上面的示例中定义函数(
criticalCode
),该函数可以确保将数据发送到Google Analytical,然后使用您的代码

为了更好地理解分析api,fyr:

看看:有一个老跟踪器,我使用的是通用分析,没有gaq功能不启动……我想,这是因为我使用的是谷歌标签管理器银行!仅供参考,至少现在看来,
hitcallbackail
似乎不存在了?不确定-我们不得不转向服务器端发送,因为js发送不可靠并且会丢失数据谢谢救了我一天:)谢谢也救了我一天!这是一段允许自定义超时的代码。永远不要假设谷歌是在线的:)
 ga('send', 'event', {
   'eventCategory': 'YOUR Category Name', //required
   'eventAction': 'YOUR Action name', //required
   'eventLabel': 'YOUR Label',
   'eventValue': 1,
   'hitCallback': function() {
       console.log('Sent!!');
      //callback function
    },
   'hitCallbackFail' : function () {
      console.log("Unable to send Google Analytics data");
      //callback function
   }
});
// Alerts the user when a hit is sent.
ga('send', 'pageview', {
  'hitCallback': function() {
    alert('hit sent');
  }
});
// Use a timeout to ensure the execution of critical application code.
ga('send', 'pageview', {'hitCallback': criticalCode});
setTimeout(criticalCode, 2000);

// Only run the critical code once.
var alreadyCalled = false;
function criticalCode() {
  if (alreadyCalled) return;
  alreadyCalled = true;

  // Run critical code here...
}