Skip to content

Instantly share code, notes, and snippets.

@teejayvanslyke
Created May 1, 2012 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save teejayvanslyke/2569284 to your computer and use it in GitHub Desktop.
Save teejayvanslyke/2569284 to your computer and use it in GitHub Desktop.
Using data attributes to elegantly create Mixpanel trackers. http://tjvanslyke.com/using-data-attributes-to-elegantly-create-mixpanel-trackers/
$('a[data-mixpanel-tracker]').click ->
setTimeout (=> window.location.href = $(@).attr('href')), 300
tracker = $(@).attr('data-mixpanel-tracker')
properties = JSON.parse($(@).attr('data-mixpanel-properties') || '{}')
mixpanel.track tracker, properties
return false
@mtrimpe
Copy link

mtrimpe commented Jun 14, 2013

You can improve on this a bit more still by turning the function that jumps to the href location into a var and passing it as the callback of mixpanel.track as well.

That way you jump as soon as the event is received by MixPanel in case it's faster than 300ms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment