Google Analytics Event Tracking using jQuery Event API v1.3 not getting any response data back after clicking download link

StackOverflow https://stackoverflow.com/questions/19811776

  •  04-07-2022
  •  | 
  •  

Question

I'm using the Google Analytics Event tracking as per: - https://developers.google.com/analytics/devguides/collection/analyticsjs/events.

The code i've used in my app is as follows: -

$('.gaDownload').on('click', function() {
        ga('send', 'event', 'link', 'click', $(this).attr('data-download'));
    })

Example of my link is as follows:

<a href="myresource.pdf" class="gaDownload" data-download="my resource name">Download</a>

When I click on the download link on the front end page the network panel seems to show that it all works but I don't get any data response, should I be?

The underlying problem is that when I check my GA account not all of the clicks are being captured but it's not easy to debug what's happening as I'm not getting any response back from google analytics when the request is made.

Was it helpful?

Solution

I've noticed that the requests are appearing red in Chrome's dev tools. That can happen if the the server responds with a non 200 code, or if the request was cancelled by the browser.

Because there is no response at all, I would think the request is being cancelled, which is likely being caused because the link is navigating away from the current page, causing all outstanding google analytics ajax requests to be cancelled.

This would potentially explain any intermittent behavior, if the google analytics responds before the browser starts handling download, you will receive a response. If the download starts, the analytics request will be stopped by the browser.

It would be interesting to make the files open in a new window/tab and see if the issue persists.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top