A quick Google search later, I found some documentation and a script to dynamically add link tracking from Blast Analytics & Marketing. I liked the concept of the script, but (like every coder) there were a couple of things I wanted to change. While doing that, I thought of a couple more, and, the next thing I knew, I had several variations on a theme (all of which are available on GitHub).
To use, choose one of the files below and add it to the page/site you want to track via the standard
script element (near the foot of the document is probably best). As of 19 April 2013, it requires jQuery 1.7+ to run, but I’m working on a pure JS implementation.
This is probably the version most people will want to use. It tracks clicks as events for the following link types:
- External links (Different host than current site or marked
- File links (Executables, zip archives, PDFs, etc.)
- Email links (
- Telephone links (
Tracked clicks will show up in Google Analytics under “Content” > “Events” in the following ways:
- Event Category
- External, email and telephone links will all have the category “Link clicks.” File links will have the category “Downloads.”
- Event Action
- External, email and telephone links are all labeled as such. File links are labeled with the file extension plus “download.” For instance,
- Event Label
- External links are labeled with the URL of the link. File links are labeled with the file name. Email links are labeled with the email address of the link, and telephone links are labeled with the telephone number of the link.
This version is for people who want to inflate their pageviews! It’s very similar to
ga-click-tracking-events.js, except PDF and text documents are not tracked as events, they are tracked as pageviews. (Note: Google recommends using event tracking, not virtual pageviews.)
The event tracking is the same as above, but clicks on PDF and text documents will show up in Google Analytics under “Content” > “Site Content” as pages. The page path is artificial. It’s constructed from the file extension plus the file name, for instance
This is a stripped-down version of
ga-click-tracking-events.js that only tracks clicks on file links as events. This is the version for people who don’t care about tracking clicks on external, email and telephone links because, really? create some new content instead.