Created
January 27, 2016 16:21
-
-
Save johncblandii/921857d51dbb1f8e5f53 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
require 'mixpanel-ruby' | |
class AnalyticsHelper | |
def initialize | |
@@tracker = Mixpanel::Tracker.new Rails.application.secrets.mixpanel_token | |
end | |
def track user_id, action, properties=nil | |
Rails.logger.debug "Tracking: #{action}" | |
@@tracker.track(user_id, action, properties || {}) | |
end | |
# => USER | |
def user_registered user, label="User Registered" | |
log "User registered: #{user.email}" | |
self.update_user user | |
self.track user.id, label | |
end | |
def user_profile_changed user, label="User Profile Updated" | |
log "User profile changed: #{user.email}" | |
self.update_user user | |
self.track user.id, label | |
end | |
def user_signed_in user, label="User Signed In" | |
log "User signed in: #{user.email}" | |
@@tracker.people.set(user.id, { | |
'Logins' => user.sign_in_count, | |
'$first_name' => user.first_name, # these fields are here for pre-analytics users | |
'$last_name' => user.last_name, | |
'$email' => user.email | |
}) | |
@@tracker.people.set_once(user.id, { | |
'$created' => user.created_at | |
}) | |
self.track user.id, label | |
end | |
# => Utility | |
def log message | |
Rails.logger.debug "ANALYTICS: #{message}" | |
end | |
protected | |
def update_user user | |
log "Updating User: #{user.email}" | |
@@tracker.people.set(user.id, { | |
'$first_name' => user.first_name, | |
'$last_name' => user.last_name, | |
'$email' => user.email, | |
'$created' => user.created_at, | |
'account_id' => user.account_id, | |
}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment