Created
April 16, 2012 17:36
-
-
Save jhdavids8/2400137 to your computer and use it in GitHub Desktop.
A bit of a hack to track right-click/context menu link openings (i.e. right-click and select "Open in New Tab/Window")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
// A bit of a hack to track right-click and "Open in New Tab/Window clicks" | |
$('#div_id a').bind("contextmenu",function(e){ | |
var d1 = new Date (); | |
var d2 = new Date ( d1 ); | |
// Cookies will expire in 10 seconds, so basically the user has 10 seconds to right-click and open the link. | |
d2.setSeconds ( d1.getSeconds() + 10 ); | |
$.cookie('right_click_cookie', $(this).attr('href'), { | |
expires: d2, | |
path: '/' | |
}); | |
}); | |
right_click_cookie = $.cookie('right_click_cookie') | |
// Does the value of the cookie equal the relative or absolute URL for the right-clicked link? If so, it's likely the user right-clicked the link and hit "Open in New Tab/Window" | |
if (right_click_cookie != null && (window.location.pathname == right_click_cookie || window.location.href == right_click_cookie)) { | |
// Clear the cookie so a refresh doesn't retrigger the event | |
$.cookie('right_click_cookie', null, { path: '/' }); | |
// Came up with this code to track ad clicks via Mixpanel, so log a Mixpanel event that serves as a normal ad-clicked event | |
mixpanel.track("Ad Click", { | |
// Whatever Mixpanel properties you'd like to send! | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'll need this library to write/read the cookies: https://github.com/carhartl/jquery-cookie