Using Google Analytics


There are quite a few options when trying to report statistics on your page views. Of all of the options we have used, custom reports from the raw logs via grep, AWStats (which is a great open source resource for basics stats), but we are really falling for the free Google Analytics. We have seen botched installations of Google Analytics so we wanted to make a few tips absolutely clear.

First and foremost, take your time. Google Analytics installation will take about 60 seconds, so spend your time reading and listening to the video tour. Everything we are about to tell you is in the documentation, however. we have seen several complaints about the free system all related to not following the instructions. If you have created your website with a few basic tips that we also deploy our websites with, it can’t get any easier.

The basic tip we are speaking with that will help you with your Google Analytics statistics is to create a footer include for your site. Header, menu, and footer includes allow you to make changes in one location that is automatically updating your entire site. If you pages are written in php this is easily done with the include instruction. If you are using straight html, do a javascript include. Either way this makes the rest of the Google Analytics installation very easily.

First, signup for Google Analytics by visiting Google. If you already have a Google Webmasters account, click on My Account and signup for Google Analytics under the same username. Upon signing up it will ask you to submit the domain name you wish to gather data for. You will then be supplied with code to enter on each page you wish to gather data for. The biggest mistake that we see turns into our number one tip.

Google Analytics requires code to be inserted on each page you wish to gather statistics for. This code is a javascript insert that is hitting Google servers in order to calculate hits for your site. As you can imagine millions of pages are hitting this cluster of servers every second of every day. Would you think from time to time it might slow things down a bit. Sure! This is where the complaints come in. However, if you read the documentation carefully, and in turn install the code appropriately, it shouldn’t impact your site.

The Google Analytics documentation specifies that you instal the code directly above the end body tag or within the footer of your website (which should be very close to the tag). What this means for your website is that it will load as usual. When website owners have asked us to take a look at the slowness Google Analytics caused, they had all put the cody directly after the opening body tag .

The issue that arises here is that if Google servers are slow at any time, your page will halt waiting for their code to do their piece. If it is installed correctly, your page will load, and if Google causes any slowness it will only affect the footer of your webpage. This impacts your site very minimally as the user will see the bulk of your site and only be delayed in getting the “Done” comment at the bottom of the browser and seeing the very bottom of your website. So just make sure the Google Analytics code is below your main content and all will be well.

For all of those that are deploying Google Analytics on pages that sometimes are accessible via SSL you will need to make changes to your Google code as well. Rather than them pointing you to http://www.google-analytics.com/urchin.js, you need to change this to https://ssl.google-analytics.com/urchin.js – so note the two changes, http to https and www to ssl. Make these simple changes and you will not get error messages from your browser.

If you use an include file or footer file that is at the end of all of your pages this presents another issue. You don’t always want to hit Google’s SSL page for all of your pages. So here is a suggestion. Assuming you use something like PHP for your includes, create a IF THEN ELSE statement around your Google Analytic code. The IF THEN ELSE statement will look for the server variable for HTTPS, in PHP 5 it is $_SERVER['HTTPS']==’on’.

Therefore, you end up with something like this:
(make sure you replace the XXX’s with your UA code number)

<? if ($_SERVER['HTTPS']==’on’) { ?>

<script src=”https://ssl.google-analytics.com/urchin.js” type=”text/javascript”>

</script>

<script type=”text/javascript”>

_uacct = “UA-XXXXXXX-X”;

urchinTracker();

</script>

<? } else { ?>

<script src=”http://www.google-analytics.com/urchin.js” type=”text/javascript”>

</script>

<script type=”text/javascript”>

_uacct = “UA-XXXXXXX-X”;

urchinTracker();

</script>

<? } ?>

Update: Google has since updated their code and fixed this issue!

  • Advertisement

    Free PositiveSSL and WhoisGuard with Every Domain Purchase