How can I register a MixPanel event when a user clicks a link?

I am trying to register an event in MixPanel when users click on a specific type of link. I use jQuery to do this unobtrusively, and as far as I understand, I need to add a callback function to bring the user to the URL after the event has been logged.

This is the code I'm using:

<script type="text/javascript"> $("#more-posts").click(function() { event.preventDefault(); mpq.track("More Posts", function(){ window.location = $(this).attr("href"); }); }); </script> 

Unfortunately, this does not lead the user to the page and does not register the event, but I do not see errors in the Javascript console in Chrome.

Any ideas what might be the problem?

Update: also tried this code based on suggestions in the comments:

 <script type="text/javascript"> function go_to_link(link) { window.location = link; } $("#more-posts").on("click", function(event) { event.preventDefault(); mpq.track("More Posts"); setTimeout("go_to_link($("#more-posts").attr("href"))", 2000); }); </script> 

Now it is redirected to the correct link, but does not register the event.

+6
source share
5 answers

Mixpanel recently added a mixpanel.track_links method that does the job.

+11
source

The third argument to mpq.track is the callback. This code runs after tracking is complete and will be a reliable way to send the user to another page.

 $("#more-posts").on("click", function(event) { event.preventDefault(); mpq.track("More Posts", null, function() { go_to_link($("#more-posts").attr("href")) }); }); 
+3
source

I believe this is a candidate for MixPanel support: support@mixpanel.com. error is not related to your jQuery code. Here's a working jsFiddle demonstrating basic features.

As I mentioned, I saw similar issues with _kmq.push with Kissmetrics. Their JS just does not have time to register the event. If you try to use a longer timeout, this may work, but it is bad UX .

Update here if / when you go to MixPanel.

+2
source

Instead of using setTimeout, consider using the mpq.track callback parameter.

Alternatively, keep track of the page loading where the link went.

+1
source

The easiest way is to use the callback function in mixpanel.track, which is run after the event completes successfully.

The code will look as follows.

 mixpanel.track("Great Success!", {}, function(){ window.location.href = "www.whatever.com"; }); 
0
source

Source: https://habr.com/ru/post/904602/


All Articles