Last active
September 12, 2016 18:34
-
-
Save mberman84/ddccdd0389cdc869705072330b6998fb 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
class SegmentService | |
# method_name must be a symbol | |
# def initialize(user, method_name, method_args = nil) | |
# binding.pry | |
# @user = user | |
# @company = @user.company | |
# @analytics_uuid = @user.analytics_uuid | |
# send(method_name, method_args) | |
# # wrap the entire SegmentService in a rescue so if SegmentService fails | |
# # for whatever reason, our other code doesn't fail | |
# rescue => e | |
# Rails.logger.info "SegmentService Error: #{e.message}, on event: #{method_name}, with user: #{user&.id}" | |
# end | |
def initialize(user) | |
@user = user | |
@company = @user.company | |
@analytics_uuid = @user.analytics_uuid | |
end | |
def self.safe_method(name, &blk) | |
define_method name do |*args, &blk2| | |
begin | |
blk.call(*args, &blk2) | |
rescue => e | |
Rails.logger.info "SegmentService Rescued #{name}, with error: #{e.message}." | |
end | |
end | |
end | |
def self.generate_analytics_uuid | |
SecureRandom.uuid | |
end | |
def self.tracked_utm_params | |
[:utm_campaign, :utm_medium, :utm_source] | |
end | |
def self.get_analytics_uuid_from_cookies(cookies = nil) | |
cookies[:analytics_uuid] | |
end | |
def self.get_utm_params_from_cookies(cookies = nil) | |
utm_params = {} | |
tracked_utm_params.each do |utm_param| | |
utm_params[utm_param] = cookies[utm_param] if cookies[utm_param] | |
end | |
utm_params | |
end | |
safe_method :send_segment_payment_event do |params| | |
event_params = { | |
analytics_uuid: @analytics_uuid, | |
event: 'Payment Received', | |
properties: { | |
revenue: params[:amount] / 100, # preferred Segment naming convention | |
type: params[:type] | |
} | |
} | |
send_event_to_segment(event_params) | |
end | |
safe_method :send_segment_invitation_sent_event do |user_email| | |
binding.pry | |
event_params = { | |
analytics_uuid: @analytics_uuid, | |
event: 'Invitation Sent', | |
properties: { invite_email: user_email } | |
} | |
send_event_to_segment(event_params) | |
end | |
safe_method :send_segment_invitation_accepted_event do | |
binding.pry | |
event_params = { | |
analytics_uuid: @analytics_uuid, | |
event: 'Invitation Accepted', | |
properties: { | |
invitation_sent_at: @user.invitation_sent_at, | |
invitation_accepted_at: @user.invitation_accepted_at | |
} | |
} | |
send_event_to_segment(event_params) | |
end | |
safe_method :send_segment_user_login_event do | |
event_params = { | |
analytics_uuid: @analytics_uuid, | |
event: 'User Logged In', | |
properties: { | |
last_sign_in_at: @user.last_sign_in_at | |
} | |
} | |
send_event_to_segment(event_params) | |
end | |
safe_method :send_segment_new_company_event do | |
event_params = { | |
analytics_uuid: @analytics_uuid, | |
event: 'Company Created', | |
properties: { | |
company_id: @company.id | |
} | |
} | |
send_event_to_segment(event_params) | |
end | |
safe_method :send_segment_new_subscription_event do | |
event_params = { | |
analytics_uuid: @analytics_uuid, | |
event: 'Subscription Created', | |
properties: subscription_properties(@company.subscription) | |
} | |
send_event_to_segment(event_params) | |
end | |
safe_method :send_segment_subscription_update_event do | |
event_params = { | |
analytics_uuid: @analytics_uuid, | |
event: 'Subscription Updated', | |
properties: subscription_properties(@company.subscription) | |
} | |
send_event_to_segment(event_params) | |
end | |
safe_method :send_segment_subscription_cancel_event do | |
event_params = { | |
analytics_uuid: @analytics_uuid, | |
event: 'Subscription Cancelled', | |
properties: {} | |
} | |
send_event_to_segment(event_params) | |
end | |
safe_method :send_segment_mass_message_event do |params| | |
event_params = { | |
analytics_uuid: @analytics_uuid, | |
event: 'Mass Message Sent', | |
properties: { | |
recipient_count: params[:recipient_count], | |
mass_message_id: params[:mass_message_id], | |
mass_message_text1: params[:mass_message_text1], | |
mass_message_text2: params[:mass_message_text2], | |
mass_message_property_name: params[:mass_message_property_name], | |
mass_message_property_value: params[:mass_message_property_value] | |
} | |
} | |
send_event_to_segment(event_params) | |
end | |
private | |
def subscription_properties(subscription) | |
{ | |
subscription_id: subscription.id, | |
current_period_start: subscription.current_period_start, | |
current_period_end: subscription.current_period_end, | |
plan_id: subscription.plan_id, | |
status: subscription.status | |
} | |
end | |
def send_event_to_segment(event_params) | |
Analytics.track( | |
user_id: event_params[:analytics_uuid], | |
event: event_params[:event], | |
properties: event_params[:properties] | |
) | |
send_segment_identify_params | |
rescue => e | |
Rails.logger.info "Analytics Event failed with error #{e.message}" | |
end | |
def send_segment_identify_params | |
if @company.utm_params | |
utm_campaign = @company.utm_params['utm_campaign'] | |
utm_source = @company.utm_params['utm_source'] | |
utm_medium = @company.utm_params['utm_medium'] | |
end | |
user_params = { | |
user_id: @analytics_uuid, | |
traits: { | |
email: @user.email, | |
firstName: @user.first_name, | |
lastName: @user.last_name, | |
signInCount: @user.sign_in_count, | |
companyId: @company.id, | |
companyName: @company.name, | |
massMessageEnabled: @company.mass_message_enabled, | |
beta: @company.beta, | |
companyTier: @company.tier, | |
facebookIntegration: @company.fb_integrations.any?, | |
invited: @user.invitation_accepted_at.present?, | |
id: @user.id, | |
createdAt: @user.created_at, | |
phone: @user.verified_phone_number, | |
admin: @user.admin, | |
utm_campaign: utm_campaign, | |
utm_source: utm_source, | |
utm_medium: utm_medium | |
} | |
} | |
Analytics.identify(user_id: user_params[:user_id], traits: user_params[:traits]) | |
rescue => e | |
Rails.logger.info "Analytics Identify failed with error #{e.message}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment