Skip to content

Commit

Permalink
Add tests and clean up dtm adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
claubacher authored and Chris Laubacher committed Oct 4, 2016
1 parent 0eb51a4 commit 762bedc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
37 changes: 9 additions & 28 deletions addon/metrics-adapters/dynamic-tag-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,27 @@ import BaseAdapter from './base';
const {
assert,
get,
set
set,
A: emberArray
} = Ember;
const {
compact
} = objectTransforms;

export default BaseAdapter.extend({

eventQueue: [],

toStringExtension() {
return 'DynamicTagManager';
},

init () {
init() {
const config = get(this, 'config');
const { src } = config;

assert(`[ember-metrics] You must pass a valid \`src\` to the ${this.toString()} adapter`, src);
set(this, 'dtmSrc', src);

set(this, 'dataLayerNameString', config['dataLayerName'] || 'dtmDataLayer');
window[get(this, 'dataLayerNameString')] = {};
let dataLayerNameString = set(this, 'dataLayerNameString', config['dataLayerName'] || 'dtmDataLayer');
window[dataLayerNameString] = {};

if (canUseDOM) {
/* jshint ignore:start */
Expand All @@ -39,7 +37,7 @@ export default BaseAdapter.extend({
/* jshint ignore:end */
}

this.checkForQueue();
this.eventQueue = emberArray();
},

pushAndCheck(dtmEvent) {
Expand All @@ -48,7 +46,6 @@ export default BaseAdapter.extend({
},

checkForQueue() {

if (this.get('eventQueue').length) {
if (this.isSatelliteDefined()) {
this.sendEvents();
Expand All @@ -70,27 +67,12 @@ export default BaseAdapter.extend({
this.set('eventQueue', []);
},

dtmObject(compactedOptions) {

const dtmEvent = {'event': compactedOptions['event']};
delete compactedOptions['event'];
for (let key in compactedOptions) {
dtmEvent[`${key}`] = compactedOptions[key];
}
return dtmEvent;

},

isSatelliteDefined() {
if (typeof(window._satellite) === "object") {
return true;
} else {
return false;
}
return typeof(window._satellite) === "object";
},

trackEvent(options = {}) {
const dtmEvent = this.dtmObject(compact(options));
const dtmEvent = compact(options);
this.pushAndCheck(dtmEvent);
return dtmEvent;
},
Expand All @@ -102,7 +84,7 @@ export default BaseAdapter.extend({
},

trackPage(options = {}) {
const dtmEvent = this.dtmObject(compact(options));
const dtmEvent = compact(options);
dtmEvent['event'] = 'vpv';
this.pushAndCheck(dtmEvent);
return dtmEvent;
Expand All @@ -112,5 +94,4 @@ export default BaseAdapter.extend({
delete window['_satellite'];
delete window[get(this, 'dataLayerNameString')];
}

});
1 change: 0 additions & 1 deletion addon/metrics-adapters/google-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default BaseAdapter.extend({

init() {
const config = copy(get(this, 'config'));
console.log(config);
const { id } = config;

assert(`[ember-metrics] You must pass a valid \`id\` to the ${this.toString()} adapter`, id);
Expand Down
38 changes: 34 additions & 4 deletions tests/unit/metrics-adapters/dynamic-tag-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,41 @@ moduleFor('ember-metrics@metrics-adapter:dynamic-tag-manager', 'dynamic-tag-mana
config = {
src: '//path.com/to.src.js'
};
window._satellite = { track() {} };
},
afterEach() {
sandbox.restore();
}
});

// Replace this with your real tests.
test('it exists', function(assert) {
var adapter = this.subject({ config });
assert.ok(adapter);
test('#trackEvent calls `_satellite.track`', function(assert) {
const adapter = this.subject({ config });
const stub = sandbox.stub(window._satellite, 'track');

const event = 'click-button';
const eventData = {
event,
category: 'button',
action: 'click',
label: 'nav buttons',
value: 4
};

const result = adapter.trackEvent(eventData);

assert.ok(stub.calledWith(event), 'it sends the right argument');
assert.deepEqual(result, eventData, 'it returns the right response');
});

test('#trackPage calls `_satellite.track`', function(assert) {
const adapter = this.subject({ config });
const stub = sandbox.stub(window._satellite, 'track');

const event = 'vpv';
const eventData = { event };

const result = adapter.trackPage();

assert.ok(stub.calledWith(event), 'it sends the right argument');
assert.deepEqual(result, eventData, 'it returns the right response');
});

0 comments on commit 762bedc

Please sign in to comment.