Skip to content

Instantly share code, notes, and snippets.

@joshdover
Created December 14, 2013 22:49
Show Gist options
  • Save joshdover/7966016 to your computer and use it in GitHub Desktop.
Save joshdover/7966016 to your computer and use it in GitHub Desktop.
Declarative Mixpanel Helper Allows you to declare tracking of onClick event on any type of DOM element.
# Helper for tracking mixpanel events, allows for declaritve events. Usage:
# <a href="http://google.com" data-mixpanel="name: 'clicked link'">Google</a>
# <a href="http://cows.com" data-mixpanel="name: 'clicked link', data: { animal: 'cow' }">Cow</a>
# <li data-mixpanel="'clicked list element'">I am not a link!</li>
$(document).on "click", "[data-mixpanel]", (event) ->
options = eval "({" + $(this).attr("data-mixpanel") + "})"
# If event is bound to a link, add delay to ensure event is tracked.
if event.currentTarget.href?
options.data = {} if !options.data?
options.data.url = event.currentTarget.href
# Only add delay if link is being opened in this frame
if !(event.which is 2 or event.metaKey or event.currentTarget.target is '_blank')
event.preventDefault()
setTimeout(->
window.location = options.data.url
, 300)
mixpanel.track options.name, options.data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment