简体   繁体   English

Google Analytics 发送事件回调函数

[英]Google Analytics send event callback function

I'm trying to send an event to Google Analytics after a user is registered and before he's redirected.我正在尝试在用户注册之后和重定向之前向 Google Analytics 发送一个事件。 I'm using Google Tag Manager and universal js.我正在使用 Google 标签管理器和通用 js。

First, I was trying to use the dataLayer object, as described here: developers.google首先,我试图使用dataLayer对象,如下所述: developers.google

Here'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 .问题是我只收到了所有事件的 25% 左右,其他所有事件都丢失了 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.我不知道在将对象添加到 dataLayer 后事件是否以及何时实际发送到 Google,我认为 75% 的事件根本没有发送。

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!但是ga('send')没有任何回调函数了!

How do I make sure that an event was actually sent to Google, using the dataLayer or ga('send')?如何使用 dataLayer 或ga('send')?确保事件实际上已发送到 Google 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.就我而言,我使用 Google 标签管理器,因此我必须采取一些解决方法才能成功触发事件并获得回调。

First, we have to get ClientId , which is required with any event sent to Google servers.首先,我们必须获得ClientId ,这是发送到 Google 服务器的任何事件所必需的。 Actually it's kept in cookies, but Google does not recommend to take it directly from there.实际上它保存在 cookie 中,但 Google 不建议直接从那里获取它。

Here is how Google recommends to get it, but this will not work if you are using Google Tag Manager.以下是 Google 推荐的获取方式,但如果您使用的是 Google 标签管理器,这将不起作用

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

Instead, you have to get ClientId from getAll method.相反,您必须从 getAll 方法获取 ClientId。

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

From Google Analytic doc https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#hitCallback News来自谷歌分析文档https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#hitCallback新闻

// Alerts the user when a hit is sent.
ga('send', 'pageview', {
  'hitCallback': function() {
    alert('hit sent');
  }
});

You can edit the hitCallback function for yours.您可以为自己编辑hitCallback函数。

OR或者

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

Here you can define your function ( criticalCode ) in the above example that can ensure the data sent to Google Analytic and then work with your code.在这里,您可以在上面的示例中定义您的函数 ( criticalCode ),以确保将数据发送到 Google Analytic,然后使用您的代码。

For much understanding api of analytic, fyr: https://developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference对于分析的更多理解api,fyr: https : //developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference

From the docs: https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#hitCallback来自文档: https : //developers.google.com/analytics/devguides/collection/analyticsjs/advanced#hitCallback

ga('send', 'pageview', {
  'page': '/my-new-page',
  'hitCallback': function() {
  alert('analytics.js done sending data');
}
});

In this example, the field name object configures both the page parameter, as well as setting the hitCallback.在此示例中,字段名称对象配置页面参数以及设置 hitCallback。 Once the tracker has completed sending data, an alert box will be shown to the user.跟踪器完成发送数据后,将向用户显示警告框。

You can use hitCallback for events, page views etc..您可以将 hitCallback 用于事件、页面浏览等。

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

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