Created
July 30, 2013 15:22
-
-
Save sandinmyjoints/6113950 to your computer and use it in GitHub Desktop.
This file contains 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
# Helpers. | |
_mp = -> | |
window.mixpanel ? null | |
redirect = -> | |
_redirect = -> | |
window.location.href = "#{host}/#users/new-profile" | |
null | |
# A little delay to ensure MP events are all sent. Setting this to 50 ms | |
# resulted in the $created super property not being set. Not 100% sure it's | |
# a timing issuse, but with 100 ms it seems to work consistently. | |
# | |
setTimeout _redirect, 100 | |
isoDate = (date = new Date()) -> | |
pad = (num) -> if num < 10 then '0' + num else num | |
[ | |
date.getUTCFullYear(), '-' | |
pad(date.getUTCMonth()+1), '-' | |
pad(date.getUTCDate()), 'T' | |
pad(date.getUTCHours()), ':' | |
pad(date.getUTCMinutes()), ':' | |
pad(date.getUTCSeconds()), '.' | |
pad(date.getUTCMilliseconds()), 'Z' | |
].join "" | |
# Set Mixpanel data. | |
# | |
# `user` is from data in a form submitted to our server. `resUser` is | |
# from the server response after the user is created. | |
# | |
data = | |
$email: user.attributes.email | |
$first_name: user.attributes.name.first | |
$last_name: user.attributes.name.last | |
$created: new Date() | |
_mp()?.people.set data | |
# MixPanel: Alias distinct_id to user id. | |
# | |
# This switches the MixPanel GUID to our id (the user id) and | |
# should only be done *once* - **here**. After this, we associate | |
# our ID after authentication (with `mixpanel.identify()`). | |
# | |
# See: | |
# - http://blog.mixpanel.com/2012/09/12/best-practices-updated/ | |
# - https://mixpanel.com/docs/managing-users/ | |
# assigning-your-own-unique-identifiers-to-users | |
_mp()?.alias resUser._id | |
# Mutate created to be ISO string. | |
# | |
# `people.set` uses a real JS date, but the rest of tracking properties | |
# don't. | |
# | |
data = $.extend {}, data, | |
$created: isoDate(data.$created) | |
# Also set event super property with data. | |
_mp()?.register data | |
# Identify to trigger sending queued people.set data. | |
_mp()?.identify resUser._id | |
# Event tracking the same global information and special "$signup" | |
# See: http://blog.mixpanel.com/2011/12/22/ | |
# importing-born-or-signup-events-for-existing-users/ | |
# | |
# On callback, redirect to app. | |
# | |
_mp()?.track "$signup", data, -> | |
redirect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment