Skip to content

Instantly share code, notes, and snippets.

@upstarter
Forked from nateberkopec/mixpanel_init.js.coffee
Last active August 29, 2015 14:18
Show Gist options
  • Save upstarter/d76c4bee6472af245630 to your computer and use it in GitHub Desktop.
Save upstarter/d76c4bee6472af245630 to your computer and use it in GitHub Desktop.
$ ->
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