class SnowplowTracker::Tracker

Public Class Methods

new(emitters, subject=nil, namespace=nil, app_id=nil, encode_base64=@@default_encode_base64) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 69
def initialize(emitters, subject=nil, namespace=nil, app_id=nil, encode_base64=@@default_encode_base64)
  @emitters = Array(emitters)
  if subject.nil?
    @subject = Subject.new
  else
    @subject = subject
  end
  @standard_nv_pairs = {
    'tna' => namespace,
    'tv'  => @@version,
    'aid' => app_id
  }
  @config = {
    'encode_base64' => encode_base64
  }

  self
end

Public Instance Methods

add_emitter(emitter) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 359
def add_emitter(emitter)
  @emitters.push(emitter)
  self
end
flush(async=false) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 340
def flush(async=false)
  @emitters.each do |emitter|
    emitter.flush(async)
  end

  self
end
get_event_id() click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 100
def get_event_id()
  SecureRandom.uuid
end
set_subject(subject) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 351
def set_subject(subject)
  @subject = subject
  self
end
track_ecommerce_transaction(transaction, items, context=nil, tstamp=nil) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 194
def track_ecommerce_transaction(transaction, 
                                items,
                                context=nil,
                                tstamp=nil)      
  if tstamp.nil?
    tstamp = get_timestamp
  end

  track_ecommerce_transaction(transaction, items, context, DeviceTimestamp.new(tstamp))
end
track_page_view(page_url, page_title=nil, referrer=nil, context=nil, tstamp=nil) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 139
def track_page_view(page_url, page_title=nil, referrer=nil, context=nil, tstamp=nil)
  if tstamp.nil?
    tstamp = get_timestamp
  end

  track_page_view(page_url, page_title, referrer, context, DeviceTimestamp.new(tstamp))  
end
track_screen_view(name=nil, id=nil, context=nil, tstamp=nil) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 273
def track_screen_view(name=nil, id=nil, context=nil, tstamp=nil)
  screen_view_properties = {}
  unless name.nil? 
    screen_view_properties['name'] = name
  end
  unless id.nil? 
    screen_view_properties['id'] = id
  end
  screen_view_schema = "#{@@base_schema_path}/screen_view/#{@@schema_tag}/1-0-0"

  event_json = SelfDescribingJson.new(screen_view_schema, screen_view_properties)

  self.track_unstruct_event(event_json, context, tstamp)

  self
end
track_self_describing_event(event_json, context=nil, tstamp=nil) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 293
def track_self_describing_event(event_json, context=nil, tstamp=nil)
  track_unstruct_event(event_json, context, tstamp)
end
track_struct_event(category, action, label=nil, property=nil, value=nil, context=nil, tstamp=nil) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 242
def track_struct_event(category, action, label=nil, property=nil, value=nil, context=nil, tstamp=nil)
  if tstamp.nil?
    tstamp = get_timestamp
  end

  track_struct_event(category, action, label, property, value, context, DeviceTimestamp.new(tstamp))
end
track_unstruct_event(event_json, context=nil, tstamp=nil) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 307
def track_unstruct_event(event_json, context=nil, tstamp=nil)
  if tstamp.nil?
    tstamp = get_timestamp
  end

  track_unstruct_event(event_json, context, DeviceTimestamp.new(tstamp))
end

Private Instance Methods

build_context(context) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 114
def build_context(context)
  SelfDescribingJson.new(
    @@context_schema,
    context.map {|c| c.to_json}
    ).to_json
end
get_timestamp() click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 107
def get_timestamp
  (Time.now.to_f * 1000).to_i
end
track(pb) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 127
def track(pb)
  pb.add_dict(@subject.standard_nv_pairs)
  pb.add_dict(@standard_nv_pairs)
  pb.add('eid', get_event_id())
  @emitters.each{ |emitter| emitter.input(pb.context)}

  nil
end
track_ecommerce_transaction_item(argmap) click to toggle source
# File lib/snowplow-tracker/tracker.rb, line 172
def track_ecommerce_transaction_item(argmap)
  pb = Payload.new
  pb.add('e', 'ti')
  pb.add('ti_id', argmap['order_id'])
  pb.add('ti_sk', argmap['sku'])
  pb.add('ti_pr', argmap['price'])
  pb.add('ti_qu', argmap['quantity'])
  pb.add('ti_nm', argmap['name'])
  pb.add('ti_ca', argmap['category'])
  pb.add('ti_cu', argmap['currency'])
  unless argmap['context'].nil?
    pb.add_json(build_context(argmap['context']), @config['encode_base64'], 'cx', 'co')
  end
  pb.add(argmap['tstamp'].type, argmap['tstamp'].value)
  track(pb)

  self
end