Class: Greenfinch::Events
- Inherits:
-
Object
- Object
- Greenfinch::Events
- Defined in:
- lib/greenfinch-ruby/events.rb
Overview
Handles formatting Greenfinch event tracking messages and sending them to the consumer. Greenfinch::Tracker is a subclass of this class, and the best way to track events is to instantiate a Greenfinch::Tracker
tracker = Greenfinch::Tracker.new(YOUR_GREENFINCH_TOKEN) # Has all of the methods of Greenfinch::Event
tracker.track(...)
Direct Known Subclasses
Instance Method Summary collapse
-
#import(api_key, distinct_id, event, properties = {}, ip = nil) ⇒ Object
Imports an event that has occurred in the past, along with a distinct_id representing the source of that event (for example, a user id), an event name describing the event and a set of properties describing that event.
-
#initialize(token, service_name, debug, error_handler = nil, use_internal_domain: false, &block) ⇒ Events
constructor
You likely won’t need to instantiate an instance of Greenfinch::Events directly.
-
#track(distinct_id, event, properties = {}, ip = nil) ⇒ Object
Notes that an event has occurred, along with a distinct_id representing the source of that event (for example, a user id), an event name describing the event and a set of properties describing that event.
Constructor Details
#initialize(token, service_name, debug, error_handler = nil, use_internal_domain: false, &block) ⇒ Events
You likely won’t need to instantiate an instance of Greenfinch::Events directly. The best way to get an instance is to use Greenfinch::Tracker
# tracker has all of the methods of Greenfinch::Events
tracker = Greenfinch::Tracker.new(YOUR_GREENFINCH_TOKEN)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/greenfinch-ruby/events.rb', line 25 def initialize(token, service_name, debug, error_handler=nil, use_internal_domain: false, &block) @token = token @service_name = service_name @error_handler = error_handler || ErrorHandler.new events_endpoint = if debug if use_internal_domain "https://internal-event-staging.kcd.partners/api/publish" else "https://event-staging.kcd.partners/api/publish" end else if use_internal_domain "https://internal-event.kcd.partners/api/publish" else "https://event.kcd.partners/api/publish" end end if block @sink = block else consumer = Consumer.new(events_endpoint) @sink = consumer.method(:send!) end end |
Instance Method Details
#import(api_key, distinct_id, event, properties = {}, ip = nil) ⇒ Object
Imports an event that has occurred in the past, along with a distinct_id representing the source of that event (for example, a user id), an event name describing the event and a set of properties describing that event. Properties are provided as a Hash with string keys and strings, numbers or booleans as values. By default, we pass the time of the method call as the time the event occured, if you wish to override this pass a timestamp in the properties hash.
tracker = Greenfinch::Tracker.new(YOUR_GREENFINCH_TOKEN)
# Track that user "12345"'s credit card was declined
tracker.import("API_KEY", "12345", "Credit Card Declined")
# Properties describe the circumstances of the event,
# or aspects of the source or user associated with the event
tracker.import("API_KEY", "12345", "Welcome Email Sent", {
'Email Template' => 'Pretty Pink Welcome',
'User Sign-up Cohort' => 'July 2013',
'time' => 1369353600,
})
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/greenfinch-ruby/events.rb', line 122 def import(api_key, distinct_id, event, properties={}, ip=nil) properties = { 'distinct_id' => distinct_id, 'token' => @token, 'time' => Time.now.to_i, 'mp_lib' => 'ruby', '$lib_version' => Greenfinch::VERSION, }.merge(properties) properties['ip'] = ip if ip data = { 'event' => event, 'properties' => properties, } = { 'data' => data, 'api_key' => api_key, } ret = true begin @sink.call(:import, .to_json) rescue GreenfinchError => e @error_handler.handle(e) ret = false end ret end |
#track(distinct_id, event, properties = {}, ip = nil) ⇒ Object
Notes that an event has occurred, along with a distinct_id representing the source of that event (for example, a user id), an event name describing the event and a set of properties describing that event. Properties are provided as a Hash with string keys and strings, numbers or booleans as values.
tracker = Greenfinch::Tracker.new(YOUR_GREENFINCH_TOKEN)
# Track that user "12345"'s credit card was declined
tracker.track("12345", "Credit Card Declined")
# Properties describe the circumstances of the event,
# or aspects of the source or user associated with the event
tracker.track("12345", "Welcome Email Sent", {
'Email Template' => 'Pretty Pink Welcome',
'User Sign-up Cohort' => 'July 2013'
})
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/greenfinch-ruby/events.rb', line 70 def track(distinct_id, event, properties={}, ip=nil) properties = { 'user_id' => distinct_id, 'time' => Time.now.to_i, 'mp_lib' => 'ruby', '$lib_version' => Greenfinch::VERSION, }.merge(properties) properties['ip'] = ip if ip data = { 'event' => event, 'prop' => properties, } = { 'data' => data, 'jwt_token' => @token, 'service_name' => @service_name, } ret = true begin @sink.call(:event, .to_json) rescue GreenfinchError => e @error_handler.handle(e) ret = false end ret end |