analytical 1.1.1 → 1.2.0

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 1.2.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{analytical}
8
- s.version = "1.1.1"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joshua Krall"]
@@ -87,6 +87,7 @@ Gem::Specification.new do |s|
87
87
  "lib/analytical/kiss_metrics.rb",
88
88
  "rails/init.rb",
89
89
  "spec/analytical/api_spec.rb",
90
+ "spec/analytical/base_spec.rb",
90
91
  "spec/analytical/bot_detector_spec.rb",
91
92
  "spec/analytical/clicky_spec.rb",
92
93
  "spec/analytical/google_spec.rb",
@@ -103,6 +104,7 @@ Gem::Specification.new do |s|
103
104
  s.summary = %q{Gem for managing multiple analytics services in your rails app.}
104
105
  s.test_files = [
105
106
  "spec/analytical/api_spec.rb",
107
+ "spec/analytical/base_spec.rb",
106
108
  "spec/analytical/bot_detector_spec.rb",
107
109
  "spec/analytical/clicky_spec.rb",
108
110
  "spec/analytical/google_spec.rb",
@@ -9,21 +9,21 @@ module Analytical
9
9
  end
10
10
 
11
11
  def init_javascript(location)
12
- return '' unless init_location?(location)
13
-
14
- @initializing = true
15
- html = "<!-- Analytical Init: Google Adwords -->\n"
16
- event_commands = []
17
- @commands.each do |c|
18
- if c[0] == :event
19
- event_commands << event(*c[1..-1])
12
+ init_location(location) do
13
+ @initializing = true
14
+ html = "<!-- Analytical Init: Google Adwords -->\n"
15
+ event_commands = []
16
+ @commands.each do |c|
17
+ if c[0] == :event
18
+ event_commands << event(*c[1..-1])
19
+ end
20
20
  end
21
- end
22
- html += event_commands.join("\n")
23
- @commands = @commands.delete_if {|c| c[0] == :event }
24
- @initializing = false
21
+ html += event_commands.join("\n")
22
+ @commands = @commands.delete_if {|c| c[0] == :event }
23
+ @initializing = false
25
24
 
26
- html
25
+ html
26
+ end
27
27
  end
28
28
 
29
29
  #
@@ -70,7 +70,7 @@ module Analytical
70
70
  def tracking_javascript(location)
71
71
  commands = []
72
72
  @modules.each do |name, m|
73
- commands += m.process_queued_commands if m.tracking_command_location==location
73
+ commands += m.process_queued_commands if m.init_location?(location) || m.initialized
74
74
  end
75
75
  commands = commands.delete_if{|c| c.blank? || c.empty?}
76
76
  unless commands.empty?
@@ -1,13 +1,14 @@
1
1
  module Analytical
2
2
  module Base
3
3
  module Api
4
- attr_reader :tracking_command_location, :parent, :options
4
+ attr_reader :tracking_command_location, :parent, :options, :initialized
5
5
  attr_accessor :commands
6
6
 
7
7
  def initialize(_parent, _options={})
8
8
  @parent = _parent
9
9
  @options = _options
10
10
  @tracking_command_location = :body_prepend
11
+ @initialized = false
11
12
  @commands = []
12
13
  end
13
14
 
@@ -29,7 +30,7 @@ module Analytical
29
30
  def set(data); ''; end
30
31
 
31
32
  # This method generates the initialization javascript that an analytics service uses to track your site
32
- def init_javascript(location); {}; end
33
+ def init_javascript(location); ''; end
33
34
 
34
35
  def queue(*args)
35
36
  if args.first==:identify
@@ -48,6 +49,19 @@ module Analytical
48
49
  @tracking_command_location==location
49
50
  end
50
51
 
52
+ def init_location(location, &block)
53
+ if init_location?(location)
54
+ @initialized = true
55
+ if block_given?
56
+ yield
57
+ else
58
+ ''
59
+ end
60
+ else
61
+ ''
62
+ end
63
+ end
64
+
51
65
  end
52
66
  end
53
67
  end
@@ -9,27 +9,27 @@ module Analytical
9
9
  end
10
10
 
11
11
  def init_javascript(location)
12
- return '' unless init_location?(location)
13
-
14
- protocol = options[:ssl] ? 'https' : 'http'
15
-
16
- js = <<-HTML
17
- <!-- Analytical Init: Clicky -->
18
- <script src="#{protocol}://static.getclicky.com/js" type="text/javascript"></script>
19
- <script type="text/javascript">clicky.init('#{@options[:key]}');</script>
20
- <noscript><p><img alt="Clicky" width="1" height="1" src="#{protocol}://in.getclicky.com/#{@options[:key]}ns.gif" /></p></noscript>
21
- HTML
22
-
23
- identify_commands = []
24
- @commands.each do |c|
25
- if c[0] == :identify
26
- identify_commands << identify(*c[1..-1])
12
+ init_location(location) do
13
+ protocol = options[:ssl] ? 'https' : 'http'
14
+
15
+ js = <<-HTML
16
+ <!-- Analytical Init: Clicky -->
17
+ <script src="#{protocol}://static.getclicky.com/js" type="text/javascript"></script>
18
+ <script type="text/javascript">clicky.init('#{@options[:key]}');</script>
19
+ <noscript><p><img alt="Clicky" width="1" height="1" src="#{protocol}://in.getclicky.com/#{@options[:key]}ns.gif" /></p></noscript>
20
+ HTML
21
+
22
+ identify_commands = []
23
+ @commands.each do |c|
24
+ if c[0] == :identify
25
+ identify_commands << identify(*c[1..-1])
26
+ end
27
27
  end
28
- end
29
- js = identify_commands.join("\n") + "\n" + js
30
- @commands = @commands.delete_if {|c| c[0] == :identify }
28
+ js = identify_commands.join("\n") + "\n" + js
29
+ @commands = @commands.delete_if {|c| c[0] == :identify }
31
30
 
32
- js
31
+ js
32
+ end
33
33
  end
34
34
 
35
35
  def track(*args)
@@ -9,14 +9,15 @@ module Analytical
9
9
  end
10
10
 
11
11
  def init_javascript(location)
12
- return '' unless init_location?(location)
13
- js = <<-HTML
14
- <!-- Analytical Init: Console -->
15
- <script type="text/javascript">
16
- console.log('Analytical Init: Console');
17
- </script>
18
- HTML
19
- js
12
+ init_location(location) do
13
+ js = <<-HTML
14
+ <!-- Analytical Init: Console -->
15
+ <script type="text/javascript">
16
+ console.log('Analytical Init: Console');
17
+ </script>
18
+ HTML
19
+ js
20
+ end
20
21
  end
21
22
 
22
23
  def track(*args)
@@ -9,14 +9,15 @@ module Analytical
9
9
  end
10
10
 
11
11
  def init_javascript(location)
12
- return '' unless init_location?(location)
13
- code_url = "#{options[:key][0,4]}/#{options[:key][4,4]}"
14
- protocol = options[:ssl] ? 'https' : 'http'
15
- js = <<-HTML
16
- <!-- Analytical Init: CrazyEgg -->
17
- <script type="text/javascript" src="#{protocol}://s3.amazonaws.com/new.cetrk.com/pages/scripts/#{code_url}.js"> </script>
18
- HTML
19
- js
12
+ init_location(location) do
13
+ code_url = "#{options[:key][0,4]}/#{options[:key][4,4]}"
14
+ protocol = options[:ssl] ? 'https' : 'http'
15
+ js = <<-HTML
16
+ <!-- Analytical Init: CrazyEgg -->
17
+ <script type="text/javascript" src="#{protocol}://s3.amazonaws.com/new.cetrk.com/pages/scripts/#{code_url}.js"> </script>
18
+ HTML
19
+ js
20
+ end
20
21
  end
21
22
 
22
23
  end
@@ -9,20 +9,21 @@ module Analytical
9
9
  end
10
10
 
11
11
  def init_javascript(location)
12
- return '' unless init_location?(location)
13
- js = <<-HTML
14
- <!-- Analytical Init: Google -->
15
- <script type="text/javascript">
16
- var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
17
- document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
18
- </script>
19
- <script type="text/javascript">
20
- var googleAnalyticsTracker = _gat._getTracker("#{options[:key]}");
21
- googleAnalyticsTracker._initData();
22
- googleAnalyticsTracker._trackPageview();
23
- </script>
24
- HTML
25
- js
12
+ init_location(location) do
13
+ js = <<-HTML
14
+ <!-- Analytical Init: Google -->
15
+ <script type="text/javascript">
16
+ var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
17
+ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
18
+ </script>
19
+ <script type="text/javascript">
20
+ var googleAnalyticsTracker = _gat._getTracker("#{options[:key]}");
21
+ googleAnalyticsTracker._initData();
22
+ googleAnalyticsTracker._trackPageview();
23
+ </script>
24
+ HTML
25
+ js
26
+ end
26
27
  end
27
28
 
28
29
  def track(*args)
@@ -9,17 +9,18 @@ module Analytical
9
9
  end
10
10
 
11
11
  def init_javascript(location)
12
- return '' unless init_location?(location)
13
- js = <<-HTML
14
- <!-- Analytical Init: Hubspot -->
15
- <script type="text/javascript" language="javascript">
16
- var hs_portalid=#{options[:portal_id]};
17
- var hs_salog_version = "2.00";
18
- var hs_ppa = "#{options[:domain]}";
19
- document.write(unescape("%3Cscript src='" + document.location.protocol + "//" + hs_ppa + "/salog.js.aspx' type='text/javascript'%3E%3C/script%3E"));
20
- </script>
21
- HTML
22
- js
12
+ init_location(location) do
13
+ js = <<-HTML
14
+ <!-- Analytical Init: Hubspot -->
15
+ <script type="text/javascript" language="javascript">
16
+ var hs_portalid=#{options[:portal_id]};
17
+ var hs_salog_version = "2.00";
18
+ var hs_ppa = "#{options[:domain]}";
19
+ document.write(unescape("%3Cscript src='" + document.location.protocol + "//" + hs_ppa + "/salog.js.aspx' type='text/javascript'%3E%3C/script%3E"));
20
+ </script>
21
+ HTML
22
+ js
23
+ end
23
24
  end
24
25
 
25
26
  end
@@ -9,18 +9,19 @@ module Analytical
9
9
  end
10
10
 
11
11
  def init_javascript(location)
12
- return '' unless init_location?(location)
13
- js = <<-HTML
14
- <!-- Analytical Init: KissMetrics -->
15
- <script type="text/javascript">
16
- var _kmq = _kmq || [];
17
- (function(){function _kms(u,d){if(navigator.appName.indexOf("Microsoft")==0 && d)document.write("<scr"+"ipt defer='defer' async='true' src='"+u+"'></scr"+"ipt>");else{var s=document.createElement('script');s.type='text/javascript';s.async=true;s.src=u;(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);}}_kms('http' + ('https:' == document.location.protocol ? 's://': '://') + 'i.kissmetrics.com/i.js', 1);_kms('http'+('https:'==document.location.protocol ? 's://s3.amazonaws.com/' : '://')+'scripts.kissmetrics.com/#{options[:key]}.1.js',1);})();
18
- </script>
19
- <script type="text/javascript">
20
- _kmq.push(['pageView']);
21
- </script>
22
- HTML
23
- js
12
+ init_location(location) do
13
+ js = <<-HTML
14
+ <!-- Analytical Init: KissMetrics -->
15
+ <script type="text/javascript">
16
+ var _kmq = _kmq || [];
17
+ (function(){function _kms(u,d){if(navigator.appName.indexOf("Microsoft")==0 && d)document.write("<scr"+"ipt defer='defer' async='true' src='"+u+"'></scr"+"ipt>");else{var s=document.createElement('script');s.type='text/javascript';s.async=true;s.src=u;(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);}}_kms('http' + ('https:' == document.location.protocol ? 's://': '://') + 'i.kissmetrics.com/i.js', 1);_kms('http'+('https:'==document.location.protocol ? 's://s3.amazonaws.com/' : '://')+'scripts.kissmetrics.com/#{options[:key]}.1.js',1);})();
18
+ </script>
19
+ <script type="text/javascript">
20
+ _kmq.push(['pageView']);
21
+ </script>
22
+ HTML
23
+ js
24
+ end
24
25
  end
25
26
 
26
27
  def identify(id, *args)
@@ -8,8 +8,8 @@ describe "Analytical::Api" do
8
8
  Analytical::Google::Api.should_receive(:new).and_return(@google = mock('google'))
9
9
  a = Analytical::Api.new :modules=>[:console, :google]
10
10
  a.modules.should == {
11
- :console=>@console,
12
- :google=>@google,
11
+ :console=>@console,
12
+ :google=>@google,
13
13
  }
14
14
  end
15
15
  end
@@ -18,47 +18,47 @@ describe "Analytical::Api" do
18
18
  before(:each) do
19
19
  Analytical::Console::Api.stub!(:new).and_return(@console = mock('console'))
20
20
  Analytical::Google::Api.stub!(:new).and_return(@google = mock('google'))
21
- Analytical::Clicky::Api.stub!(:new).and_return(@clicky = mock('clicky'))
22
-
21
+ Analytical::Clicky::Api.stub!(:new).and_return(@clicky = mock('clicky'))
22
+
23
23
  @api = Analytical::Api.new :modules=>[:console, :google]
24
24
  end
25
25
 
26
26
  describe '#track' do
27
27
  it 'should store the #track command for each module api class' do
28
28
  @api = Analytical::Api.new :modules=>[:console, :google, :clicky]
29
-
29
+
30
30
  @console.should_receive(:queue).with(:track, 'something', {:a=>1, :b=>2})
31
31
  @clicky.should_receive(:queue).with(:track, 'something', {:a=>1, :b=>2})
32
32
  @google.should_receive(:queue).with(:track, 'something', {:a=>1, :b=>2})
33
-
33
+
34
34
  @api.track('something', {:a=>1, :b=>2})
35
35
  end
36
36
  end
37
-
37
+
38
38
  describe '#identify' do
39
39
  it 'should store the #track command for each module api class' do
40
40
  @api = Analytical::Api.new :modules=>[:console, :google, :clicky]
41
-
41
+
42
42
  @console.should_receive(:queue).with(:identify, 'something', {:a=>1, :b=>2})
43
43
  @clicky.should_receive(:queue).with(:identify, 'something', {:a=>1, :b=>2})
44
44
  @google.should_receive(:queue).with(:identify, 'something', {:a=>1, :b=>2})
45
-
45
+
46
46
  @api.identify('something', {:a=>1, :b=>2})
47
47
  end
48
48
  end
49
-
49
+
50
50
  describe '#now' do
51
51
  it 'should call a command on each module and collect the results' do
52
- @api = Analytical::Api.new :modules=>[:console, :google, :clicky]
52
+ @api = Analytical::Api.new :modules=>[:console, :google, :clicky]
53
53
 
54
54
  @console.should_receive(:track).with('something', {:a=>1, :b=>2}).and_return('console track')
55
55
  @clicky.should_receive(:track).with('something', {:a=>1, :b=>2}).and_return('clicky track')
56
56
  @google.should_receive(:track).with('something', {:a=>1, :b=>2}).and_return('google track')
57
-
57
+
58
58
  @api.now.track('something', {:a=>1, :b=>2}).should == "console track\ngoogle track\nclicky track"
59
59
  end
60
60
  end
61
-
61
+
62
62
  describe 'when accessing a module by name' do
63
63
  it 'should return the module api object' do
64
64
  @api = Analytical::Api.new :modules=>[:console, :google, :clicky]
@@ -67,12 +67,14 @@ describe "Analytical::Api" do
67
67
  @api.google.should == @google
68
68
  end
69
69
  end
70
-
70
+
71
71
  describe 'gathering javascript' do
72
72
  before(:each) do
73
- @console.stub!(:tracking_command_location).and_return(:body_prepend)
73
+ @console.stub!(:init_location?).and_return(false)
74
+ @console.stub!(:initialized).and_return(false)
74
75
  @console.stub!(:process_queued_commands).and_return([])
75
- @google.stub!(:tracking_command_location).and_return(:body_prepend)
76
+ @google.stub!(:init_location?).and_return(false)
77
+ @google.stub!(:initialized).and_return(false)
76
78
  @google.stub!(:process_queued_commands).and_return([])
77
79
  end
78
80
  describe '#head_javascript' do
@@ -91,19 +93,23 @@ describe "Analytical::Api" do
91
93
  end
92
94
  describe '#body_append_javascript' do
93
95
  it 'should return the javascript' do
94
- @console.should_receive(:init_javascript).with(:body_append).and_return('console_c')
95
- @google.should_receive(:init_javascript).with(:body_append).and_return('google_c')
96
+ @console.should_receive(:init_javascript).with(:body_append).and_return('console_c')
97
+ @google.should_receive(:init_javascript).with(:body_append).and_return('google_c')
96
98
  @api.body_append_javascript.should == "console_c\ngoogle_c"
97
99
  end
98
100
  end
99
101
  describe 'with stored commands' do
100
102
  before(:each) do
103
+ @console.stub!(:init_location?).and_return(true)
104
+ @console.stub!(:initialized).and_return(false)
101
105
  @console.stub!(:track).and_return('console track called')
102
106
  @console.stub!(:queue)
103
- @console.stub!(:process_queued_commands).and_return(['console track called'])
107
+ @console.stub!(:process_queued_commands).and_return(['console track called'])
108
+ @google.stub!(:init_location?).and_return(false)
109
+ @google.stub!(:initialized).and_return(true)
104
110
  @google.stub!(:track).and_return('google track called')
105
- @google.stub!(:queue)
106
- @google.stub!(:process_queued_commands).and_return(['google track called'])
111
+ @google.stub!(:queue)
112
+ @google.stub!(:process_queued_commands).and_return(['google track called'])
107
113
  @api.track('something', {:a=>1, :b=>2})
108
114
  end
109
115
  describe '#body_prepend_javascript' do
@@ -0,0 +1,96 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Analytical::Base::Api do
4
+
5
+ class BaseApiDummy
6
+ include Analytical::Base::Api
7
+ end
8
+
9
+ describe '#queue' do
10
+ before(:each) do
11
+ @api = BaseApiDummy.new(mock('parent'))
12
+ @api.commands = [:a, :b, :c]
13
+ end
14
+ describe 'with an identify command' do
15
+ it 'should store it at the head of the command list' do
16
+ @api.queue :identify, 'someone', {:some=>:args}
17
+ @api.commands.should == [[:identify, 'someone', {:some=>:args}], :a, :b, :c]
18
+ end
19
+ end
20
+ describe 'with any other command' do
21
+ it 'should store it at the end of the command list' do
22
+ @api.queue :other, {:some=>:args}
23
+ @api.commands.should == [:a, :b, :c, [:other, {:some=>:args}]]
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#process_queued_commands' do
29
+ before(:each) do
30
+ @api = BaseApiDummy.new(mock('parent'))
31
+ @api.commands = [[:a, 1, 2, 3], [:b, {:some=>:args}]]
32
+ @api.stub!(:a).and_return('a')
33
+ @api.stub!(:b).and_return('b')
34
+ end
35
+ it 'should send each of the args arrays in the command list' do
36
+ @api.should_receive(:a).with(1, 2, 3).and_return('a')
37
+ @api.should_receive(:b).with({:some=>:args}).and_return('b')
38
+ @api.process_queued_commands
39
+ end
40
+ it 'should return the results as an array' do
41
+ @api.process_queued_commands.should == ['a', 'b']
42
+ end
43
+ it 'should clear the commands list' do
44
+ @api.process_queued_commands
45
+ @api.commands == []
46
+ end
47
+ end
48
+
49
+ describe '#init_location?' do
50
+ before(:each) do
51
+ @api = BaseApiDummy.new(mock('parent'))
52
+ @api.instance_variable_set '@tracking_command_location', :my_location
53
+ end
54
+ describe 'when the command location matches the init location' do
55
+ it 'should return true' do
56
+ @api.init_location?(:my_location).should be_true
57
+ end
58
+ end
59
+ describe 'when the command location does not match the init location' do
60
+ it 'should return false' do
61
+ @api.init_location?(:not_my_location).should be_false
62
+ end
63
+ end
64
+ end
65
+
66
+ describe '#init_location' do
67
+ before(:each) do
68
+ @api = BaseApiDummy.new(mock('parent'))
69
+ end
70
+ it 'should check for the init_location' do
71
+ @api.should_receive(:init_location?).with(:some_location).and_return(false)
72
+ @api.init_location(:some_location)
73
+ end
74
+ describe 'for a valid init location' do
75
+ before(:each) { @api.stub!(:init_location?).and_return(true) }
76
+ it 'should set initialized to true' do
77
+ @api.init_location(:some_location)
78
+ @api.initialized.should be_true
79
+ end
80
+ it 'should yield to the block and return its result' do
81
+ @api.init_location(:some_location) do
82
+ 'result from the block'
83
+ end.should == 'result from the block'
84
+ end
85
+ end
86
+ describe 'not for an init location' do
87
+ before(:each) do
88
+ @api.stub!(:init_location?).and_return(false)
89
+ end
90
+ it 'should return an empty string' do
91
+ @api.init_location(:not_my_init_location).should == ''
92
+ end
93
+ end
94
+ end
95
+
96
+ end
@@ -35,7 +35,7 @@ describe "Analytical::KissMetrics::Api" do
35
35
  describe '#set' do
36
36
  it 'should return a js string' do
37
37
  @api = Analytical::KissMetrics::Api.new @parent, {:key=>'abcdef'}
38
- @api.set({:something=>'good', :b=>2}).should == "_kmq.push([\"set\", {\"something\":\"good\",\"b\":2}]);"
38
+ @api.set({:something=>'good', :b=>2}).should == "_kmq.push([\"set\", #{{:something=>'good', :b=>2}.to_json}]);"
39
39
  end
40
40
  end
41
41
  describe '#init_javascript' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: analytical
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 1
9
- - 1
10
- version: 1.1.1
8
+ - 2
9
+ - 0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Krall
@@ -128,6 +128,7 @@ files:
128
128
  - lib/analytical/kiss_metrics.rb
129
129
  - rails/init.rb
130
130
  - spec/analytical/api_spec.rb
131
+ - spec/analytical/base_spec.rb
131
132
  - spec/analytical/bot_detector_spec.rb
132
133
  - spec/analytical/clicky_spec.rb
133
134
  - spec/analytical/google_spec.rb
@@ -172,6 +173,7 @@ specification_version: 3
172
173
  summary: Gem for managing multiple analytics services in your rails app.
173
174
  test_files:
174
175
  - spec/analytical/api_spec.rb
176
+ - spec/analytical/base_spec.rb
175
177
  - spec/analytical/bot_detector_spec.rb
176
178
  - spec/analytical/clicky_spec.rb
177
179
  - spec/analytical/google_spec.rb