Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global objects of Google Analytics

In Google Analytics, there are some global objects:-

For ga.js:-

  • gaq
  • gat
  • pageTracker

Which object guarantees that ga.js is being used?

For analytics.js:-

  • ga (can be changed)

Since, the name of global object can be changed, how can we get the actual global object in analytics.js?

like image 847
Kaushik Avatar asked Nov 18 '25 11:11

Kaushik


1 Answers

For ga.js,

The _gat global object is used to create and retrieve tracker objects, from which all other methods are invoked.

For analytics.js:-

Google Analytics global object can be accessed by window['GoogleAnalyticsObject'];. This will give us the global variable name. To again get the object for that variable we can convert the variable name into object by: window[window['GoogleAnalyticsObject']]

So, in case we want to get the clientId:-

var gaObj = window['GoogleAnalyticsObject'];
window[gaObj](function(tracker) {
    var clientId = tracker.get('clientId');
    console.log(clientId)
});
like image 184
Kaushik Avatar answered Nov 21 '25 01:11

Kaushik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!