Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

Commit

Permalink
Always include KM javascript, even if there are no queue items to emit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Morrison committed Jun 27, 2011
1 parent 57a7a3b commit 01a7137
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
30 changes: 13 additions & 17 deletions lib/snogmetrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,18 @@ def js!
# subsequent calls to #js will not send events that have already been sent.
def js(options={})
options = {:reset => false}.merge(options)

if queue.empty?
''
else
buffer = queue.map { |item| %(_kmq.push(#{item.to_json});) }

queue.clear if options[:reset]

<<-JAVASCRIPT
<script type="text/javascript">
var _kmq = _kmq || [];
#{api_js}
#{buffer.join("\n")}
</script>
JAVASCRIPT
end

buffer = queue.map { |item| %(_kmq.push(#{item.to_json});) }

queue.clear if options[:reset]

<<-JAVASCRIPT
<script type="text/javascript">
var _kmq = _kmq || [];
#{api_js}
#{buffer.join("\n")}
</script>
JAVASCRIPT
end

private
Expand Down Expand Up @@ -151,4 +147,4 @@ def api_js
end
end
end
end
end
12 changes: 6 additions & 6 deletions spec/snogmetrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ def self.production?
end

describe '#js' do
it 'outputs nothing if there are no events and no identity' do
@context.km.js.should be_empty
end

context 'in production' do
before do
Rails.stub!(:env).and_return(mock('env', :production? => true))
Expand All @@ -135,6 +131,10 @@ def self.production?
@context.km.js.should_not include('scripts.kissmetrics.com')
end
end

it 'outputs javascript even if there are no events and no identity' do
@context.km.js.should include('kmq')
end

it 'outputs code that conditionally sets the _kmq variable' do
@context.km.identify('Phil')
Expand All @@ -151,7 +151,7 @@ def self.production?
it 'resets the session when passed :reset => true' do
@context.km.record('hello world')
@context.km.js(:reset => true)
@context.km.js.should be_empty
@context.km.js.should_not include('hello world')
end

it 'does not let HTML slip through' do
Expand All @@ -164,7 +164,7 @@ def self.production?
it 'works like #js(:reset => true)' do
@context.km.record('hello world')
@context.km.js!
@context.km.js.should be_empty
@context.km.js!.should_not include('_kmq.push(["record"')
end

it 'does not push an identify call if the identity has already been sent once' do
Expand Down

0 comments on commit 01a7137

Please sign in to comment.