Download Google Analytics from jQuery doc?

The problem is that it sometimes takes a while to load, and my jQuery ready () functions do not run until they are executed. I would like to move the GA code itself to the end of the ready () function. I'm not looking for additional click tracking integration - I just want my ready () scripts to run first.

My questions are: 1) Will moving the GA code disrupt their tracking statistics in any way? And also, 2) Do I need to emulate the use of two script tags (one of which generates an external script tag and one that calls the function)? If so, why and what is the best way to do this in jQuery function?

To explain # 2, here's the GA code that now immediately precedes the closing body tag:

<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-8704511-1"); pageTracker._trackPageview(); } catch(err) {}</script> 
+6
jquery google-analytics
source share
3 answers

A similar question here:

  • How to dynamically load Google Analytics (JavaScript)?

And the accepted answer is here:

+5
source share

It looks like Google has also addressed this issue - see this later stack overflow question

+3
source share

The reason for having two script blocks is because the script in the first block inserts a link to the Google Analytics javascript file between the two blocks. If everything was in one block, the tracker would try to initialize before it was loaded. So, No. 2 is definitely true, you will need to either emulate two script tags.

+1
source share

All Articles