Class: Path::Reporting::Analytics::Amplitude

Inherits:
Object
  • Object
show all
Defined in:
lib/path/reporting/analytics/amplitude.rb

Overview

Amplitude analytics is our primary analytics channel for production

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Amplitude

Setup and configure AmplitudeAPI with the given configuration

Parameters:

  • config (AmplitudeAPI::Config)

    the configuration for AmplitudeAPI

See Also:



66
67
68
69
70
71
72
# File 'lib/path/reporting/analytics/amplitude.rb', line 66

def initialize(config)
  @config = config

  config.analytics.amplitude_config.each do |key, value|
    AmplitudeAPI.config.instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



60
61
62
# File 'lib/path/reporting/analytics/amplitude.rb', line 60

def config
  @config
end

Instance Method Details

#record(name:, user:, user_type:, trigger:, metadata: {}) ⇒ Object

Record the metadata to Amplitude

Parameters:

  • name (String)

    Formatted name to send to Amplitude

  • user (Hash)

    User object. Must contain :id and no PII

  • user_type (UserType)

    Type of user

  • trigger (Trigger)

    Trigger for this event

  • metadata (Hash) (defaults to: {})

    Metadata to send with the event

Raises:

See Also:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/path/reporting/analytics/amplitude.rb', line 81

def record(name:, user:, user_type:, trigger:, metadata: {})
  user = user.dup
   = .dup
  user[:user_type] = user_type
  [:trigger] = trigger
  [:system_name] = config.system_name

  event_props = {
    user_properties: scrub_pii(user),
    event_type: name,
    event_properties: scrub_pii()
  }
  event_props[:user_id] = (user[:id] || user["id"]).to_s unless (user[:id] || user["id"]).nil?
  event_props[:device_id] = (user[:device_id] || user["device_id"]).to_s unless (user[:device_id] || user["device_id"]).nil?
  API_METADATA_TO_ELEVATE.each do |key|
    event_props[key] = [key] if .key? key
  end

  response = AmplitudeAPI.track AmplitudeAPI::Event.new event_props
  raise Error, response.body unless response.success?

  response
end