How to Track Downloads & Outbound Links in Google Analytics
It has been almost 2 years since we published our wildly popular post, How to Track Downloads in Google Analytics. Since Google Analytics does not track file downloads, email, telephone or other outbound link clicks automatically, we saw an opportunity to provide this code to the community. In our original post, we provided some explanations and code examples for tracking file downloads in Google Analytics using event tracking.
We decided to revisit our original post and provide a valuable code update that improves performance, makes tracking downloads more manageable and easier to extend.
Dynamically Track Downloads & Other External Links
We have updated the code to make it more manageable and easier to extend. In addition, we are using the jQuery on() method for attaching the click event handler to links. Since we are using the on() method you will need to use jQuery v1.7+. If you are using an earlier version of jQuery the .live() method can be used instead.
Again, feel free to customize this code to suite your needs. It can be placed in its own .js file and should be placed in the <head> of your pages. This script automates the following:
- Tracks file downloads as events for the following extensions: .zip, .exe, dmg, .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .mp3, .txt, rar, wma, mov, avi, wmv, flv, wav (again feel free to modify the list)
- Tracks outbound clicks as events if the href value contains http:// or https:// and the domain value doesn’t match the current domain
- Tracks mailto email clicks
- Tracks Tel telephone clicks
The script sets download, email and tel link clicks as interaction events while the external site clicks are non-interaction which can be adjusted if desired.
As before, the script will detect if the link is opening in a new window or not and automatically uses setTimeout() for 400ms if you are not. This is to allow time for the hit request to process before taking the user to the new page.
Inline Download & Other External Links Tracking
The original syntax for adding the code inline remains the same.
For links that open a new window or tab (such as using target=”_blank” for example) you will want to use the code below:
In current window
For links that open within the same window, replacing the current page, you will want to use the code below:
These type of links require a slight delay to allow time for the hit request to process before taking the user to the new page.
Detailed Download & External Link Reports
After all of your hard work you will have event tracking reports with neatly organized data around file downloads, external link, email link, and telephone link clicks. From these reports you can gauge the usage and usefulness of your file downloads and various links.
Lastly, since events are associated with the page they were fired on, you are able to apply a secondary dimension of page to find out which page contained the download or external link click that occurred.
If you need assistance with outbound link analysis, setup of this tracking code or any other analytics help, don’t hesitate to comment below or contact our Google Analytics Consulting Experts.