Skip to content

Instantly share code, notes, and snippets.

@leoallen85
Created November 18, 2015 11:55
Show Gist options
  • Save leoallen85/b341441168310863b2d7 to your computer and use it in GitHub Desktop.
Save leoallen85/b341441168310863b2d7 to your computer and use it in GitHub Desktop.
require 'segment'
require './lib/configurable'
require './lib/pipedrive/client'
module Analytics
class Update
include Configurable
attr_reader :params
attr_writer :analytics, :pipedrive
INTEGRATIONS = [:"Customer.io", :Mixpanel]
def self.config_file_path
Pipedrive::CONFIG_FILE
end
def initialize(params)
@params = params
end
def call
send(stage)
end
def analytics
@analytics ||= Segment::Analytics.new(write_key: ENV["SEGMENT_WRITE_KEY"], on_error: Proc.new { |status, error| raise error })
end
def pipedrive
@pipedrive ||= Pipedrive::Client.new
end
def self.call(params)
new(params).call
end
private
def interview_offered
identify_details(
"Current Stage" => "Interview Offered",
"Automatically Send Email" => true,
"Send Interview Follow-Up" => true,
"Pipedrive Deal ID" => deal_id,
"Interview Notification Sent" => Date.today
)
track_event("Interview Offered")
end
def interview_booked
identify_details("Current Stage" => "Interview Booked")
track_event("Interview Booked")
end
def interview_passed
identify_details(
"Current Stage" => "Interview Passed",
"Interview Quality" => interview_quality,
"Offer Notification Sent" => Date.today
)
track_event("Interview Passed")
end
def deposit_paid
identify_details("Current Stage" => "Deposit Paid", "Interview Quality" => interview_quality)
track_event("Deposit Paid", "Interview Quality" => interview_quality)
end
def fees_paid
identify_details(
"Interview Quality" => interview_quality,
"Current Stage" => "Fees Paid",
revenue: course_fee
)
track_event("Fees Paid", revenue: course_fee, "Interview Quality" => interview_quality)
end
def hired
identify_details(
"Interview Quality" => interview_quality,
"Course Quality" => course_quality,
"Current Stage" => "Hired",
revenue: placement_fee
)
track_event("Hired",
"Interview Quality" => interview_quality,
"Course Quality" => course_quality,
"Total Revenue" => course_fee + placement_fee,
revenue: placement_fee,
)
end
def deal_lost
identify_details("Current Stage" => "Deal Lost")
end
def track_event(event, additional_properties = {})
analytics.track(user_id: email,
event: event,
properties: event_properties.merge(additional_properties),
integrations: integrations)
flush
end
def identify_basic_details
analytics.identify(user_id: email, traits: {
"email" => email,
"firstName" => first_name,
"lastName" => last_name,
"Cohort" => cohort,
"Course Type" => course_type
}, integrations: integrations)
end
def identify_details(params)
identify_basic_details
analytics.identify(user_id: email, traits: params, integrations: integrations)
end
def flush
analytics.flush if in_production?
end
def in_production?
ENV["RACK_ENV"] == 'production'
end
def integrations
INTEGRATIONS
.map { |integration| [integration, true] }
.to_h
.merge(all: false)
end
def event_properties
{ "Course Type" => course_type }
end
def email
params["email"]
end
def cohort
pipedrive.label_for_person_field(config[:people_fields][:cohort], params["cohort"].to_i)
end
def course_fee
params["course_fee"].to_i
end
def placement_fee
params["placement_fee"].to_i
end
def first_name
params["first_name"]
end
def last_name
params["last_name"]
end
def deal_id
params["deal_id"]
end
def course_type
config[:course_type][params["course_type"].to_i]
end
def interview_quality
config[:interview_quality][params["interview_quality"].to_i]
end
def course_quality
config[:course_quality][params["course_quality"].to_i]
end
def stage
config[:stages][params["deal_stage_id"].to_i]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment