Class: Itly::Plugin::Snowplow

Inherits:
Itly::Plugin show all
Defined in:
lib/itly/plugin/snowplow/snowplow.rb,
lib/itly/plugin/snowplow/context.rb,
lib/itly/plugin/snowplow/options.rb,
lib/itly/plugin/snowplow/version.rb,
lib/itly/plugin/snowplow/call_options.rb

Overview

Snowplow plugin class for Itly SDK

Defined Under Namespace

Classes: CallOptions, Context, Options, PageOptions, TrackOptions

Constant Summary collapse

VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vendor:, options:) ⇒ Snowplow

Instantiate a new Plugin::Snowplow

Parameters:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 20

def initialize(vendor:, options:)
  super()
  @vendor = vendor
  @disabled = options.disabled

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 12

def client
  @client
end

#disabledObject (readonly)

Returns the value of attribute disabled.



12
13
14
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 12

def disabled
  @disabled
end

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 12

def logger
  @logger
end

#vendorObject (readonly)

Returns the value of attribute vendor.



12
13
14
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 12

def vendor
  @vendor
end

Instance Method Details

#idString

Get the plugin ID

Returns:

  • (String)

    plugin id



145
146
147
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 145

def id
  'snowplow'
end

#identify(user_id:, properties: nil, options: nil) ⇒ Object

Identify a user

Raise an error if the client fails

Parameters:

  • user_id: (String)

    the id of the user in your application

  • properties: (Hash) (defaults to: nil)

    unused

  • options: (Itly::Plugin::Snowplow::IdentifyOptions) (defaults to: nil)

    the plugin specific options



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 57

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:) ⇒ Object

Initialize Snowplow plugin

Parameters:

  • options: (Itly::PluginOptions)

    plugin options



37
38
39
40
41
42
43
44
45
46
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 37

def load(options:)
  super
  # Get options
  @logger = options.logger

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

  logger&.info "#{id}: plugin is disabled!" if @disabled
end

#nameObject

Snowplow specific plugin options class for calls to plugin methods



54
55
56
57
58
59
60
61
# File 'lib/itly/plugin/snowplow/call_options.rb', line 54

%w[Identify Group Alias].each do |name|
  class_eval(
    <<-EVAL, __FILE__, __LINE__ + 1
      class #{name}Options < CallOptions         # class IdentifyOptions < CallOptions
      end                                        # end
    EVAL
  )
end

#page(user_id:, category: nil, name: nil, properties: nil, options: nil) ⇒ Object

Record page views

Raise an error if the client fails

Parameters:

  • user_id: (String)

    the id of the user in your application

  • category: (String) (defaults to: nil)

    the category of the page

  • name: (String) (defaults to: nil)

    the name of the page.

  • properties: (Hash) (defaults to: nil)

    the properties to pass to your application

  • options: (Itly::Plugin::Snowplow::PageOptions) (defaults to: nil)

    the plugin specific options



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 80

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: name, context: contexts
end

#track(user_id:, event:, options: nil) ⇒ Object

Track an event

Raise an error if the client fails

Parameters:

  • user_id: (String)

    the id of the user in your application

  • event: (Event)

    the Event object to pass to your application

  • options: (Itly::Plugin::Snowplow::TrackOptions) (defaults to: nil)

    the plugin specific options



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/itly/plugin/snowplow/snowplow.rb', line 111

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: event_json, context: contexts
end