javascript - How to tell if Google Analytics is loaded -
i make lot of javascript widgets. part of these widgets, use google analytics track actions within widget. simplified, is:
var setupga = function(){ window._gaq.push(['_setaccount', 'ua-###']); }; if(window._gaq){ setupga(); } else { this.loadscript(google_analytics_path, function(){ var waitga = setinterval(function(){ if(window._gaq){ clearinterval(waitga); setupga(); } }, 500); }); } where google_analytics_path local path file , loadscript custom method load script , execute callback.
with google updating analytics library (now analytics.js), old methods of ensuring analytics library has been loaded no longer work. example code google analytics helpfully provides global ga object, object can have custom name. old queue check, i'm wondering how best can check existance of either analytic.js or ga.js versions of google analytics (i can skip old urchin tracking types).
it must like:
var setupga = function(){ window._gaq.push(['_setaccount', 'ua-###']); }; if(window._gaq && !window.**ga**){ setupga(); } else { this.loadscript(google_analytics_path, function(){ var waitga = setinterval(function(){ //_gaq loaded if(window._gaq){ clearinterval(waitga); setupga(); } }, 500); }); } where ga whatever global element can check. suppose thats i'm looking for
answered own question little more exploration.
with new universal analytics platform, seems window level variable created. variable called googleanalyticsobject. object give variable name created house universal analytics object
ex: using (window,document,'script','//www.google-analytics.com/analytics.js','ga'); return "ga" ex: using (window,document,'script','//www.google-analytics.com/analytics.js','notga'); return "notga" you can use variable pull analytics object:
window[window.googleanalyticsobject]
Comments
Post a Comment