Class: Path::Reporting::Analytics

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

Overview

Our primary class for reporting analytics data. Once configured, this class can report analytics to any and all enabled and configured reporters.

This class is not a singleton, but is exposed via the analytics property once the Reporting module is initialized

Defined Under Namespace

Classes: Amplitude, Configuration, Console

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Analytics

Create a new analytics reporter with the given configuration

Parameters:



19
20
21
# File 'lib/path/reporting/analytics.rb', line 19

def initialize(config)
  @config = config
end

Instance Method Details

#record(name:, user:, product_code: "", product_area: "", user_type: UserType::PATIENT, trigger: Trigger::INTERACTION, metadata: {}, use_raw_event_name: false) ⇒ Array

Note:

No patient PII or PHI is allowed in our analytics data. By default we will attempt to strip this out of user data as well as metadata, but this is imperfect and should not be relied on. Instead, proactively exclude that data before it gets here.

Note:

The product_code, product_area, and name parameters will be formatted for easier searching automatically. Feel free to use regular text formatting here. E.g. "Self Scheduling" or "Hold booked"

Record analytics data to our enabled analytics channel

This is the primary (and at the moment only) way to report analytics data in our systems. Every configured analytics reporter will record the event with the data given here.

Examples:

PathReporting.analytics.record(
  product_code: Constants::ANALYTICS_PRODUCT_CODE,
  product_area: Constants::ANALYTICS_PRODUCT_AREA_MATCHING,
  name: 'Preferred provider multiple valid matches',
  user: @contact.analytics_friendly_hash,
  user_type: PathReporting::UserType::PATIENT,
  trigger: PathReporting::Trigger.PAGE_VIEW,
  metadata: ,
)

Validating Successful Reporting

analytics_reported = PathReporting.analytics.record(
  product_code: Constants::ANALYTICS_PRODUCT_CODE,
  product_area: Constants::ANALYTICS_PRODUCT_AREA_MATCHING,
  name: 'No preferred provider',
  user: @contact.analytics_friendly_hash,
  user_type: PathReporting::UserType::PATIENT,
  trigger: PathReporting::Trigger.PAGE_VIEW,
)

analytics_reported.each do |status|
  Rails.logger.warn("#{status.reporter} failed") unless status[:result].nil?
end

Parameters:

  • product_code (String) (defaults to: "")

    Code denoting which product this event occurred within. For example, "Self Scheduling". Can be used to view all the events that happen in a specific product

    Bias names toward whatever the major user-facing component is. For example, Therapy or Operations.

  • product_area (String) (defaults to: "")

    Area of the product that event relates to. For example, “Appointment Booking” or “User Settings”. Can be used to see all events in a particular product area, as well as in rare cases be used across product codes to show events across the system.

    Bias this name toward the particular flow or feature being used, e.g. Scheduling

  • name (String)

    Short plain text description of the event that is being recorded. For example, “Hold booked”

    Follow the format: “Object action [descriptor]”. For example, “Hold converted to appointment” or “Appointment deleted”

  • user (Hash)

    A simple hash containing relevant user information. This information should never contain any PII or PHI. There does, however, need to be an id property to uniquely identify the user.

    One of the following (in order):

    1. Patient/User
    2. If it does not relate to a patient, Provider
    3. If it does not relate to a patient or provider, Insurer
    4. If it does not relate to a patient, provider, or insurer, use external system ID information
      • E.g. for Zocdoc, this maybe the primary key for their API key
      • Do not use an API key as an identifier, instead use another key like the id in our database for that key

    If a different user (like an ops person) took an action, put an ID for them under agent_id in the metadata

  • user_type (Reporting::UserType) (defaults to: UserType::PATIENT)

    The type of user we are reporting this event for

  • trigger (Reporting::Trigger) (defaults to: Trigger::INTERACTION)

    What triggered this event to be recoreded (e.g. a page view, or an interaction).

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

    Metadata to report alongside the analytics event. *This should not contain any PII or PHI

  • use_raw_event_name (boolean) (defaults to: false)

    If true, uses just name for the event name. Otherwise it will be formated to code_area_name

Returns:

  • (Array)

    An array of result hashes with two keys:

    • :reporter [String] the analytics reporter the result is for
    • :result [nil | StandardError] what the result of running this reporter was. If it did not run, it will always be nil

Raises:

  • (StandardError)

    if no user is provided or user does not have id

  • (StandardError)

    if user_type is not a Reporting::UserType

  • (StandardError)

    if trigger is not a Reporting::Trigger

See Also:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/path/reporting/analytics.rb', line 158

def record(
  name:,
  user:,
  product_code: "",
  product_area: "",
  user_type: UserType::PATIENT,
  trigger: Trigger::INTERACTION,
  metadata: {},
  use_raw_event_name: false
)
  raise Error, "No user hash provided when reporting analytics" if !user.is_a?(Hash) || !(user[:id] || user["id"] || user[:device_id] || user["device_id"])
  raise Error, "Invalid UserType #{user_type}" unless UserType.valid?(user_type)
  raise Error, "Invalid Trigger #{trigger}" unless Trigger.valid?(trigger)

  full_name = use_raw_event_name ? name : format_event_name(product_code: product_code, product_area: product_area, name: name)

  clients.map do |reporter, client|
    {
      reporter: reporter.to_s,
      result: send_event_to_client(client, {
                                     name: full_name,
                                     user: user,
                                     user_type: user_type,
                                     metadata: ,
                                     trigger: trigger
                                   })
    }
  end
end