Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Google Analytics global function name?

Hi all super google analytics rockstars,

How can I get the google analytics global function name, when it's renamed from the install script.

E.g.:

 (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 "__gaTracker" function name that was renamed (or something else).

Just setting the context, we have a script that runs on client's websites and sends events to google analytics. Sometimes they have the "ga" function and we call ga('send'...) but if they rename it, we need to call (e.g.) __gaTracker('send'...).

Is there any way to get the global function that was renamed.

Thanks in advance !!!!

like image 750
LFC Avatar asked Feb 09 '23 16:02

LFC


1 Answers

Google recommends to use both "GoogleAnalyticsObject" and "ga" variables for GA tracker lookup:

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

Full example on developers.google.com

like image 83
Vlad Mysla Avatar answered Feb 12 '23 10:02

Vlad Mysla