Dec 6, 2009

Download counter and iWEB

Do you need to count downloads or something like that on your WEB page?
You don't want to write it yourself, right?
Do you use iWEB to maintain you web site?
If all answers are YES, than I think I have a solution for you.

Let's start from the download counter. Google Analytics is a great product of Google. It is a mandatory tool, I guess, for all who have web sites. Recently I found another great ability of the tool. It can track and log events! This ultimate ability we can use to count downloads, for example, on our sites or any other events. Chip and easy solution!

Now, if you use iWEB 08 or 09, than you would need a bit of voodoo here.
Let's say, you have a page and you want to have a download button, triggering which user gets a file to download.
Use "insert HTML Snippet" in iWEB to add a required code block. Important to have your analytics code inserted before the events tracking code. I used the same HTML snippets block for both analytics and events tracking code.

!!!IMPORTANT!!! You would need to modify a bit your analytics code. Namely, you need to add "pageTracker._initData();" after "pageTracker._trackPageview();", otherwise you get errors in the events tracking code.

Here we have a code snippets for that:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXX-XXX");
} catch(err) {}

the following is the download button, which I want to be tracked and I do that by calling my tracking code on OnClick event:
<input type="button" name="Download" style="height: 1.5em; width: 14em"  value="source (22.8MB)" onClick="pageTracker._trackEvent( 'MyProject', 'Download', 'v1.1.2');'', 'download', config='height=200,width=200' );">

Check the events tracking documentation for all details related to event tracking machinery of Google Analytics.