谷歌分析发送事件回调函数 [英] google analytics send event callback function

查看:411
本文介绍了谷歌分析发送事件回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户注册后将事件发送到Google分析,并在之前发送重定向事件。
我正在使用Google跟踪代码管理器和univerasl js。



首先,我试图使用 dataLayer 对象,如此处所述: developers.google



这就是我的函数的样子:

  //通过ajax注册新用户
$ .ajax('/ register /',{
success:function(){

//将事件推送到dataLayer
dataLayer.push({
'Category':'Registration Process',
'event':'注册提交Btn'
});

//登录新用户并重定向页面超时
setTimeout(函数(){
loginAction();
},500)
}
})

麻烦在于我只收到所有事件的25%,所有其他人都丢失。我不知道在向dataLayer添加对象后是否以及何时将事件发送给Google,我认为75%的事件根本不会发送。



现在, m试图执行另一种方法:

pre code $ //通过ajax注册新用户
$ .ajax('/ register / ',{
success:function(){

//通过ga发送事件('send')
parent.ga('send','event','注册(');

//登录新用户并用超时重定向页面b $ b},500)
}
})

但ga( 'send')没有任何回调函数!



我如何确定事件实际上是使用dataLayer或ga('send')发送给Google的?

解决方案

终于明白了。这很复杂,并没有在文档中描述。
在我的情况下,我使用Google跟踪代码管理器,因此我需要做一些解决方法才能成功触发事件并获得回叫。

首先,我们有获得 ClientId ,这是任何活动所必需的发送到Google服务器。实际上它保存在Cookie中,但Google不建议直接从这里取得。



以下是Google推荐的方式,但如果您使用的是Google跟踪代码管理器,则无法使用


$ b $ pre $ ga(函数(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':'您的类别名称',//必需
'eventAction':'您的Action name',//必需
'eventLabel':'您的标签',
'eventValue':1,$ b $'hitCallback':function(){
console.log '已发送!!');
//回调函数
},
'hitCallbackFail':function(){
console.log(无法发送Google Analytics数据) ;
//回调函数
}
});


I'm trying to send event go google analytics after user is registered and before he's redirected. I'm using Google Tag Manager and univerasl js.

First, I was trying to use dataLayer object, as described here: developers.google

That's what my function looked like:

//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)
    }
})

The trouble is that I was receiving just about 25% of all events, all others are lost. I don't know if and when event is actually sent to Google after adding object to dataLayer and I think 75% of events were not send at all.

Now I'm trying to implement another approach:

//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)
    }
})

But ga('send') does not have any callback function again!

How do i get sure that event was actually sent to google, using dataLayer or ga('send')?

解决方案

Finally got it. It's pretty complicated and not described in docs. In my case I use Google Tag Manager, so there some workarounds I had to make to get successfully fire an event and get callback.

First, we have to get ClientId, which is required with any event sent to Google servers. Actually it's kept in cookies, but Google does not recommend to take it directly from there.

Here is how Google recommends to get it, but this will not work if you are using Google Tag Manager.

 ga(function(tracker) {
       var clientId = tracker.get('clientId');
 });

Instead, you have to get ClientId from getAll method.

 var clientId = ga.getAll()[0].get('clientId');

After, you have to create new tracker

    ga('create', 'UA-XXX-YYY', {
        'clientId': clientId
    });

And after that we can send an event:

 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
   }
});

这篇关于谷歌分析发送事件回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆