Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I log a MixPanel event when a user clicks a link?

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.

like image 236
cutwithflourish Avatar asked Dec 28 '11 14:12

cutwithflourish


People also ask

How do I track a link on Mixpanel?

It would look something like this: mixpanel. track_links("#MyLink","Clicked Link"); The first argument is a css-style selector for the link you want to track, the second argument is the name of the event.

How do I track a user on Mixpanel?

mixpanel. track(“Sign-up Page View”, { page: “email screen” }); Essentially you will only be sending in one event named “Sign-up Page View” but you can then expand the event by the event property “page”, where you can see what page your users where looking at while they signed up. Hope this helps!

How do I view events on Mixpanel?

Checking events To view more events, you can click on the "Load More" button at the bottom of the page. "Load More" will fetch the next 100 most recent events timestamped in the 30 days prior to the previously fetched events. Once the new events show up on "Events", they will be queryable in other Mixpanel reports.


2 Answers

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

like image 57
alephzarro Avatar answered Oct 04 '22 02:10

alephzarro


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"))
        });
});
like image 21
Thomas Hunter II Avatar answered Oct 04 '22 03:10

Thomas Hunter II