Class: EventTracker::SegmentIo

Inherits:
Tracker
  • Object
show all
Defined in:
lib/event_tracker/segment_io.rb

Constant Summary collapse

JS_ESCAPE_MAP =
{
    '\\'    => '\\\\',
    '</'    => '<\/',
    "\r\n"  => '\n',
    "\n"    => '\n',
    "\r"    => '\n',
    '"'     => '\\"',
    "'"     => "\\'"
}

Instance Method Summary collapse

Methods inherited from Tracker

#set_options

Constructor Details

#initialize(options = {}) ⇒ SegmentIo



14
15
16
# File 'lib/event_tracker/segment_io.rb', line 14

def initialize(options = {})
  @key = options[:key]
end

Instance Method Details

#add_properties(properties = nil) ⇒ Object



40
41
42
# File 'lib/event_tracker/segment_io.rb', line 40

def add_properties(properties = nil)
  %Q{analytics.track('set', #{ruby_hash_to_js(properties)});}
end

#create_alias(identity1, identity2) ⇒ Object



36
37
38
# File 'lib/event_tracker/segment_io.rb', line 36

def create_alias(identity1, identity2)
  %Q{analytics.alias('#{identity1}', '#{identity2}');}
end

#create_alias_for_identity(identity1, identity2) ⇒ Object



73
74
75
76
77
78
# File 'lib/event_tracker/segment_io.rb', line 73

def create_alias_for_identity(identity1, identity2)
  return if EventTracker.disabled?
  if identity1.present? && identity2.present?
    client.alias(from: identity1, to: identity2)
  end
end

#identify(identity = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/event_tracker/segment_io.rb', line 26

def identify(identity = nil)
  if identity.present? && identity.has_key?(:id)
    %Q{analytics.identify('#{identity[:id]}');}
  elsif identity.present?
    %Q{analytics.identify(#{ruby_hash_to_js(identity)});}
  else
    nil
  end
end

#identify_for_identity(identity, with_info = false) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/event_tracker/segment_io.rb', line 64

def identify_for_identity(identity, with_info = false)
  return if EventTracker.disabled?
  if identity.present? && identity.has_key?(:id)
    client.identify({
        user_id: "#{identity[:id]}",
    }.merge_if(with_info, { traits: camelize_hash(identity.except(:id)) }))
  end
end

#initObject



18
19
20
21
22
23
24
# File 'lib/event_tracker/segment_io.rb', line 18

def init
  <<-EOD
  window.analytics=window.analytics||[],window.analytics.methods=["identify","group","track","page","pageview","alias","ready","on","once","off","trackLink","trackForm","trackClick","trackSubmit"],window.analytics.factory=function(t){return function(){var a=Array.prototype.slice.call(arguments);return a.unshift(t),window.analytics.push(a),window.analytics}};for(var i=0;i<window.analytics.methods.length;i++){var key=window.analytics.methods[i];window.analytics[key]=window.analytics.factory(key)}window.analytics.load=function(t){if(!document.getElementById("analytics-js")){var a=document.createElement("script");a.type="text/javascript",a.id="analytics-js",a.async=!0,a.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.io/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n)}},window.analytics.SNIPPET_VERSION="2.0.9",
  window.analytics.load("#{@key}");
  window.analytics.page();
  EOD
end

#track_event(event_name, properties) ⇒ Object



55
56
57
58
# File 'lib/event_tracker/segment_io.rb', line 55

def track_event(event_name, properties)
  p = properties.empty? ? "" : ", #{ruby_hash_to_js(properties.except(:analytics))}"
  %Q{analytics.track('#{event_name}'#{p});}
end

#track_event_for_identity(identity, event_name, properties = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/event_tracker/segment_io.rb', line 80

def track_event_for_identity(identity, event_name, properties = {})
  return if EventTracker.disabled?
  if identity.present? && identity.has_key?(:id)
    client.track(
        user_id: "#{identity[:id]}",
        event: "#{event_name}",
        properties: camelize_hash(properties.except(:analytics))
    )
  end
end

#track_pageview(name = nil, category = nil, properties = {}, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/event_tracker/segment_io.rb', line 44

def track_pageview(name = nil, category = nil, properties = {}, options = {})
  p = properties.empty? ? "" : ", #{ruby_hash_to_js(properties)}"
  if category.present? && name.present?
    %Q{analytics.page('#{category}', '#{name}'#{p});}
  elsif name.present?
    %Q{analytics.page('#{name}'#{p});}
  else
    %Q{analytics.page('');}
  end
end

#track_transaction(event_name, properties = {}) ⇒ Object



60
61
62
# File 'lib/event_tracker/segment_io.rb', line 60

def track_transaction(event_name, properties = {})
  track_event(event_name, properties)
end

#track_transaction_for_identity(identity, event_name, properties = {}) ⇒ Object



91
92
93
# File 'lib/event_tracker/segment_io.rb', line 91

def track_transaction_for_identity(identity, event_name, properties = {})
  track_event_for_identity(identity, event_name, properties)
end