Combining JavaScript for Google Analytics with yours. (Asynchronous tracking.)

Posted by lorenzo 72 on Stack Overflow See other posts from Stack Overflow or by lorenzo 72
Published on 2010-03-08T12:42:38Z Indexed on 2010/03/08 14:51 UTC
Read the original article Hit count: 199

I have a JavaScript file which is loaded up at the end of my HTML page.

Rather than adding the script code for asynchronous tracking for Google in yet another script I would rather combine the two scripts together.

So instead of this:

    <html>
        ...

        <script src="myScript.js">

        <!-- google analytics -->
        <script type="text/javascript">

            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-XXXXX-X']);
            _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';
                (document.getElementsByTagName('head')[0] ||
                 document.getElementsByTagName('body')[0]).appendChild(ga);
            })();
        </script>
    </html>

I would have that bit of code in the second script tag at the end of my 'myScript.js'.

I have not found one place in google documentation where it suggests to combine the script with yours.

© Stack Overflow or respective owner

Related posts about google

Related posts about google-analytics