Created
November 13, 2012 21:09
-
-
Save nateberkopec/4068411 to your computer and use it in GitHub Desktop.
mixpanel_init.js.coffee
This file contains hidden or 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
$ -> | |
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