Skip to content

Instantly share code, notes, and snippets.

@sandinmyjoints
Last active December 21, 2015 02:19
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/6234654 to your computer and use it in GitHub Desktop.
Save sandinmyjoints/6234654 to your computer and use it in GitHub Desktop.
Account create success callback.
# Constants.
#
WAIT_BEFORE_REDIRECT = 300 # ms to wait before any redirect.
WAIT_AFTER_TRACK = 2000 # ms to wait for mp to acknowledge $signup event.
# Helpers.
#
redirect = (msg, error = false) ->
if error
logError msg
else
logInfo msg
_redirect = ->
window.location.href = "#{host}/#users/new-profile"
null
# A little delay to give our various async requests time to finish before
# hard redirect.
#
setTimeout _redirect, WAIT_BEFORE_REDIRECT
# Mixpanel getter.
#
_mp = ->
logError "window.mixpanel is null" unless window.mixpanel
window.mixpanel ? null
# Log and track Mixpanel event.
mpTrack = (ev, data, msg, cb = ->) ->
logEvent $.extend event: ev, data
logInfo msg if msg?
_mp()?.track ev, data, cb
# ISO UTC date.
#
# Adapted from: http://webcloud.se/log/JavaScript-and-ISO-8601/
#
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 ""
# Callback from the xhr request to create a new user in our app.
#
success = (resUser, status, xhr) ->
_createdDate = new Date()
_createdIso = isoDate _createdDate
# Initial Mixpanel data.
#
newUserData =
$email: user.attributes.email
$first_name: user.attributes.name.first
$last_name: user.attributes.name.last
$created: _createdIso
user_id: resUser._id
user_agent: navigator.userAgent
lessons_completed: 0
trial_lessons_completed: 0
# 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.
#
userMpData = $.extend {}, newUserData, generalMpData()
mpTrack "$signup", userMpData, null, ->
redirect("Redirect (normal) in mpTrack callback.")
# Send a page view event to Google Analytics.
#
_gaq?.push ['_trackPageview', '/signup-success']
# Send a signup success event to Optimizely.
#
_op().push ['trackEvent', 'signup-success']
# Mixpanel: name_tag, people.set, alias.
#
# Set the people special properties that are available.
# See: https://mixpanel.com/docs/people-analytics/special-properties
#
_mp()?.name_tag user.email
# `people.set` uses a real JS date, but the rest of tracking properties
# use an ISO-date-formatted string.
#
_mp()?.people.set $.extend {}, newUserData, $created: _createdDate
# 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
#
# As seen in the Mixpanel js lib source, `alias` calls `identify`, so we
# do **not** need to call it.
#
aliasResult = _mp()?.alias resUser._id
if aliasResult < 0
logError "#{resUser._id}: alias returned #{aliasResult}"
# Also set event super property with newUserData.
_mp()?.register newUserData
# Force redirect to app after if we still haven't heard from MP.
msg = "Redirect (forced) after #{WAIT_AFTER_TRACK} ms timeout."
setTimeout (-> redirect msg, true), WAIT_AFTER_TRACK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment