How to track clicks on external links and count file downloads in Google Analytics

To follow up on what I wrote yesterday (Tracking external links in Google Analytics and browser behaviour) about how event.Preventdefault can break things I thought I'd share what I was working on when discovering that.

The code below makes Google Analytics track the following events:

  • Clicks on external links
  • Clicks on internal files
  • Clicks on external files

To be clear this code is based on the work of Ralph Slooten. Hats off to him! I'd also like to thank Joakim Stai for providing some code review.

Loading comments…
All rights reserved