the_tracker 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,98 @@
1
+ module TheTracker
2
+ module Trackers
3
+ class GAnalytics < Base
4
+
5
+ # Analytics uat
6
+ def initialize(options)
7
+ @options = options
8
+ super()
9
+ end
10
+
11
+ def name
12
+ :ganalytics
13
+ end
14
+
15
+ def add_transaction(tid=0, store='', total=0, tax=0, shipping=0, city='', state='', country='')
16
+ tid = Time.now.to_i if (tid.nil?) or (tid.to_s == '0')
17
+ @transaction = Transaction.new(tid, store, total, tax, shipping, city, state, country)
18
+ end
19
+
20
+ def add_transaction_item(sku='', product='', category='', price=0, quantity=0)
21
+ @transaction.add_item(sku, product, category, price, quantity)
22
+ end
23
+
24
+ def add_custom_var(index, name, value, scope)
25
+ custom_vars[index] = [name, value, scope]
26
+ end
27
+
28
+ def track_event(category, action, label='', value=0, non_interactive=false)
29
+ "_gaq.push(['_trackEvent', '#{category}', '#{action}', '#{label}', #{value}, #{non_interactive}]);"
30
+ end
31
+
32
+ def header
33
+ return if !active
34
+ <<-EOF
35
+ <script type="text/javascript">//<![CDATA[
36
+ var _gaq = _gaq || [];
37
+ _gaq.push(['_setAccount', '#{@options[:id]}']);
38
+ #{extra_conf}
39
+ _gaq.push(['_trackPageview']);
40
+ (function () {
41
+ var ga = document.createElement('script');
42
+ ga.type = 'text/javascript';
43
+ ga.async = true;
44
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
45
+ var s = document.getElementsByTagName('script')[0];
46
+ s.parentNode.insertBefore(ga, s);
47
+ })();
48
+ //]]></script>
49
+ EOF
50
+ end
51
+
52
+ private
53
+
54
+ def extra_conf
55
+ conf = ''
56
+ conf << "_gaq.push(['_setDomainName', '#{@options[:domain_name]}']);\n" if @options[:domain_name]
57
+ conf << "_gaq.push(['_setAllowLinker', #{@options[:allow_linker]}]);\n" if @options[:allow_linker]
58
+ conf << set_custom_vars
59
+ conf << set_transactions
60
+ conf
61
+ end
62
+
63
+ def set_custom_vars
64
+ custom_vars.map do | index, cv |
65
+ "_gaq.push(['_setCustomVar', #{index}, '#{cv[0]}', '#{cv[1]}', '#{cv[2]}']);"
66
+ end.join('\n')
67
+ end
68
+
69
+ def set_transactions
70
+ return '' unless @transaction
71
+ conf = "_gaq.push(['_addTrans', '#{@transaction.id}', '#{@transaction.store}', '#{@transaction.total}', '#{@transaction.tax}', '#{@transaction.shipping}', '#{@transaction.city}', '#{@transaction.state}', '#{@transaction.country}']);\n"
72
+ conf << @transaction.items.map do |item|
73
+ "_gaq.push(['_addItem', '#{@transaction.id}', '#{item.sku}', '#{item.product}', '#{item.category}', '#{item.price}', '#{item.quantity}']);"
74
+ end.join('\n')
75
+ conf << "_gaq.push(['_trackTrans']);"
76
+ @transaction = nil
77
+ conf
78
+ end
79
+
80
+ def custom_vars
81
+ @custom_vars ||= {}
82
+ end
83
+ end
84
+
85
+ class Item < Struct.new(:sku, :product, :category, :price, :quantity)
86
+ end
87
+
88
+ class Transaction < Struct.new(:id, :store, :total, :tax, :shipping, :city, :state, :country)
89
+ def add_item(sku, product, category, price, quantity)
90
+ items << Item.new(sku, product, category, price, quantity)
91
+ end
92
+
93
+ def items
94
+ @items ||= []
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,126 @@
1
+ module TheTracker
2
+ module Trackers
3
+ class GUniversal < Base
4
+
5
+ attr_accessor :name
6
+
7
+ # Universal Analytics
8
+ def initialize(options)
9
+ @name = options.delete(:name) || :guniversal
10
+ @options = options
11
+ super()
12
+ end
13
+
14
+ def add_transaction(tid=0, store='', total=0, tax=0, shipping=0)
15
+ tid = Time.now.to_i if (tid.nil?) or (tid.to_s == '0')
16
+ @transaction = EcommerceTransaction.new(tid, store, total, tax, shipping)
17
+ end
18
+
19
+ def add_transaction_item(sku='', product='', category='', price=0, quantity=0)
20
+ @transaction.add_item(sku, product, category, price, quantity)
21
+ end
22
+
23
+ def add_custom_var(type, index, value)
24
+ if (type == :dimension)
25
+ custom_dimensions[index] = value
26
+ else
27
+ custom_metrics[index] = value
28
+ end
29
+ end
30
+
31
+ def add_user_id(uid)
32
+ @uid = uid
33
+ end
34
+
35
+ def header
36
+ return if !active
37
+ <<-EOF
38
+ <!-- Google Analytics -->
39
+ <script>
40
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
41
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
42
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
43
+ })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
44
+ ga('create', '#{@options[:id]}', 'auto', {#{create_conf}});
45
+ ga('#{name}.send', 'pageview');
46
+ #{extra_conf}
47
+ </script>
48
+ <!-- End Google Analytics -->
49
+ EOF
50
+ end
51
+
52
+ private
53
+
54
+ def create_conf
55
+ [account_name, allow_linker, user_id].compact.join(', ')
56
+ end
57
+
58
+ def account_name
59
+ "'name': '#{name}'"
60
+ end
61
+
62
+ def allow_linker
63
+ "'allowLinker': true" if @options[:allow_linker]
64
+ end
65
+
66
+ def user_id
67
+ "'userId': '#{@uid}'" if @uid
68
+ end
69
+
70
+ def extra_conf
71
+ conf = ''
72
+ conf << "ga('#{name}.require', 'linker');\n"
73
+ conf << "ga('linker:autoLink', #{@options[:domain_name]});\n" if @options[:domain_name]
74
+ conf << set_custom_dimensions
75
+ conf << set_custom_metrics
76
+ conf << set_transactions
77
+ conf
78
+ end
79
+
80
+ def set_custom_dimensions
81
+ custom_dimensions.map do | index, value |
82
+ "ga('#{name}.set', 'dimension#{index}', '#{value}');"
83
+ end.join(' ')
84
+ end
85
+
86
+ def set_custom_metrics
87
+ custom_metrics.map do | index, value |
88
+ "ga('#{name}.set', 'metric#{index}', '#{value}');"
89
+ end.join(' ')
90
+ end
91
+
92
+ def set_transactions
93
+ return '' unless @transaction
94
+ conf = "ga('#{name}.require', 'ecommerce', 'ecommerce.js');\n"
95
+ conf << "ga('#{name}.ecommerce:addTransaction', { 'id': '#{@transaction.id}', 'affiliation': '#{@transaction.store}', 'revenue': '#{@transaction.total}', 'shipping': '#{@transaction.shipping}', 'tax': '#{@transaction.tax}' });\n"
96
+ conf << @transaction.items.map do |item|
97
+ "ga('#{name}.ecommerce:addItem', {'id': '#{@transaction.id}', 'name': '#{item.product}', 'sku': '#{item.sku}', 'category': '#{item.category}', 'price': '#{item.price}', 'quantity': '#{item.quantity}'});\n"
98
+ end.join('\n')
99
+ conf << "ga('#{name}.ecommerce:send');\n"
100
+ @transaction = nil
101
+ conf
102
+ end
103
+
104
+ def custom_dimensions
105
+ @custom_dimensions ||= {}
106
+ end
107
+
108
+ def custom_metrics
109
+ @custom_metrics ||= {}
110
+ end
111
+ end
112
+
113
+ class EcommerceItem < Struct.new(:sku, :product, :category, :price, :quantity)
114
+ end
115
+
116
+ class EcommerceTransaction < Struct.new(:id, :store, :total, :tax, :shipping)
117
+ def add_item(sku, product, category, price, quantity)
118
+ items << EcommerceItem.new(sku, product, category, price, quantity)
119
+ end
120
+
121
+ def items
122
+ @items ||= []
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,59 @@
1
+ module TheTracker
2
+ module Trackers
3
+ class Gtm < Base
4
+
5
+ attr_accessor :data_layer
6
+
7
+ # Gtm info
8
+ def initialize(options)
9
+ @gtmid = options[:gtmid]
10
+ super()
11
+ end
12
+
13
+ def name
14
+ :gtm
15
+ end
16
+
17
+ def body_top
18
+ return if !active
19
+ <<-EOF
20
+ #{show_data_layer}
21
+ <!-- Google Tag Manager -->
22
+ <noscript><iframe src="//www.googletagmanager.com/ns.html?id=#{@gtmid}"
23
+ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
24
+ <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
25
+ new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
26
+ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
27
+ '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
28
+ })(window,document,'script','dataLayer','#{@gtmid}');</script>
29
+ <!-- End Google Tag Manager -->
30
+ EOF
31
+ end
32
+
33
+ def add_data_layer(data, value)
34
+ data_layer[data] = value
35
+ end
36
+
37
+ private
38
+
39
+ def data_layer
40
+ @data_layer ||= {}
41
+ end
42
+
43
+ def show_data_layer
44
+ return if data_layer.empty?
45
+ <<-EOF
46
+ <script>
47
+ dataLayer = [{
48
+ #{data_layer_to_string}
49
+ }];
50
+ </script>
51
+ EOF
52
+ end
53
+
54
+ def data_layer_to_string
55
+ data_layer.map{|k,v| "'#{k}': '#{v}'"}.join(',')
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,49 @@
1
+ module TheTracker
2
+ module Trackers
3
+ class Kenshoo < Base
4
+
5
+ # Kenshoo info
6
+ def initialize(options)
7
+ @token = options[:token]
8
+ @type = options[:type]
9
+ @val = options[:val]
10
+ @orderId = options[:orderId]
11
+ @promoCode = options[:promoCode]
12
+ @valueCurrency = options[:valueCurrency]
13
+ @trackEvent = options[:trackEvent]
14
+ super()
15
+ end
16
+
17
+ def name
18
+ :kenshoo
19
+ end
20
+
21
+ def body_top
22
+ return if !active
23
+ <<-EOF
24
+ <script type=text/javascript>
25
+ var hostProtocol = (("https:" == document.location.protocol) ? "https" : "http");
26
+ document.write('<scr'+'ipt src="', hostProtocol+
27
+ '://#{@trackEvent}.xg4ken.com/media/getpx.php?cid=#{@id}','" type="text/JavaScript"><\/scr'+'ipt>');
28
+ </script>
29
+ <script type=text/javascript>
30
+ var params = new Array();
31
+ params[0]='id=#{@token}';
32
+ params[1]='type=#{@type}';
33
+ params[2]='val=#{@val}';
34
+ params[3]='orderId=#{@orderId}';
35
+ params[4]='promoCode=#{@promoCode}';
36
+ params[5]='valueCurrency=#{@valueCurrency}';
37
+ params[6]='GCID='; //For Live Tracking only
38
+ params[7]='kw='; //For Live Tracking only
39
+ params[8]='product='; //For Live Tracking only
40
+ k_trackevent(params,'#{@trackEvent}');
41
+ </script>
42
+ <noscript>
43
+ <img src="https://1191.xg4ken.com/media/redir.php?track=1&token=#{@token}&type=#{@type}&val=#{@val}&orderId=#{@orderId}&promoCode=#{@promoCode}&valueCurrency=#{@valueCurrency}&GCID=&kw=&product=" width="1" height="1">
44
+ </noscript>
45
+ EOF
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,25 @@
1
+ module TheTracker
2
+ module Trackers
3
+ class Relevant < Base
4
+
5
+ # Relevant info
6
+ def initialize(options)
7
+ @token = options[:token]
8
+ @seg = options[:seg ]
9
+ @orderId = options[:orderId]
10
+ super()
11
+ end
12
+
13
+ def name
14
+ :relevant
15
+ end
16
+
17
+ def body_bottom
18
+ return if !active
19
+ <<-EOF
20
+ <img src="http://ib.adnxs.com/px?id=#{@token}&seg=#{@seg}&orderId=#{@orderId}&t=2" width="1" height="1" />
21
+ EOF
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,39 @@
1
+ module TheTracker
2
+ module Trackers
3
+ class Uservoice < Base
4
+ require 'json'
5
+
6
+ # AdForm info pm and id
7
+ def initialize(key_file, options)
8
+ @options = options
9
+ @key_file = key_file
10
+ super()
11
+ end
12
+
13
+ def name
14
+ :uservoice
15
+ end
16
+
17
+ def header
18
+ return if !active
19
+ <<-EOF
20
+ <script type="text/javascript">
21
+ (function(){var uv=document.createElement('script');uv.type='text/javascript';uv.async=true;uv.src='//widget.uservoice.com/#{@key_file}.js';var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(uv,s)})()
22
+ </script>
23
+ EOF
24
+ end
25
+
26
+ def body_bottom
27
+ return if !active
28
+ <<-EOF
29
+ <script type="text/javascript">
30
+ UserVoice = window.UserVoice || [];
31
+ UserVoice.push(['showTab', 'classic_widget',
32
+ #{@options.to_json}
33
+ ]);
34
+ </script>
35
+ EOF
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module TheTracker
2
+ VERSION = "1.2.3"
3
+ end
@@ -0,0 +1,15 @@
1
+ module TheTracker
2
+ module ViewHelpers
3
+ def header_tracking_code
4
+ TheTracker::Tracker.instance.header
5
+ end
6
+
7
+ def body_top_tracking_code
8
+ TheTracker::Tracker.instance.body_top
9
+ end
10
+
11
+ def body_bottom_tracking_code
12
+ TheTracker::Tracker.instance.body_bottom
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ require 'the_tracker/version'
2
+ require 'the_tracker/tracker'
3
+ require 'the_tracker/view_helpers'
4
+ require 'the_tracker/trackers/base'
5
+ require 'the_tracker/railtie' if defined? Rails
6
+ Dir.glob("#{File.dirname(__FILE__)}/the_tracker/trackers/*.rb").each do |f|
7
+ require f
8
+ end
@@ -0,0 +1,15 @@
1
+ require 'the_tracker'
2
+
3
+ class TheTracker::Tracker
4
+ def self.reset
5
+ self.instance.instance_variable_set(:@trackers, {})
6
+ end
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+ config.order = 'random'
14
+ config.before { TheTracker::Tracker.reset }
15
+ end
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+
3
+ describe TheTracker::Tracker do
4
+ describe :methods do
5
+ describe :add do
6
+ it 'returns registered trackers' do
7
+ test_tr = mock('Object', :header => 'my tracker', :name => :track_test)
8
+ described_class.reset
9
+ described_class.config do |tmf|
10
+ tmf.add test_tr
11
+ end
12
+ test_tr2 = mock('Object', :header => 'other tracker', :name => :track_test2)
13
+ described_class.instance.add test_tr2
14
+ described_class.instance.trackers.size.should == 2
15
+ end
16
+ end
17
+
18
+ describe :add_once do
19
+ before :each do
20
+ test_tr = mock('Object', :body_bottom => 'my tracker', :name => :track_test)
21
+ described_class.reset
22
+ described_class.config do |tmf|
23
+ tmf.add test_tr
24
+ end
25
+ test_tr2 = mock('Object', :body_bottom => 'other tracker', :name => :track_test2)
26
+ described_class.instance.add_once test_tr2
27
+ end
28
+
29
+ it 'register a new tracker' do
30
+ described_class.instance.trackers.size.should == 2
31
+ end
32
+
33
+ it 'deregister the tracker after it has been injected on a page' do
34
+ described_class.instance.trackers.size.should == 2
35
+ described_class.instance.body_bottom
36
+ described_class.instance.trackers.size.should == 1
37
+ end
38
+ end
39
+
40
+ describe :header do
41
+ before :each do
42
+ ga = mock('Object', :header => 'google analytics', :name => :ganalytics)
43
+ tracker = TheTracker::Tracker.clone
44
+ @tracker = tracker.instance
45
+ @tracker.add ga
46
+ end
47
+
48
+ it 'returns all headers from trackers' do
49
+ @tracker.header.should == 'google analytics'
50
+ end
51
+
52
+ it 'adds headers of new tracker' do
53
+ sc = mock('Object', :header => 'site catalist', :name => :sitecat)
54
+ sc.stub(:header).and_return('site catalist')
55
+ @tracker.add sc
56
+ @tracker.header.should == "google analytics\nsite catalist"
57
+ end
58
+
59
+ it 'should not render nil trackers' do
60
+ ss = mock('Object', :header => 'site nil', :name => :stupid_site)
61
+ ss.stub(:header).and_return(nil)
62
+ @tracker.add ss
63
+ @tracker.header.should == 'google analytics'
64
+ end
65
+ end
66
+
67
+ describe :body_top do
68
+ before :each do
69
+ ga = mock('Object', :body_top => 'google analytics', :name => :ganalytics)
70
+ tracker = TheTracker::Tracker.clone
71
+ @tracker = tracker.instance
72
+ @tracker.add ga
73
+ end
74
+
75
+ it 'returns body_top from trackers' do
76
+ @tracker.body_top.should == 'google analytics'
77
+ end
78
+
79
+ it 'adds new tracker' do
80
+ sc = mock('Object', :body_top => 'site catalist', :name => :sitecat)
81
+ sc.stub(:body_top).and_return('site catalist')
82
+ @tracker.add sc
83
+ @tracker.body_top.should == "google analytics\nsite catalist"
84
+ end
85
+
86
+ it 'should not render nil trackers' do
87
+ ss = mock('Object', :body_top => 'site nil', :name => :stupid_site)
88
+ ss.stub(:body_top).and_return(nil)
89
+ @tracker.add ss
90
+ @tracker.body_top.should == 'google analytics'
91
+ end
92
+ end
93
+
94
+ describe :body_bottom do
95
+ before :each do
96
+ ga = mock('Object', :body_bottom => 'google analytics', :name => :ganalytics)
97
+ tracker = TheTracker::Tracker.clone
98
+ @tracker = tracker.instance
99
+ @tracker.add ga
100
+ end
101
+
102
+ it 'returns body_bottom from trackers' do
103
+ @tracker.body_bottom.should == 'google analytics'
104
+ end
105
+
106
+ it 'adds new tracker' do
107
+ sc = mock('Object', :body_bottom => 'site catalist', :name => :sitecat)
108
+ sc.stub(:body_bottom).and_return('site catalist')
109
+ @tracker.add sc
110
+ @tracker.body_bottom.should == "google analytics\nsite catalist"
111
+ end
112
+
113
+ it 'should not render nil trackers' do
114
+ ss = mock('Object', :body_bottom => 'site nil', :name => :stupid_site)
115
+ ss.stub(:body_bottom).and_return(nil)
116
+ @tracker.add ss
117
+ @tracker.body_bottom.should == 'google analytics'
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe TheTracker::Trackers::AdForm do
4
+ subject { described_class.new(:pm => '111666', :id => '333555') }
5
+ describe :methods do
6
+ describe :header do
7
+ it 'should return ad_form content' do
8
+ subject.header.should include('https://track.adform.net/serving/scripts/trackpoint/async/')
9
+ end
10
+
11
+ it 'should include pm and id information' do
12
+ subject.header.should include('pm: 111666')
13
+ subject.header.should include('id: 333555')
14
+ end
15
+
16
+ it 'returns nothing if tracker not active' do
17
+ subject.active = false
18
+ subject.header.should == nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe TheTracker::Trackers::Base do
4
+ describe :methods do
5
+ describe :header do
6
+ it 'should return an empty array' do
7
+ subject.header.should == []
8
+ end
9
+ end
10
+
11
+ describe :body_top do
12
+ it 'should return an empty array' do
13
+ subject.body_top.should == []
14
+ end
15
+ end
16
+
17
+ describe :body_bottom do
18
+ it 'should return an empty array' do
19
+ subject.body_bottom.should == []
20
+ end
21
+ end
22
+
23
+ describe :name do
24
+ it 'should raise a NotImplementedError' do
25
+ expect { subject.name }.to raise_error(NotImplementedError)
26
+ end
27
+ end
28
+
29
+ describe :active do
30
+ it 'should be active by default' do
31
+ subject.active.should be
32
+ end
33
+
34
+ it 'should be active by default' do
35
+ subject.active = false
36
+ subject.active.should be_false
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe TheTracker::Trackers::GAdServices do
4
+ subject { described_class.new(:id => 12345678, :language => 'en', :format => '2', :color => "ffffff", :label => 'SomeThingReallyCool', :value => 0) }
5
+ describe :methods do
6
+ describe :body_bottom do
7
+ it 'should return ad_form content' do
8
+ subject.body_bottom.should include('//www.googleadservices.com/pagead/conversion.js')
9
+ end
10
+
11
+ it 'should include id, langage, format, color, label and value information' do
12
+ subject.body_bottom.should include('var google_conversion_id = 12345678');
13
+ subject.body_bottom.should include("var google_conversion_language = \"en\"");
14
+ subject.body_bottom.should include("var google_conversion_format = \"2\"");
15
+ subject.body_bottom.should include("var google_conversion_color = \"ffffff\"");
16
+ subject.body_bottom.should include("var google_conversion_label = \"SomeThingReallyCool\"");
17
+ subject.body_bottom.should include('var google_conversion_value = 0');
18
+ end
19
+
20
+ it 'returns nothing if tracker not active' do
21
+ subject.active = false
22
+ subject.body_bottom.should == nil
23
+ end
24
+ end
25
+ end
26
+ end