I'm trying to log an event in MixPanel when users click a certain type of link. I'm using JQuery to do it unobtrusively and as far as I understand I need to add a callback function to take the user to 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 neither takes the user to the page nor logs the event, but I see no errors in the Javascript console in Chrome.
Any ideas what the problem may be?
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>
It now redirects to the correct link, but still doesn't log an event.
I'm trying to log an event in MixPanel when users click a certain type of link. I'm using JQuery to do it unobtrusively and as far as I understand I need to add a callback function to take the user to 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 neither takes the user to the page nor logs the event, but I see no errors in the Javascript console in Chrome.
Any ideas what the problem may be?
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>
It now redirects to the correct link, but still doesn't log an event.
Share Improve this question edited Dec 29, 2011 at 11:01 cutwithflourish asked Dec 28, 2011 at 14:27 cutwithflourishcutwithflourish 1211 gold badge1 silver badge8 bronze badges 9 | Show 4 more comments5 Answers
Reset to default 11Mixpanel has recently added a method mixpanel.track_links that does the job.
The third argument of the mpq.track function is a callback. This code gets executed once the tracking has completed, and would be a reliable way to send the user to the other page.
$("#more-posts").on("click", function(event) {
event.preventDefault();
mpq.track("More Posts", null, function() {
go_to_link($("#more-posts").attr("href"))
});
});
I believe this is a candidate for MixPanel support: [email protected]. The bug does not lie with your jQuery code. Here's a working jsFiddle that demonstrates the basic functionality.
As I mentioned, I've seen similar issues with a _kmq.push
with Kissmetrics. Their JS simply might not have time to register the event. If you try a longer timeout, it might work, but this is bad UX.
Update here if/when you reach out to MixPanel.
Instead of using setTimeout, consider using the callback param of mpq.track.
Alternatively, track the page load at the location that the link would have gone to.
The easiest way is to use the function callback in mixpanel.track which fires after the event is successfully logged.
Code would look like this.
mixpanel.track("Great Success!", {}, function(){
window.location.href = "www.whatever.com";
});
event
comes from? It should be an argument of the click callback. – Robin Commented Dec 28, 2011 at 14:37.on('click', function(event) {...}
. – Josh Smith Commented Dec 28, 2011 at 16:34setTimeout
before the redirect and see if it logs the event correctly. If it does, then this is your issue, and you likely want to log the click event after they reach the new location. – Josh Smith Commented Dec 28, 2011 at 16:36