Skip to content

Instantly share code, notes, and snippets.

@nateberkopec
Created November 13, 2012 21:09
Show Gist options
  • Save nateberkopec/4068411 to your computer and use it in GitHub Desktop.
Save nateberkopec/4068411 to your computer and use it in GitHub Desktop.
mixpanel_init.js.coffee
$ ->
active_tracker.track_links()
active_tracker.track_forms()
# be sure to load Mixpanel's javascript before this, so window.mixpanel exists.
window.active_tracker = new Object
active_tracker.track_links = (selector = 'a[data-event-name]') ->
$.each $(selector), (index, value) ->
$value = $(value)
properties = $.parseJSON($value.attr("data-event-properties")) or new Object
eventName = $value.attr 'data-event-name'
klass = 'active_tracker_link' + index
$value.addClass(klass)
mixpanel.track_links '.' + klass, eventName, properties
active_tracker.track_forms = (selector = "form[data-event-name]") ->
$.each $(selector), (index, value) ->
$value = $(value)
properties = $.parseJSON($value.attr("data-event-properties")) or new Object
eventName = $value.attr 'data-event-name'
id = 'active_tracker_form' + index
$value.attr('id', id)
mixpanel.track_forms '#' + id, eventName, properties
# now, you can add 'data-event-name="Event Name"' as an attribute to any link, and clicking on that link will send an event to Mixpanel!
# also, if you add 'data-event-properties' to the link with some JSON inside of it, this coffeescript will send along the json as additional information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment