Google Analytics 发送事件回调函数 [英] Google Analytics send event callback function

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

问题描述

我正在尝试在用户注册后和重定向之前向 Google Analytics(分析)发送事件.我正在使用 Google 标签管理器和通用 js.

I'm trying to send an event to Google Analytics after a user is registered and before he's redirected. I'm using Google Tag Manager and universal js.

首先,我尝试使用 dataLayer 对象,如下所述:developers.google

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

这是我的函数的样子:

//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%,所有其他事件都丢失了.我不知道在向 dataLayer 添加对象后事件是否以及何时真正发送到 Google,我认为 75% 的事件根本没有发送.

The trouble is that I was receiving just about 25% of all events, all others are lost. I don't know if and when events are actually sent to Google after adding objects to the 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)
    }
})

但是ga('send')又没有回调函数了!

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

如何使用 dataLayer 或 ga('send') 确保事件确实发送到 Google?

推荐答案

终于搞定了.它非常复杂,文档中没有描述.就我而言,我使用 Google 标签管理器,因此我必须采取一些解决方法才能成功触发事件并获得回调.

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.

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

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.

以下是 Google 推荐的获取方式,但如果您使用的是 Google 跟踪代码管理器,此方法将不起作用.

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

相反,您必须从 getAll 方法中获取 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
   }
});

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

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