Best way to integrate Google Analytics

This is the best solution to integrate Google Analytics into your site. It uses ideas and code from the following 2 sites:

Here is the improved snippet:

<script type="text/javascript" src=""></script>
<script type="text/javascript">
    if (typeof(_gat)=='object')
            _gat._getTracker("UA-1234567-8")._trackPageview()}, 1500);


  1. Does not use document.write
  2. Fixes the “_gat is not defined” issue
  3. Delayed GA access (1.5 second) so the page load time is not affected
  4. Very concise

Another improvement would be to cache ga.js locally to avoid the DNS lookup.

Comments are closed.