Kaushik
Kaushik

Reputation: 3381

Global objects of Google Analytics

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?

Upvotes: 3

Views: 3605

Answers (2)

Kaushik
Kaushik

Reputation: 3381

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

Upvotes: 5

Vikas
Vikas

Reputation: 250

please check this link -

https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced

According to this -

"When the snippet runs, it first creates an new global function named ga. Next, the snippet asynchronously loads the analytics.js library onto the page.

The ga global function is the main way you interact with the analytics.js library. "

and if you want to change,

"In some cases the ga variable name might already be used by an existing object on your page. To avoid overriding your existing object, you can rename the ga function, for example to __gaTracker."

so it is your code which can change the name of this global object and if you will change it and you obviously know what is the new name

Upvotes: -1

Related Questions