snogmetrics 0.1.7 → 0.1.8

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/lib/snogmetrics.rb CHANGED
@@ -10,26 +10,45 @@ require 'snogmetrics/railtie' if defined? Rails::Railtie
10
10
  # You should override #kissmetrics_api_key in an initializer to set the
11
11
  # KISSmetrics API key.
12
12
  #
13
- # You can also override #use_fake_kissmetrics_api? to provide your own logic for
14
- # when the real KISSmetrics API and when the fake should be used. The fake API
15
- # simply outputs all events to the console (if the console is defined). The
16
- # the default implementation outputs the real API only when
17
- # `Rails.env.production?` is true.
13
+ # You can also override #output_strategy to provide your own logic for
14
+ # when the real KISSmetrics API, console.log fake, and array fake should be used.
15
+ # The console_log output strategy outputs all events to the console (if the console is defined).
16
+ # The array output strategy simply logs all events in the _kmq variable.
17
+ # The live output strategy sends calls to the async KISSmetrics JS API.
18
+ #
19
+ # The default implementation outputs the real API only when
20
+ # `Rails.env.production?` is true, and otherwise uses console.log
18
21
  module Snogmetrics
19
- VERSION = '0.1.7'
22
+ VERSION = '0.1.8'
20
23
 
21
24
  # Returns an instance of KissmetricsApi, which is an interface to the
22
25
  # KISSmetrics API. It has the methods #record and #identify, which work just
23
26
  # like the corresponding methods in the JavaScript API.
24
27
  def km
25
- @km_api ||= KissmetricsApi.new(kissmetrics_api_key, session, use_fake_kissmetrics_api?)
28
+ @km_api ||= KissmetricsApi.new(kissmetrics_api_key, session, output_strategy)
26
29
  end
27
30
 
28
31
  # Override this method to set the KISSmetrics API key
29
32
  def kissmetrics_api_key
30
33
  ''
31
34
  end
35
+
36
+ # Override this method to set the output strategy.
37
+ # Available return values:
38
+ #
39
+ # :console_log use console.log to display events pushed to KISSmetrics
40
+ # :array store events pushed to KISSmetrics on _kmq
41
+ # :live send events to KISSmetrics via the async JS API
42
+ def output_strategy
43
+ if use_fake_kissmetrics_api?
44
+ :console_log
45
+ else
46
+ :live
47
+ end
48
+ end
32
49
 
50
+ # Deprecated: Prefer overriding #output_strategy to control the output strategy.
51
+ #
33
52
  # Override this method to customize when the real API and when the stub API
34
53
  # will be outputted.
35
54
  def use_fake_kissmetrics_api?
@@ -39,16 +58,17 @@ module Snogmetrics
39
58
  false
40
59
  end
41
60
  end
61
+
42
62
 
43
63
  private
44
64
 
45
65
  class KissmetricsApi
46
66
  # Do not instantiate KissmetricsApi yourself, instead mix in Snogmetrics
47
67
  # and use it's #km method to get an instance of KissmetricsApi.
48
- def initialize(api_key, session, fake_it)
49
- @session = session
50
- @api_key = api_key
51
- @fake_it = fake_it
68
+ def initialize(api_key, session, output_strategy)
69
+ @session = session
70
+ @api_key = api_key
71
+ @output_strategy = output_strategy
52
72
  end
53
73
 
54
74
  # The equivalent of the `KM.record` method of the JavaScript API. You can
@@ -97,9 +117,9 @@ private
97
117
  #
98
118
  # Returns the JavaScript needed to send the current state to KISSmetrics.
99
119
  # This includes the a JavaScript tag that loads the API code (or a fake API
100
- # that sends events to the console if the `fake_it` parameter to #initialize
101
- # is true), as well as statements that push the current state onto the
102
- # `_kmq` array.
120
+ # that sends events to the console or a global array if the `output_strategy`
121
+ # parameter to #initialize is :console_log or :array), as well as statements
122
+ # that push the current state onto the `_kmq` array.
103
123
  #
104
124
  # You can pass the option `:reset` to reset the state, this makes sure that
105
125
  # subsequent calls to #js will not send events that have already been sent.
@@ -126,7 +146,7 @@ private
126
146
  end
127
147
 
128
148
  def api_js
129
- if @fake_it
149
+ if @output_strategy == :console_log
130
150
  <<-JS
131
151
  if (window.console) {
132
152
  _kmq = (function(queue) {
@@ -142,8 +162,12 @@ private
142
162
  })(_kmq);
143
163
  }
144
164
  JS
145
- else
165
+ elsif @output_strategy == :live
146
166
  %((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('https://i.kissmetrics.com/i.js');_kms('http'+('https:'==document.location.protocol ? 's://s3.amazonaws.com/' : '://')+'scripts.kissmetrics.com/#{@api_key}.1.js',1);})();)
167
+ elsif @output_strategy == :array
168
+ ""
169
+ else
170
+ raise "Unknown KISSmetrics output strategy: #{@output_strategy}"
147
171
  end
148
172
  end
149
173
  end
@@ -132,6 +132,45 @@ describe Snogmetrics do
132
132
  end
133
133
  end
134
134
 
135
+ context 'overriding #output_strategy with :console_log' do
136
+ it 'outputs calls to console.log' do
137
+ Rails.stub!(:env).and_return(mock('env', :production? => true))
138
+ @context.stub!(:output_strategy).and_return(:console_log)
139
+ @context.km.identify('Joyce')
140
+ @context.km.js.should_not include('scripts.kissmetrics.com')
141
+ @context.km.js.should include('console.dir')
142
+ end
143
+ end
144
+
145
+ context 'overriding #output_strategy with :array' do
146
+ it 'just stores calls in the _kmq array' do
147
+ Rails.stub!(:env).and_return(mock('env', :production? => true))
148
+ @context.stub!(:output_strategy).and_return(:array)
149
+ @context.km.identify('Joyce')
150
+ @context.km.js.should_not include('scripts.kissmetrics.com')
151
+ @context.km.js.should_not include('console.dir')
152
+ end
153
+ end
154
+
155
+ context 'overriding #output_strategy with :live' do
156
+ it 'sends calls to KISSmetrics' do
157
+ Rails.stub!(:env).and_return(mock('env', :production? => true))
158
+ @context.stub!(:output_strategy).and_return(:live)
159
+ @context.km.identify('Joyce')
160
+ @context.km.js.should include('scripts.kissmetrics.com')
161
+ @context.km.js.should_not include('console.dir')
162
+ end
163
+ end
164
+
165
+ context 'overriding #output_strategy with something else' do
166
+ it 'raises' do
167
+ @context.stub!(:output_strategy).and_return(:something_else)
168
+ lambda {
169
+ @context.km.js.should include('scripts.kissmetrics.com')
170
+ }.should raise_error(RuntimeError, "Unknown KISSmetrics output strategy: something_else")
171
+ end
172
+ end
173
+
135
174
  it 'outputs javascript even if there are no events and no identity' do
136
175
  @context.km.js.should include('kmq')
137
176
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: snogmetrics
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.7
5
+ version: 0.1.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Theo Hultberg
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-27 00:00:00 +02:00
13
+ date: 2011-06-30 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency