Event Tracking + Redirect


#1

Dear all,

I have a site that displays a set of rotating banners on the home page which are internally linked to other pages within the same site. I would like to track how many times each banner was clicked. So far, my solution is to use Event Tracking but my problem is with the redirects. Once the banner is clicked, I fire off the trackEvent, but I haven’t figured out how to “wait” until the tracking of the event is complete before issuing the redirect.


$("img.banner").click(function() {
     var id = $(this).attr('data-banner-id');
     _paq.push(['trackEvent', 'Banner', 'Click', id]);
     // there's a race condition between the tracking of the event
     // and the browser executing the following redirect
     var url = $(this).attr("data-url");
     window.location.href = url;
});

It appears there is a race condition between trackEvent and the redirect. I also tried ‘pushing’ a function into _paq but the results are the same. I simply cannot figure out a way to synchronously track the event, and then issue the redirect.

Am I missing something? Perhaps I am approaching the problem wrong?

Any help is greatly appreciated!

–mgb


(Matthieu Aubry) #2

The easiest way to do it is to wait like 500ms before redirecting user to banner.
This is how Piwik itself tracks clicks on links and download, see: matomo/piwik.js at master · matomo-org/matomo · GitHub

Btw we really want to make tracking banner Ads easier. Here is the feature request: Content Tracking, Banner tracking: track impressions, Clicks on any piece of content · Issue #4996 · matomo-org/matomo · GitHub

feel free to add your voice there!


#3

Hey Matt,

Thanks for your reply!

I had also thought of this. Even though I successfully hacked a ‘synchronous’ trackEvent2 function, I opted for this delay soltuion as I didn’t like the idea of maintaining my own fork of piwik.js :slight_smile: Anyways, it’s now (sufficiently) working as desired.

Regarding generic content tracking, I think this would be very useful to a lot of folks (myself included). I’d be more than happy to help out wherever I can, whenever I have the free time.

Cheers!
Matt