From f3ba99d324f36b1fff1929a1cc01df6f0f9ca5e2 Mon Sep 17 00:00:00 2001 From: Ryan Brewster Date: Wed, 29 Oct 2014 17:38:41 -0700 Subject: [PATCH] 1.0.6 * option namespacing --- lib/atrackt.console.js | 12 ++-- lib/atrackt.js | 4 ++ lib/plugins/atrackt.omniture.js | 33 ++++++----- spec/atrackt_spec.coffee | 72 ++++++++++++++++++++--- spec/plugins/atrackt.omniture_spec.coffee | 47 ++++++++------- src/README.md | 3 + src/atrackt.coffee | 4 ++ src/atrackt.console.coffee | 17 +++--- src/plugins/atrackt.omniture.coffee | 32 +++++----- 9 files changed, 151 insertions(+), 73 deletions(-) diff --git a/lib/atrackt.console.js b/lib/atrackt.console.js index 4a583fe..7f578e2 100644 --- a/lib/atrackt.console.js +++ b/lib/atrackt.console.js @@ -12,14 +12,16 @@ https://github.com/brewster1134/atrackt __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; (function(root, factory) { - if (location.href.indexOf('atracktConsole') > -1) { - if (typeof define === 'function' && define.amd) { - return define(['jquery', 'atrackt', 'jquery.scrollTo'], function($, Atrackt) { + if (typeof define === 'function' && define.amd) { + return define(['jquery', 'atrackt', 'jquery.scrollTo'], function($, Atrackt) { + if (location.href.indexOf('atracktConsole') > -1) { return $(function() { return window.Atrackt = new (factory($, Atrackt.constructor)); }); - }); - } else { + } + }); + } else { + if (location.href.indexOf('atracktConsole') > -1) { return window.Atrackt = new (factory(window.jQuery, window.Atrackt.constructor)); } } diff --git a/lib/atrackt.js b/lib/atrackt.js index 73c752c..c29c096 100644 --- a/lib/atrackt.js +++ b/lib/atrackt.js @@ -175,6 +175,10 @@ https://github.com/brewster1134/atrackt _results = []; for (pluginName in _ref) { pluginData = _ref[pluginName]; + if (options[pluginName]) { + $.extend(true, options, options[pluginName]); + delete options[pluginName]; + } if (event) { eventNamespace = event != null ? event.handleObj.namespace : void 0; if (eventNamespace === 'atrackt' || eventNamespace === ("atrackt." + pluginName)) { diff --git a/lib/plugins/atrackt.omniture.js b/lib/plugins/atrackt.omniture.js index deadeb1..9096731 100644 --- a/lib/plugins/atrackt.omniture.js +++ b/lib/plugins/atrackt.omniture.js @@ -18,11 +18,28 @@ https://github.com/brewster1134/atrackt } })(this, function($, Atrackt) { return window.Atrackt.setPlugin('omniture', { + options: { + trackingType: 'o', + charReplaceRegex: /[^\x20-\x7E]/g, + version: 14, + delimiters: { + linkName: '/', + category: '|' + }, + linkTrackVars: ['products', 'events'], + propMap: { + _location: 'prop1', + _categories: 'prop2', + _value: 'prop3', + _event: 'prop4' + } + }, send: function(data, options) { var arg, _ref, _ref1; if (typeof s === 'undefined') { return console.error('ATRACKT ERROR: PLUGIN `omniture` - Site catalyst library not loaded'); } + console.log('dataoptions', data, options); $.extend(true, this.options, options); data._categories = (_ref = data._categories) != null ? _ref.join(this.options.delimiters.category) : void 0; data = this._translatePropMap(data); @@ -35,22 +52,6 @@ https://github.com/brewster1134/atrackt } return data; }, - options: { - trackingType: 'o', - charReplaceRegex: /[^\x20-\x7E]/g, - version: 14, - delimiters: { - linkName: '/', - category: '|' - }, - linkTrackVars: ['products', 'events'], - propMap: { - _location: 'prop1', - _categories: 'prop2', - _value: 'prop3', - _event: 'prop4' - } - }, _buildSObject: function(obj) { var key, linkTrackVars, value; switch (this.options.version) { diff --git a/spec/atrackt_spec.coffee b/spec/atrackt_spec.coffee index d7d5724..7dd5b94 100644 --- a/spec/atrackt_spec.coffee +++ b/spec/atrackt_spec.coffee @@ -210,9 +210,6 @@ describe 'Atrackt', -> context 'when tracking an element', -> before -> $fooEl = $('') - $fooEl.data 'atrackt-function', (data, options) -> - data['function_data'] = true - options['function_option'] = true Atrackt.plugins['foo-plugin'].track $fooEl, track_option: true @@ -227,22 +224,40 @@ describe 'Atrackt', -> global_data: true plugin_data: true option_data: true - function_data: true , global_option: true plugin_option: true track_option: true - function_option: true context 'when tracking by event', -> before -> $fooEl = $('') + $('body').append $fooEl + + Atrackt.plugins['foo-plugin'].setEvent + click: $fooEl + + $fooEl.trigger 'click' + + it 'should call the send method on plugins with data and options', -> + expect(pluginSpy).to.be.calledWithExactly + _location: 'Atrackt Test' + _categories: ['Anchor'] + _value: 'Foo' + _event: 'click' + global_data: true + plugin_data: true + , + global_option: true + plugin_option: true + + context 'when tracking an element with a custom function', -> + before -> + $fooEl = $('') $fooEl.data 'atrackt-function', (data, options) -> data['function_data'] = true options['function_option'] = true - $('body').append $fooEl - Atrackt.plugins['foo-plugin'].setEvent click: $fooEl @@ -251,7 +266,7 @@ describe 'Atrackt', -> it 'should call the send method on plugins with data and options', -> expect(pluginSpy).to.be.calledWithExactly _location: 'Atrackt Test' - _categories: ['Anchor'] + _categories: [] _value: 'Foo' _event: 'click' global_data: true @@ -261,3 +276,44 @@ describe 'Atrackt', -> global_option: true plugin_option: true function_option: true + + context 'when passing options globally', -> + before -> + $fooEl = $('') + + Atrackt.track $fooEl, + global_option: 'global' + global_only: true + 'foo-plugin': + plugin_option: 'track-global-plugin-option' + global_option: 'track-global-plugin-overwrite-option' + + it 'should', -> + expect(pluginSpy).to.be.calledWithExactly + _location: 'Atrackt Test' + _categories: [] + _value: 'Foo' + global_data: true + plugin_data: true + , + global_only: true + global_option: 'track-global-plugin-overwrite-option' + plugin_option: 'track-global-plugin-option' + + context 'when passing options on a plugin', -> + before -> + $fooEl = $('') + + Atrackt.plugins['foo-plugin'].track $fooEl, + plugin_option: 'track|plugin-option' + + it 'should', -> + expect(pluginSpy).to.be.calledWithExactly + _location: 'Atrackt Test' + _categories: [] + _value: 'Foo' + global_data: true + plugin_data: true + , + global_option: true + plugin_option: 'track|plugin-option' diff --git a/spec/plugins/atrackt.omniture_spec.coffee b/spec/plugins/atrackt.omniture_spec.coffee index 9e5c8fd..812e63f 100644 --- a/spec/plugins/atrackt.omniture_spec.coffee +++ b/spec/plugins/atrackt.omniture_spec.coffee @@ -1,19 +1,8 @@ describe 'Plugin: Omniture', -> _plugin = Atrackt.plugins['omniture'] - describe '#buildSObject', -> - before -> - window.s = {} - _plugin.options.version = 14 - _plugin.options.linkTrackVars = ['foo'] - _plugin.buildSObject - bar: 'bar' - - it 'should add to the s object', -> - expect(s.foo).to_exist - - it 'shoule set linkTrackVars', -> - expect(s.linkTrackVars).to.equal 'foo,bar' + before -> + window.s = {} describe '#send', -> obj = null @@ -34,15 +23,21 @@ describe 'Plugin: Omniture', -> it 'should keep the original key if it does not exist in the propMap', -> expect(obj.bar).to.exist - describe '#keyLookup', -> + describe '#_buildSObject', -> before -> - _plugin.options.propMap = - foo: 'bar' + window.s = {} + _plugin.options.version = 14 + _plugin.options.linkTrackVars = ['foo'] + _plugin._buildSObject + bar: 'bar' - it 'should lookup from propMap', -> - expect(_plugin.keyLookup('foo')).to.equal 'bar' + it 'should add to the s object', -> + expect(s.foo).to_exist + + it 'shoule set linkTrackVars', -> + expect(s.linkTrackVars).to.equal 'foo,bar' - describe '#buildLinkName', -> + describe '#_buildLinkName', -> linkName = null before -> @@ -53,7 +48,7 @@ describe 'Plugin: Omniture', -> _location: 'prop2' _categories: 'prop3' - linkName = _plugin.buildLinkName + linkName = _plugin._buildLinkName prop1: 'baz' prop2: 'foo' prop3: 'bar' @@ -61,14 +56,22 @@ describe 'Plugin: Omniture', -> it 'should build a link name', -> expect(linkName).to.equal 'foo|bar|baz' - describe '#translatePropMap', -> + describe '#_translatePropMap', -> pre = null post = null before -> pre = integer: 10 - post = _plugin.translatePropMap pre + post = _plugin._translatePropMap pre it 'should handle any value type', -> expect(post['integer']).to.equal '10' + + describe '#_keyLookup', -> + before -> + _plugin.options.propMap = + foo: 'bar' + + it 'should lookup from propMap', -> + expect(_plugin._keyLookup('foo')).to.equal 'bar' diff --git a/src/README.md b/src/README.md index ef60591..020dd1c 100644 --- a/src/README.md +++ b/src/README.md @@ -1,4 +1,7 @@ ### CHANGE LOG +###### 1.0.6 +* option namespacing + ###### 1.0.5 * console scroll fix * console sets existing plugins on load diff --git a/src/atrackt.coffee b/src/atrackt.coffee index c9540f1..bc00e92 100644 --- a/src/atrackt.coffee +++ b/src/atrackt.coffee @@ -132,6 +132,10 @@ https://github.com/brewster1134/atrackt # Loop through each plugin and check if the data should be tracked for pluginName, pluginData of @plugins + # extract plugin specific data + if options[pluginName] + $.extend true, options, options[pluginName] + delete options[pluginName] # If tracking is triggered by an event, make sure the event namespace matches the plugin or is global if event diff --git a/src/atrackt.console.coffee b/src/atrackt.console.coffee index d1904b1..c87c47d 100644 --- a/src/atrackt.console.coffee +++ b/src/atrackt.console.coffee @@ -6,16 +6,17 @@ https://github.com/brewster1134/atrackt ### ((root, factory) -> - if location.href.indexOf('atracktConsole') > -1 - if typeof define == 'function' && define.amd - define [ - 'jquery' - 'atrackt' - 'jquery.scrollTo' - ], ($, Atrackt) -> + if typeof define == 'function' && define.amd + define [ + 'jquery' + 'atrackt' + 'jquery.scrollTo' + ], ($, Atrackt) -> + if location.href.indexOf('atracktConsole') > -1 $ -> window.Atrackt = new(factory($, Atrackt.constructor)) - else + else + if location.href.indexOf('atracktConsole') > -1 window.Atrackt = new(factory(window.jQuery, window.Atrackt.constructor)) ) @, ($, Atrackt) -> diff --git a/src/plugins/atrackt.omniture.coffee b/src/plugins/atrackt.omniture.coffee index d76f3a3..555036b 100644 --- a/src/plugins/atrackt.omniture.coffee +++ b/src/plugins/atrackt.omniture.coffee @@ -17,13 +17,31 @@ https://github.com/brewster1134/atrackt ) @, ($, Atrackt) -> window.Atrackt.setPlugin 'omniture', + + # default options + options: + trackingType: 'o' + charReplaceRegex: /[^\x20-\x7E]/g + version: 14 + delimiters: + linkName: '/' + category: '|' + linkTrackVars: ['products', 'events'] + propMap: + _location : 'prop1' + _categories : 'prop2' + _value : 'prop3' + _event : 'prop4' + send: (data, options) -> return console.error 'ATRACKT ERROR: PLUGIN `omniture` - Site catalyst library not loaded' if typeof s == 'undefined' + console.log 'dataoptions', data, options $.extend true, @options, options data._categories = data._categories?.join @options.delimiters.category data = @_translatePropMap data + @_buildSObject data if @options.page && s.t? s.t() @@ -35,20 +53,6 @@ https://github.com/brewster1134/atrackt s.tl arg, @options['trackingType'], @_buildLinkName data data - options: - trackingType: 'o' - charReplaceRegex: /[^\x20-\x7E]/g - version: 14 - delimiters: - linkName: '/' - category: '|' - linkTrackVars: ['products', 'events'] - propMap: - _location : 'prop1' - _categories : 'prop2' - _value : 'prop3' - _event : 'prop4' - # omniture specific _buildSObject: (obj) -> switch @options.version