load - Can Google Analytics be loaded via AJAX? -



load - Can Google Analytics be loaded via AJAX? -

the typical google analytics code (or rather - 1 told add) looks this:

<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setaccount', 'ua-123456-7']); _gaq.push(['_setdomainname', '.mydomain.example']); _gaq.push(['_trackpageview']); (function() { var ga = document.createelement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getelementsbytagname('script')[0]; s.parentnode.insertbefore(ga, s); })(); </script>

note ga.async = true; part. allows conformant browsers load script asynchronously, there still plenty of browsers around don't. i'm worried performance - i've seen many pages freeze in loading while trying download google analytics script. work if load script via ajax instead , eval() it? i'd rather have page view not tracked page view not load.

you can save script in javascript file , load file after document.ready event fired. way not block loading of page. how include non-essential js , css files (like jquery plugins, ui etc).

assuming using jquery, can this:

<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setaccount', 'ua-123456-7']); _gaq.push(['_setdomainname', '.mydomain.example']); _gaq.push(['_trackpageview']); (function() { var ga = document.createelement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getelementsbytagname('script')[0]; $(document).ready(function(){ s.parentnode.insertbefore(ga, s); }); })(); </script>

note: i've not tested it. i'm inserting script element, after document.ready called.

ajax load google-analytics

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -