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

Commit

Permalink
Added click tracking and tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Mattsson authored and iconara committed Sep 2, 2010
1 parent 78b26f3 commit d7735c6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/snogmetrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ def identify(identity)
end
end

# The equivalent of the `KM.trackClick` method of the JavaScript API. The first
# argument should be a selector (a tag id or class name). Furthermore you can
# pass either an event name, an event name and a hash of properties, or only
# a hash of properties.
def trackClick(selector, *args)
raise 'Not enough arguments' if args.size == 0
raise 'Too many arguments' if args.size > 2

queue << ['trackClick', selector, *args]
end

# Register which variant the user saw in an A/B test.
def set(experiment, variant)
queue << ['set', experiment, variant]
Expand Down
29 changes: 29 additions & 0 deletions spec/snogmetrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ def self.production?
end
end

describe '#trackClick' do
it 'will output code that pushes an event with the specified name and properties' do
@context.km.trackClick('tagid', 'hello world', :foo => 'bar')
@context.km.js.should include('_kmq.push(["trackClick","tagid","hello world",{"foo":"bar"}]);')
end

it 'will output code that pushes an event with name only' do
@context.km.trackClick('tagid', 'foo')
@context.km.js.should include('_kmq.push(["trackClick","tagid","foo"]);')
end

it 'will output code that pushes an event with properties only' do
@context.km.trackClick('tagid', {:foo => 'bar', :plink => :plonk})
@context.km.js.should match(%r#_kmq.push\(\["trackClick","tagid",\{(?:"foo":"bar","plink":"plonk"|"plink":"plonk","foo":"bar")\}\]\)#)
end

it 'complains if called without arguments' do
running { @context.km.trackClick }.should raise_error
end

it 'complains if called with selector only' do
running { @context.km.trackClick('tagid') }.should raise_error
end

it 'complains if called with more than three arguments' do
running { @context.km.trackClick('tagid', 'hello world', {:foo => 'bar'}, '12') }.should raise_error
end
end

describe '#identify' do
it 'will output code that pushes an identify call with the provided identity' do
@context.km.identify('Phil')
Expand Down

0 comments on commit d7735c6

Please sign in to comment.