parent
4f0f6087ad
commit
fdbad96a17
@ -0,0 +1,35 @@
|
||||
module Cartodb
|
||||
|
||||
class EventTracker
|
||||
|
||||
def send_event(user, event_name, custom_properties = {})
|
||||
return unless is_tracking_active?
|
||||
|
||||
# Some events register custom properties
|
||||
# Monitary values associated with the event should use 'revenue' reserved key
|
||||
properties = generate_event_properties(user).merge(custom_properties)
|
||||
|
||||
Resque.enqueue(Resque::EventDeliveryJobs::TrackEvent, user.id, event_name, properties)
|
||||
end
|
||||
|
||||
def test(name)
|
||||
Resque.enqueue(Resque::EventDeliveryJobs::Test, name)
|
||||
end
|
||||
|
||||
private
|
||||
def generate_event_properties(user)
|
||||
{
|
||||
'username' => user.username,
|
||||
'email' => user.email,
|
||||
'plan' => user.account_type,
|
||||
'organization' => user.organization_user? ? user.organization.name: nil,
|
||||
'event_origin' => 'Cartodb'
|
||||
}
|
||||
end
|
||||
|
||||
def is_tracking_active?
|
||||
!Cartodb.config[:segment]['api_key'].nil?
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -0,0 +1,41 @@
|
||||
require 'segment/analytics'
|
||||
|
||||
module Cartodb
|
||||
|
||||
# Development info: https://segment.com/docs/libraries/ruby/quickstart
|
||||
class SegmentTracker
|
||||
|
||||
def initialize
|
||||
@api_key = Cartodb.config[:segment]['api_key']
|
||||
@analytics = Segment::Analytics.new({
|
||||
write_key: @api_key
|
||||
})
|
||||
end
|
||||
|
||||
def enabled?
|
||||
!@api_key.nil?
|
||||
end
|
||||
|
||||
def track_event(user_id, event, properties)
|
||||
return unless enabled?
|
||||
|
||||
begin
|
||||
@analytics.track(
|
||||
user_id: user_id,
|
||||
event: event,
|
||||
properties: properties
|
||||
)
|
||||
rescue Exception => e
|
||||
Rollbar.report_message('Segment error tracking event', 'error', { user: user_id, event: event })
|
||||
end
|
||||
end
|
||||
|
||||
def flush
|
||||
begin
|
||||
@analytics.flush
|
||||
rescue Exception => e
|
||||
Rollbar.report_message('Segment error flush', 'error')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,29 @@
|
||||
require_relative '../cartodb/segment_tracker'
|
||||
|
||||
module Resque
|
||||
module EventDeliveryJobs
|
||||
|
||||
module Test
|
||||
@queue = :tracker
|
||||
|
||||
def self.perform(name)
|
||||
File.open("/home/jmartin/#{name}.txt", 'w') do |f2|
|
||||
f2.puts "yeaaaaaaah"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module TrackEvent
|
||||
@queue = :tracker
|
||||
|
||||
def self.perform(user_id, event_name, properties)
|
||||
tracker = Cartodb::SegmentTracker.new
|
||||
tracker.track_event(user_id,
|
||||
event_name,
|
||||
properties)
|
||||
tracker.flush
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -1,2 +1,2 @@
|
||||
#!/bin/sh
|
||||
VVERBOSE=true QUEUE=imports,users,geocodings,synchronizations rake environment resque:work
|
||||
VVERBOSE=true QUEUE=imports,users,geocodings,synchronizations,tracker rake environment resque:work
|
||||
|
Loading…
Reference in new issue