How do I get the global name of a Google Analytics function?

Hello to all Google Analytics Rockstars superstars,

How do I get the global Google Analytics function name when renamed from an install script.

For example:

 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','__gaTracker');

      

I want to get the function name "__gaTracker" that has been renamed (or whatever).

By simply setting the context, we have a script that runs on client websites and sends events to Google Analytics. Sometimes they have a "ga" function and we call ga('send'...)

, but if they rename it we need to call (for example) __gaTracker('send'...)

.

Is there a way to get the global function that has been renamed.

Thanks in advance!!!!

+3


source to share


3 answers


It can be accessed via window['GoogleAnalyticsObject'];

, it will give you the name of the variable GLOBAL

. Hope this is what you asked for.

just execute F12and paste your code here in the console, you will see the global SO function name,



console.log(window['GoogleAnalyticsObject'];) // ouputs ga

      

+2


source


Google recommends using the "GoogleAnalyticsObject" and "ga" variables for GA tracking searches:

// Β© Google Inc.
// This function works even if the site  
// has customized the ga global identifier. 
var ga = window[window['GoogleAnalyticsObject'] || 'ga'];

      



Full example at developers.google.com

+3


source


There is no way to return the string "ga" (in your case). If so, you should customize the initiation of GoogleAnalyticsObject with the new default loader. Mr. Arniban N. is right.

+1


source







All Articles