Created
May 1, 2014 19:34
-
-
Save mberman84/a9b06aea5b6107ad8b9e 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
module MixpanelHelper | |
def mixpanel_key | |
Rails.env.production? ? 'xxx' : 'yyy' | |
end | |
def mixpanel_track_with_super_properties(event_name, properties = {}) | |
properties = properties.merge(get_super_properties) unless get_super_properties.nil? | |
properties = properties.merge(:distinct_id => mixpanel_distinct_id) | |
@mixpanel.track(event_name, properties) | |
end | |
def mixpanel_set(distinct_id, properties = {}) | |
@mixpanel.set(distinct_id, properties) | |
end | |
def mixpanel_track_charge(distinct_id, amount) | |
@mixpanel.track_charge(distinct_id, amount.to_f) | |
end | |
def get_super_properties | |
begin | |
JSON.parse(cookies[:mp_super_properties]) unless cookies[:mp_super_properties].nil? | |
rescue JSON::ParserError => e | |
nil | |
end | |
end | |
def get_super_property(key) | |
super_properties = get_super_properties | |
super_properties[key] unless super_properties.nil? | |
end | |
def set_super_property(key, value) | |
property_hash = { key => value } | |
cookies[:mp_super_properties] = { | |
:expires => 1.year.from_now, | |
:value => cookies[:mp_super_properties].nil? ? property_hash.to_json : get_super_properties.merge(property_hash).to_json | |
} | |
end | |
def remove_super_property(key) | |
properties = get_super_properties | |
properties.try(:delete, key) | |
if properties.blank? | |
clear_super_properties | |
return | |
end | |
cookies[:mp_super_properties] = { | |
:expires => 1.year.from_now, | |
:value => properties.to_json | |
} | |
end | |
def clear_super_properties | |
cookies.delete :mp_super_properties | |
end | |
def set_utm_params_as_super_properties | |
set_super_property("utm_campaign", params[:utm_campaign]) if params[:utm_campaign] | |
set_super_property("utm_source", params[:utm_source]) if params[:utm_source] | |
set_super_property("utm_medium", params[:utm_medium]) if params[:utm_medium] | |
end | |
def set_utm_params_super_properties_as_user_properties | |
mixpanel_set(mixpanel_distinct_id, { :utm_campaign => get_super_property("utm_campaign")}) if get_super_property("utm_campaign") | |
mixpanel_set(mixpanel_distinct_id, { :utm_source => get_super_property("utm_source")}) if get_super_property("utm_source") | |
mixpanel_set(mixpanel_distinct_id, { :utm_medium => get_super_property("utm_medium")}) if get_super_property("utm_medium") | |
end | |
def generate_mixpanel_distinct_id | |
if cookies[:mixpanel_distinct_id].blank? | |
begin | |
distinct_id = SecureRandom.urlsafe_base64 | |
end while User.where(:mixpanel_distinct_id => distinct_id).exists? | |
cookies[:mixpanel_distinct_id] = { | |
expires: 20.years.from_now, | |
value: distinct_id | |
} | |
end | |
end | |
def unique_distinct_id?(distinct_id) | |
!User.where(:mixpanel_distinct_id => distinct_id).exists? | |
end | |
def mixpanel_distinct_id | |
current_user ? current_user.mixpanel_distinct_id : cookies[:mixpanel_distinct_id] | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment