Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics gtag.js ready callback

I try to update my Google Analytics implementation from analytics.js to the new gtag.js.

In the old implementation I am using the ready callback function.

ga(function() {
    console.log('Google analytics is ready'); 
});

How can I implement a ready callback in the new gtag.js? I can't find any information in Google's documentation.

like image 361
xela84 Avatar asked Dec 07 '17 12:12

xela84


People also ask

What is the difference between GTAG js and analytics js?

Unlike analytics. js, gtag. js doesn't use trackers to send data to Google Analytics. It sends data to Google Analytics properties identified by their IDs set by the config command.

How do you fix GTAG is not defined?

The error is because you have not implemented gtag on your website. Please check the option 'Add Global Site Tracking Code gtag. js' while configuring the plugin then this error would go away(Screenshot for reference).

Should I use GTAG js?

Do not add the Google tag (gtag. js) in Google Tag Manager via custom HTML or a template as it may not work as intended. If you want to automatically Set up conversions with URLs, you will need to install the gtag. js snippet directly on your website.

Can you use GTAG with Universal Analytics?

The property ID on this line is the "controller" of the gtag. js snippet. If a Universal Analytics property ID ("UA-XXXXXXXX") controls the tag, you'll be able to use connected site tags to send measurement data to a Google Analytics 4 property, without having to add any new code to the page.


1 Answers

The command event supports the parameter event_callback, a function called when processing has completed. So, compared to the old analytics.js, you need to send an event to trigger the callback.

For example, you can use the page_view event; however, to avoid duplicates, you must disable the automatic page_view event by setting send_page_view to false.

gtag('config', GA_TRACKING_ID, {
  'send_page_view': false
});
gtag('event', 'page_view', {
  'event_callback': function() {
    console.log('Google Analytics is ready'); 
  }
});
like image 61
Benoit Blanchon Avatar answered Sep 17 '22 12:09

Benoit Blanchon