In Google Analytics, there are some global objects:-
For ga.js:-
Which object guarantees that ga.js is being used?
For analytics.js:-
Since, the name of global object can be changed, how can we get the actual global object in analytics.js?
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)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With