Skip to content

Instantly share code, notes, and snippets.

@sandinmyjoints
Created July 30, 2013 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandinmyjoints/6113950 to your computer and use it in GitHub Desktop.
Save sandinmyjoints/6113950 to your computer and use it in GitHub Desktop.
# 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