Google Analytics send event callback function

前端 未结 3 894
面向向阳花
面向向阳花 2020-12-08 14:57

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.

3条回答
  •  無奈伤痛
    2020-12-08 15:26

    From 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.

    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.

    For much understanding api of analytic, fyr: https://developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference

提交回复
热议问题