class Itly::Plugin::Snowplow

Snowplow plugin class for Itly SDK

Snowplow plugin class for Itly SDK

Constants

VERSION

Attributes

client[R]
disabled[R]
logger[R]
vendor[R]

Public Class Methods

new(vendor:, options:) click to toggle source

Instantiate a new Plugin::Snowplow

@param [String] vendor: the Snowplow vendor @param [Itly::Plugin::Snowplow::Options] options: the options. See Itly::Plugin::Snowplow::Options

Calls superclass method
# File lib/itly/plugin/snowplow/snowplow.rb, line 20
def initialize(vendor:, options:)
  super()
  @vendor = vendor
  @disabled = options.disabled

  emitter = SnowplowTracker::Emitter.new \
    options.endpoint, protocol: options.protocol, method: options.method, buffer_size: options.buffer_size
  @client = SnowplowTracker::Tracker.new emitter
end

Public Instance Methods

id() click to toggle source

Get the plugin ID

@return [String] plugin id

# File lib/itly/plugin/snowplow/snowplow.rb, line 143
def id
  'snowplow'
end
identify(user_id:, properties: nil, options: nil) click to toggle source

Identify a user

Raise an error if the client fails

@param [String] user_id: the id of the user in your application @param [Hash] properties: unused @param [Itly::Plugin::Snowplow::IdentifyOptions] options: the plugin specific options

Calls superclass method
# File lib/itly/plugin/snowplow/snowplow.rb, line 55
def identify(user_id:, properties: nil, options: nil)
  super
  return unless enabled?

  # Log
  log = Itly::Loggers.vars_to_log user_id: user_id, options: options
  logger&.info "#{id}: identify(#{log})"

  # Send through the client
  client.set_user_id user_id
end
load(options:) click to toggle source

Initialize Snowplow plugin

@param [Itly::PluginOptions] options: plugin options

Calls superclass method
# File lib/itly/plugin/snowplow/snowplow.rb, line 35
def load(options:)
  super
  # Get options
  @logger = options.logger

  # Log
  logger&.info "#{id}: load()"

  logger&.info "#{id}: plugin is disabled!" if @disabled
end
page(user_id:, category: nil, name: nil, properties: nil, options: nil) click to toggle source

Record page views

Raise an error if the client fails

@param [String] user_id: the id of the user in your application @param [String] category: the category of the page @param [String] name: the name of the page. @param [Hash] properties: the properties to pass to your application @param [Itly::Plugin::Snowplow::PageOptions] options: the plugin specific options

Calls superclass method
# File lib/itly/plugin/snowplow/snowplow.rb, line 78
def page(user_id:, category: nil, name: nil, properties: nil, options: nil)
  super
  return unless enabled?

  # Log
  log = Itly::Loggers.vars_to_log(
    user_id: user_id, category: category, name: name, properties: properties, options: options
  )
  logger&.info "#{id}: page(#{log})"

  # Identify the user
  client.set_user_id user_id

  # Send through the client
  contexts = nil
  if options&.contexts.is_a?(Array) && options.contexts.any?
    contexts = options.contexts.collect(&:to_self_describing_json)
  end

  client.track_screen_view name, nil, contexts
end
track(user_id:, event:, options: nil) click to toggle source

Track an event

Raise an error if the client fails

@param [String] user_id: the id of the user in your application @param [Event] event: the Event object to pass to your application @param [Itly::Plugin::Snowplow::TrackOptions] options: the plugin specific options

Calls superclass method
# File lib/itly/plugin/snowplow/snowplow.rb, line 109
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, version: event&.version, properties: event&.properties, options: options
  )
  logger&.info "#{id}: track(#{log})"

  # Identify the user
  client.set_user_id user_id

  # Send through the client
  schema_version = event.version&.gsub(/\./, '-')
  schema = "iglu:#{vendor}/#{event.name}/jsonschema/#{schema_version}"

  event_json = SnowplowTracker::SelfDescribingJson.new(
    schema, event.properties
  )

  contexts = nil
  if options&.contexts.is_a?(Array) && options.contexts.any?
    contexts = options.contexts.collect(&:to_self_describing_json)
  end

  client.track_self_describing_event event_json, contexts
end

Private Instance Methods

enabled?() click to toggle source
# File lib/itly/plugin/snowplow/snowplow.rb, line 149
def enabled?
  !@disabled
end