Skip to content

Commit

Permalink
v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewCallis committed Jul 7, 2015
1 parent 5ba4085 commit 6ec73d2
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 35 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.2.1 (July 6th, 2015)

* Simplify some branching logic.
* Better handing of labels for Kissmetrics.

## 1.2 (July 6th, 2015)

* Added support for Kissmetrics.
Expand Down
16 changes: 6 additions & 10 deletions build/wildberry_princess.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wildberry-princess",
"version": "1.2.0",
"version": "1.2.1",
"description": "JavaScript library for abstracting out Google Analytics (analytics.js) and Kissmetrics.",
"author": "Matthew Callis <Matthew Callis>",
"contributors": [
Expand Down
12 changes: 5 additions & 7 deletions src/wildberry_princess.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class WildberryPrincess
clickHandler: (event) =>
return unless event
element = event.target
eventParams = element.data?.eventParams
return unless element

return unless eventParams
eventParams = element.data.eventParams

label = @getLabel(element) unless label = eventParams.label
label = if eventParams.label then eventParams.label else @getLabel(element)

if @settings.useGoogleAnalytics
payload =
Expand All @@ -49,14 +49,13 @@ class WildberryPrincess
@sendPayloadGA payload

if @settings.useKissMetrics
label = "#{eventParams.category} #{label} #{eventParams.action}"
payload =
category: eventParams.category
action: eventParams.action
payload.label = label if label
payload.value = eventParams.value if eventParams.value

@trackEventKM label, payload
@trackEventKM "#{eventParams.category}: #{label} (#{eventParams.action})", payload

return

Expand All @@ -65,14 +64,13 @@ class WildberryPrincess
@trackEventGA(category, action, label, value)

if @settings.useKissMetrics
label = "#{category} #{label} #{action}"
payload =
category: category
action: action
payload.label = label if label
payload.value = value if value

@trackEventKM label, payload
@trackEventKM "#{category}: #{label} (#{action})", payload

trackEventGA: (category, action, label, value) =>
payload =
Expand Down
32 changes: 25 additions & 7 deletions test/wildberry_princess_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ describe 'WildberryPrincess', ->
button.setAttribute('data-event-label', 'First Button')
document.body.appendChild button

afterEach ->
button.parentNode.removeChild(button)

it 'should track clicks on elements', ->
wbp.trackUserActions('button', 'Buttons')

Expand All @@ -129,11 +132,11 @@ describe 'WildberryPrincess', ->
ga_spy.should.have.been.calledWith 'send', payload
payload = [
"record",
"Buttons First Button Click",
"Buttons: First Button (Click)",
{
action: "Click"
category: "Buttons"
label: "Buttons First Button Click"
label: "First Button"
}
]
kmq_spy.should.have.been.calledWith payload
Expand All @@ -158,11 +161,10 @@ describe 'WildberryPrincess', ->
ga_spy.should.have.been.calledWith 'send', payload
payload = [
"record"
"Buttons null Click"
"Buttons: null (Click)"
{
action: "Click"
category: "Buttons"
label: "Buttons null Click"
}
]
kmq_spy.should.have.been.calledWith payload
Expand All @@ -189,11 +191,11 @@ describe 'WildberryPrincess', ->
kmq_spy.should.have.been.calledOnce
payload = [
"record"
"Buttons First Button Click"
"Buttons: First Button (Click)"
{
action: "Click"
category: "Buttons"
label: "Buttons First Button Click"
label: "First Button"
value: 1
}
]
Expand All @@ -208,6 +210,17 @@ describe 'WildberryPrincess', ->
ga_spy.should.not.have.been.calledOnce
kmq_spy.should.not.have.been.calledOnce

it 'should return when there is no eventParams', ->
click_event = document.createEvent('HTMLEvents')
click_event.initEvent('click', true, false)
wbp.clickHandler(click_event)

wbp_click_spy.should.have.been.calledOnce
ga_send_spy.should.not.have.been.calledOnce
km_send_spy.should.not.have.been.calledOnce
ga_spy.should.not.have.been.calledOnce
kmq_spy.should.not.have.been.calledOnce

it 'should not call GA when useGoogleAnalytics is false', ->
wbp.settings.useGoogleAnalytics = false
wbp.trackUserActions('button', 'Buttons')
Expand Down Expand Up @@ -264,14 +277,19 @@ describe 'WildberryPrincess', ->
it 'should call trackEventKM with the correct params', ->
track_event_km_spy.callCount.should.equal 0
wbp.trackEvent 'a', 'b', 'c', 'd'
track_event_km_spy.should.have.been.calledWith 'a c b', { action: "b", category: "a", label: "a c b", value: "d" }
track_event_km_spy.should.have.been.calledWith 'a: c (b)', { action: "b", category: "a", label: "c", value: "d" }

it 'should not call trackEventKM when KM is disabled', ->
wbp = new WildberryPrincess(useKissMetrics: false)
track_event_km_spy.callCount.should.equal 0
wbp.trackEvent 'a', 'b', 'c', 'd'
track_event_km_spy.callCount.should.equal 0

it 'should exclude label and value when not set', ->
track_event_km_spy.callCount.should.equal 0
wbp.trackEvent 'a', 'b'
track_event_km_spy.should.have.been.calledWith 'a: undefined (b)', { action: "b", category: "a" }

describe '#trackEventGA', ->
wbp_spy = null

Expand Down
41 changes: 31 additions & 10 deletions test/wildberry_princess_spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6ec73d2

Please sign in to comment.