Google Analytics Integration
Friday, 01 August 2008 13:26

Most of you are familiar with Google Analytics, however some of you may not be. This Blog post is not intended to explain the features of Google Analytics, so I will be brief. It is much more than just a website statistic site. You can analyze everything from who is looking at your site, where they are from, and even how much time they are spending on each page. For a full list of features, please visit the Google Analytics – Features page.

Occasionally, I get asked, "How complicated is it to integrate Google Apps in to a website?" The truth is, it depends. If you have an "old school" website where each page is its own html page, then it would take some time. Google makes it easy to plug into their Analytics system by giving you a small piece of JavaScript called the "Tracking Code" that has to load on every page on your site. Since old websites have a separate html file for each page, you would have to add the Tracking Code to each html page.

Those days are long gone. Newer websites, not just Joomla!, usually have a file that is included into all pages. In Joomla!, we tend to use the template's index.php file to inject this code into. The reason is simple… Every time you click a link, the template is called to "paint the picture" of your new page. By adding the Tracking Code into the template's index.php file, you effectively add the Tracking Code to every page on your site.

Great, now that that is covered, let's talk about how to add it.

You will first need to get your "Tracking Code" from Google Anaytics. This Blog posts assumes that you already have an account and can find that page. Here is an example of what the Tracking Code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-9999999-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

The code above is identical for every website except for the " UA-9999999-1" part. The 9999999 part is a unique number that ties to your Google Analytics account. The "1" at the end says that this is the first website that you have tied to Google Analytics. If you add another site, that "1" would change to a "2" for that site. Easy enough? Great!

The first thing we need to do is find out template's index.php file. This is can be found in the following location: "templates//index.php". varies, obviously, depending on what template you are using.

Depending on your version of Joomla!, if you open up that file and scroll to the bottom, you will see something like:

Joomla! 1.5.x

<jdoc:include type="modules" name="debug" />
</body>
</html>

Joomla! 1.0.x

<?php mosLoadModules( 'debug', -1 );? >
</body>
</html>

You want to add your tracker code right above the closing body tag ("</body>"). The debug module may or may not be the line of code directly above the closing body tag. It depends on your template. The key here is to add the Tracking code just above the closing body tag.

Using the example Tracking Code above, the end of our template's index.php file would look like this:

<jdoc:include type="modules" name="debug" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-9999999-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</body>
</html>

After you have made the change, you can go back to Google Analytics and verify that it is working correctly. Note: you will not see actual data until the next day. Google Analytics processes its data nightly. Unfortunately, it is not in real time. Given the number of features and the price (free), I am will to accept batch processing.

Kenneth Crowder has been involved in the Joomla! Community since the days of Mambo. He has volunteered many hours to help out the Open Source Project. He is considered an expert in all things related to Joomla!. He was a Technical Reviewer for Joomla! A Users Guide, as well as another book currently in the editing phase. Learn more about Ken.

Trackback(0)
Comments (0)add comment

Write comment
smaller | bigger

security image
Write the displayed characters


busy