diff --git a/CHANGELOG.md b/CHANGELOG.md index ce937d0..009840a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/build/wildberry_princess.js b/build/wildberry_princess.js index da51f3a..b8d4a1b 100644 --- a/build/wildberry_princess.js +++ b/build/wildberry_princess.js @@ -60,18 +60,16 @@ }; WildberryPrincess.prototype.clickHandler = function(event) { - var element, eventParams, label, payload, ref; + var element, eventParams, label, payload; if (!event) { return; } element = event.target; - eventParams = (ref = element.data) != null ? ref.eventParams : void 0; - if (!eventParams) { + if (!element) { return; } - if (!(label = eventParams.label)) { - label = this.getLabel(element); - } + eventParams = element.data.eventParams; + label = eventParams.label ? eventParams.label : this.getLabel(element); if (this.settings.useGoogleAnalytics) { payload = { hitType: 'event', @@ -87,7 +85,6 @@ this.sendPayloadGA(payload); } if (this.settings.useKissMetrics) { - label = eventParams.category + " " + label + " " + eventParams.action; payload = { category: eventParams.category, action: eventParams.action @@ -98,7 +95,7 @@ if (eventParams.value) { payload.value = eventParams.value; } - this.trackEventKM(label, payload); + this.trackEventKM(eventParams.category + ": " + label + " (" + eventParams.action + ")", payload); } }; @@ -108,7 +105,6 @@ this.trackEventGA(category, action, label, value); } if (this.settings.useKissMetrics) { - label = category + " " + label + " " + action; payload = { category: category, action: action @@ -119,7 +115,7 @@ if (value) { payload.value = value; } - return this.trackEventKM(label, payload); + return this.trackEventKM(category + ": " + label + " (" + action + ")", payload); } }; diff --git a/package.json b/package.json index 7f1d8ce..7efea91 100644 --- a/package.json +++ b/package.json @@ -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 ", "contributors": [ diff --git a/src/wildberry_princess.coffee b/src/wildberry_princess.coffee index e44e401..ae842f8 100644 --- a/src/wildberry_princess.coffee +++ b/src/wildberry_princess.coffee @@ -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 = @@ -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 @@ -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 = diff --git a/test/wildberry_princess_spec.coffee b/test/wildberry_princess_spec.coffee index 5ce8a9e..53fbb64 100644 --- a/test/wildberry_princess_spec.coffee +++ b/test/wildberry_princess_spec.coffee @@ -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') @@ -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 @@ -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 @@ -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 } ] @@ -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') @@ -264,7 +277,7 @@ 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) @@ -272,6 +285,11 @@ describe 'WildberryPrincess', -> 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 diff --git a/test/wildberry_princess_spec.js b/test/wildberry_princess_spec.js index 9ba7140..2f9d9c6 100644 --- a/test/wildberry_princess_spec.js +++ b/test/wildberry_princess_spec.js @@ -113,6 +113,9 @@ button.setAttribute('data-event-label', 'First Button'); return document.body.appendChild(button); }); + afterEach(function() { + return button.parentNode.removeChild(button); + }); it('should track clicks on elements', function() { var click_event; wbp.trackUserActions('button', 'Buttons'); @@ -131,10 +134,10 @@ }; ga_spy.should.have.been.calledWith('send', payload); payload = [ - "record", "Buttons First Button Click", { + "record", "Buttons: First Button (Click)", { action: "Click", category: "Buttons", - label: "Buttons First Button Click" + label: "First Button" } ]; return kmq_spy.should.have.been.calledWith(payload); @@ -157,10 +160,9 @@ }; ga_spy.should.have.been.calledWith('send', payload); payload = [ - "record", "Buttons null Click", { + "record", "Buttons: null (Click)", { action: "Click", - category: "Buttons", - label: "Buttons null Click" + category: "Buttons" } ]; return kmq_spy.should.have.been.calledWith(payload); @@ -185,10 +187,10 @@ ga_spy.should.have.been.calledWith('send', payload); kmq_spy.should.have.been.calledOnce; payload = [ - "record", "Buttons First Button Click", { + "record", "Buttons: First Button (Click)", { action: "Click", category: "Buttons", - label: "Buttons First Button Click", + label: "First Button", value: 1 } ]; @@ -202,6 +204,17 @@ ga_spy.should.not.have.been.calledOnce; return kmq_spy.should.not.have.been.calledOnce; }); + it('should return when there is no eventParams', function() { + var click_event; + 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; + return kmq_spy.should.not.have.been.calledOnce; + }); it('should not call GA when useGoogleAnalytics is false', function() { var click_event; wbp.settings.useGoogleAnalytics = false; @@ -257,14 +270,14 @@ it('should call trackEventKM with the correct params', function() { track_event_km_spy.callCount.should.equal(0); wbp.trackEvent('a', 'b', 'c', 'd'); - return track_event_km_spy.should.have.been.calledWith('a c b', { + return track_event_km_spy.should.have.been.calledWith('a: c (b)', { action: "b", category: "a", - label: "a c b", + label: "c", value: "d" }); }); - return it('should not call trackEventKM when KM is disabled', function() { + it('should not call trackEventKM when KM is disabled', function() { wbp = new WildberryPrincess({ useKissMetrics: false }); @@ -272,6 +285,14 @@ wbp.trackEvent('a', 'b', 'c', 'd'); return track_event_km_spy.callCount.should.equal(0); }); + return it('should exclude label and value when not set', function() { + track_event_km_spy.callCount.should.equal(0); + wbp.trackEvent('a', 'b'); + return track_event_km_spy.should.have.been.calledWith('a: undefined (b)', { + action: "b", + category: "a" + }); + }); }); describe('#trackEventGA', function() { var wbp_spy;