Google Analytics new asynchronous tracking code

Google Analytics has now provides new asynchronous tracking code. The main feature of the asynchronous code is that it could load ga.js while the page is being rendered.

There are two ways to use this code.

  1. Place the code below before the  </head> tag.
    <script type="text/javascript">
    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXX-X']);
      _gaq.push(['_trackPageview']);
    
      (function() {
        var ga = document.createElement('script');
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        ga.setAttribute('async', 'true');
        document.documentElement.firstChild.appendChild(ga);
      })();
    
    </script>
  2. Split the code. Half of the code will be put before the </head> tag. Another half will be put at the end of the page, before the </body> tag.The first half (before </head> the tag).
    <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-X']);
    _gaq.push(['_trackPageview']);
    </script>

    The second half (before </body> the tag).

    <script type="text/javascript">
        (function() {
          var ga = document.createElement('script');
          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
          ga.setAttribute('async', 'true');
          document.documentElement.firstChild.appendChild(ga);
        })();
    </script>

Note:

  1. Remove any existing Google Analytics code before using this asynchronous code.
  2. Disable any Google Analytics plugin (if you use one) for WordPress.
  3. Replace UA-XXXXX-X with your web property ID.
If you are new here, you might want to subscribe to the RSS feed or newsletter.

Enter your email address:

Related

This entry was posted in Blog Tips, WordPress and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>