Class: Itly::Plugin::Amplitude
- Inherits:
-
Itly::Plugin
- Object
- Itly::Plugin
- Itly::Plugin::Amplitude
- Defined in:
- lib/itly/plugin/amplitude/amplitude.rb,
lib/itly/plugin/amplitude/options.rb,
lib/itly/plugin/amplitude/version.rb,
lib/itly/plugin/amplitude/call_options.rb
Overview
Amplitude plugin class for Itly SDK
Defined Under Namespace
Classes: CallOptions, Options
Constant Summary collapse
- VERSION =
'0.1.1'
Instance Attribute Summary collapse
-
#disabled ⇒ Object
readonly
Returns the value of attribute disabled.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#id ⇒ String
Get the plugin ID.
-
#identify(user_id:, properties: nil, options: nil) ⇒ Object
Identify a user.
-
#initialize(api_key:, disabled: false) ⇒ Amplitude
constructor
Instantiate a new Plugin::Amplitude.
-
#load(options:) ⇒ Object
Initialize AmplitudeApi client.
-
#name ⇒ Object
Amplitude specific plugin options class for calls to plugin methods.
-
#track(user_id:, event:, options: nil) ⇒ Object
Track an event.
Constructor Details
#initialize(api_key:, disabled: false) ⇒ Amplitude
Instantiate a new Plugin::Amplitude
20 21 22 23 24 25 |
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 20 def initialize(api_key:, disabled: false) super() @disabled = disabled ::AmplitudeAPI.config.api_key = api_key end |
Instance Attribute Details
#disabled ⇒ Object (readonly)
Returns the value of attribute disabled.
12 13 14 |
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 12 def disabled @disabled end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
12 13 14 |
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 12 def logger @logger end |
Instance Method Details
#id ⇒ String
Get the plugin ID
105 106 107 |
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 105 def id 'amplitude' end |
#identify(user_id:, properties: nil, options: nil) ⇒ Object
Identify a user
Raise an error if the response is not 200
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 52 def identify(user_id:, properties: nil, options: nil) super return unless enabled? # Log log = Itly::Loggers.vars_to_log user_id: user_id, properties: properties, options: logger&.info "#{id}: identify(#{log})" # Send through the client payload = {} payload.merge! .to_hash if payload.merge! properties if properties call_end_point(&.callback) do ::AmplitudeAPI.send_identify user_id, nil, payload end end |
#load(options:) ⇒ Object
Initialize AmplitudeApi client
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 32 def load(options:) super # Get options @logger = .logger # Log logger&.info "#{id}: load()" logger&.info "#{id}: plugin is disabled!" if @disabled end |
#name ⇒ Object
Amplitude specific plugin options class for calls to plugin methods
52 53 54 55 56 57 58 59 |
# File 'lib/itly/plugin/amplitude/call_options.rb', line 52 %w[Identify Group Page Track Alias].each do |name| class_eval( <<-EVAL, __FILE__, __LINE__ + 1 class #{name}Options < CallOptions # class IdentifyOptions < CallOptions end # end EVAL ) end |
#track(user_id:, event:, options: nil) ⇒ Object
Track an event
Raise an error if the response is not 200
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 79 def track(user_id:, event:, options: nil) super return unless enabled? # Log log = Itly::Loggers.vars_to_log( user_id: user_id, event: event&.name, properties: event&.properties, options: ) logger&.info "#{id}: track(#{log})" # Send through the client call_end_point(&.callback) do ::AmplitudeAPI.track ::AmplitudeAPI::Event.new( user_id: user_id, event_type: event.name, event_properties: event.properties, **(&.to_hash || {}) ) end end |