Monday, May 7, 2012

Testing Google Analytics locally


When you're first setting up Google Analytics for your website, you'll want to do some testing in your local or test environment. The good news is: testing of the transmission is fast. The bad news is: testing the processing is not. This post describes the process.

Here's what you need:
  1. A google analytics account. It's free, no big deal.
  2. A profile. The default one will do fine. In profile creation, when it asks for your Website's URL, go ahead and enter the real, production domain name. It doesn't matter.
  3. Tracking Code: the javascript to include in your pages. It looks a lot like this, but I've changed it in three places:
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-000000000-0']);
  _gaq.push(['_setDomainName', 'none']);

  _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/u/ga_debug.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

UA-000000000-0 is your Google Analytics account identifier. When you copy and paste the code from Google that'll be populated with real numbers, just for you.

_gaq.push(['_setDomainName', 'none']); is the magic make-it-work flag for local testing. This says, ignore the domain name in the URL and just post the data.

u/ga_debug.js is where the real work happens; in real life (production) this will say ga.js. For testing, use the debug version of the script. Notice the extra /u in front of the script name; the debug script is in a slightly different spot.
  1. Trigger some page loads in your local environment with that javascript included in the page.
The debug script will print useful information to the console every time information is transmitted to Google. Look for this in the debug console (command-alt-I in Chrome or Safari on a Mac):

Track Pageview
ga_debug.js:24Tracking beacon sent! utmwv=5.3.0d&utms=1&utmn=1124665081&utmhn=localhost&utmcs=UTF-8&utmsr=1920x1080&utmvp=1469x795&utmsc=24-bit&utmul=en-us&utmje=1&utmfl=11.2%20r202&utmdt=&utmhid=548350195&utmr=0&utmp=%2F&utmac=UA-31368436-1&utmcc=__utma%3D1.558561847.1335972658.1336071437.1336081900.9%3B%2B__utmz%3D1.1335972658.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=qB~
ga_debug.js:24Account ID : UA-000000000-1 Page Title : Your Title Host Name : localhost Page : /pathFromContextRoot/pageOfInterest?anyQueryParameters=stuff Referring URL : 0 Hit ID : 548350195 Visitor ID : 558561847 Session Count : 9 Session Time - First : Wed May 02 2012 10:30:58 GMT-0500 (CDT) Session Time - Last : Thu May 03 2012 13:57:17 GMT-0500 (CDT) Session Time - Current : Thu May 03 2012 16:51:40 GMT-0500 (CDT) Campaign Time : Wed May 02 2012 10:30:58 GMT-0500 (CDT) Campaign Session : 1 Campaign Count : 1 Campaign Source : (direct) Campaign Medium : (none); Campaign Name : (direct) Language : en-us Encoding : UTF-8 Flash Version : 11.2 r202 Java Enabled : true Screen Resolution : 1920x1080 Color Depth : 24-bit Ga.js Version : 5.3.0d Cachebuster : 1124665081

  1. Check Google Analytics Real-Time to see whether your visit has been recognized. (Click on Home in the orange header, then Real-Time on the left, then Overview.) This tells you that Google Analytics is receiving data.
  2. Now comes the hard part: waiting. Seriously, you have to wait a whole day to see the page views you generated. Then you can play around with the reporting, filtering, site search config, etc etc. While the data transmitted is received and recorded immediately, it is not processed for three hours or so, and more processing occurs overnight.
Keep in mind that when you change Google Analytics configuration (like site search query parameters), that affects only records that have not been processed. Once data is processed and stored, it doesn't get re-processed with updated rules. Testing configuration means make a change; generate the relevant traffic; come back tomorrow.

9 comments:

  1. This Post has been incredibly helpful to me today. Thank you very much.

    ReplyDelete
  2. This is incredible, awesome article. Thanks

    ReplyDelete
  3. Awesome article. Thanks for posting this.

    ReplyDelete
  4. Just Owao! Very nice & helpfull post! Thank you!

    ReplyDelete
  5. This information was surprisingly hard to find, but this really saved the day for me. Thanks.

    ReplyDelete