I just finished working on a plugin for Sketch and I created a simple landing page for users to download the plugin. I want to use Google Analytics event tracking to track the downloads, but the event tracking is not working and I can't seem to figure out why.
Here is what the link looks like:
<a href="downloads/colorspark.zip" download onClick="ga('send', 'event', 'Downloads', 'download', 'ColorSpark for Sketch');">Download</a>
Does anyone see what I'm doing wrong? Do I need to add any other code anywhere else besides the onclick attribute?
My bet is that you're facing what we call a
race condition
: the moment the user clicks the link, the browser initiates a page change, thus GA is interrupted before it's had a chance to send the event.2 options
target="_blank"
to your links so they open in a new tab and don't interrupt GA in the current tab.onClick
that will prevent the link from opening by default (return false;
), trigger the GA event, and use GA'shitCallback
to trigger the page change programatically.For option 2 there are different ways of doing it (since it's custom code). Here is an example from Google: https://support.google.com/analytics/answer/1136920?hl=en