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

时间:2021-09-17 15:21:49

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.

我正在尝试在用户注册之后和重定向之前发送事件go google analytics。我正在使用Google跟踪代码管理器和univerasl js。

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

首先,我尝试使用dataLayer对象,如下所述: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.

麻烦的是,我收到了大约25%的所有活动,其他所有活动都丢失了。我不知道在向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 get sure that event was actually sent to google, using dataLayer or ga('send')?

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

3 个解决方案

#1


25  

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. Actually it's kept in cookies, but Google does not recommend to take it directly from there.

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

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

#2


7  

From Google Analytic doc https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#hitCallbackNews

来自Google Analytic doc https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#hitCallbackNews

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

#3


1  

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. Once the tracker has completed sending data, an alert box will be shown to the user.

在此示例中,字段名称对象配置页面参数,以及设置hitCallback。跟踪器完成发送数据后,将向用户显示一个警告框。

You can use hitCallback for events, page views etc..

您可以将hitCallback用于事件,页面查看等。

#1


25  

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. Actually it's kept in cookies, but Google does not recommend to take it directly from there.

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

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

#2


7  

From Google Analytic doc https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#hitCallbackNews

来自Google Analytic doc https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#hitCallbackNews

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

#3


1  

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. Once the tracker has completed sending data, an alert box will be shown to the user.

在此示例中,字段名称对象配置页面参数,以及设置hitCallback。跟踪器完成发送数据后,将向用户显示一个警告框。

You can use hitCallback for events, page views etc..

您可以将hitCallback用于事件,页面查看等。