views:

63

answers:

1

I have a website where I have .tar.gz, .zip and .dmg files. I need to track the number of downloads using google analytics. I heard that I can use onclick="pageTracker._trackPageview('/file_name.file_extension') for the "a" tags on the page. I'm not clear what the file_name.file_extension corresponds to. Also, do I need to use some additional scripts?

+3  A: 

I'm not clear what the file_name.file_extension corresponds to.

Your example onclick="pageTracker._trackPageview('/file_name.file_extension') logs every click on that link as a page view for file_name.file_extension.

You can edit file_name.file_extension to be whatever you want. It is simply the name of the "page view" that gets passed to Google Analytics and is what will show up in your analytics reports.

Also, do I need to use some additional scripts?

No, adding the above onlick attribute to each link you want tracked will be enough.

See here for reference. Hope that helps.

Edit:

I assumed you knew you needed the general Google Analytics script for this to work (Thanks to Ryan in the comments for clarifying). The script looks like the following, but contains your Google Analytics account number in place of the X's in UA-XXXXXX-X:

<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">
try{
var pageTracker = _gat._getTracker("UA-XXXXXX-X");
pageTracker._trackPageview();
} catch(err) {}
</script>

To obtain the script, you'll need a Google Analytics account. Once signed into your account and after adding a new "Website Profile", you'll be given a snippet of Javascript (using your account number) to include in each page you want tracked, along with instructions. That should be enough to get you started, but let me know if I can clarify anything.

Edit 2:

As pointed out in the comments, I erroneously posted the latest, asynchronous version of the Google Analytics script which is actually incompatible with _trackPageview. I've edited my answer to include the "traditional" script that you'll want to use. See here for more info.

Colin
To be clear, you will need the Google Analytics script, and the code to initialize that, but beyond what Google gives you, there is no need for extra scripts.
Ryan Kinal
Thanks for the clarification Ryan. Could you please tell me where I can find the Google Analytics script and the code to initialize it?
Anonymous
@Anonymous: See my edit above.
Colin
Worth clarifying that the code snippet you've given is the async code, while the '_trackPageview' code you're using (and the question uses) is the regular ga.js code, and are incompatible.
yc
@yc: Good catch - should be fixed now. Thanks.
Colin