Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove event prefix from top-level keys in GoogleTagManager adapter #446

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions addon/metrics-adapters/google-tag-manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assert, deprecate } from '@ember/debug';
import { capitalize } from '@ember/string';
import { assert } from '@ember/debug';
import { compact } from 'ember-metrics/-private/utils/object-transforms';
import removeFromDOM from 'ember-metrics/-private/utils/remove-from-dom';
import BaseAdapter from './base';
Expand Down Expand Up @@ -44,30 +43,10 @@ export default class GoogleTagManager extends BaseAdapter {

trackEvent(options = {}) {
const compactedOptions = compact(options);
const dataLayer = this.dataLayer;
const gtmEvent = { event: compactedOptions['event'] };

deprecate(
'Future versions of the GoogleTagManagerAdapter will no longer prefix top-level dataLayer keys with `event`. If you wish to retain this behaviour you will need to override the adapter and prefix the keys yourself.',
false,
{
id: 'ember-metrics.issue-438',
for: 'ember-metrics',
since: '1.5.0',
until: '2.0.0',
}
);

delete compactedOptions['event'];

for (let key in compactedOptions) {
const capitalizedKey = capitalize(key);
gtmEvent[`event${capitalizedKey}`] = compactedOptions[key];
}

window[dataLayer].push(gtmEvent);
window[this.dataLayer].push(compactedOptions);

return gtmEvent;
return compactedOptions;
}

trackPage(options = {}) {
Expand Down
29 changes: 17 additions & 12 deletions tests/unit/metrics-adapters/google-tag-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,39 @@ module('google-tag-manager adapter', function (hooks) {
this.adapter.uninstall();
});

test('#trackEvent returns the correct response shape', function (assert) {
test('#trackEvent pushes the event into the dataLayer', function (assert) {
const adapter = new GoogleTagManager(this.config);
this.adapter = adapter;
adapter.install();

sinon.stub(window, 'dataLayer').value({ push() {} });
const events = [];
sinon.stub(window, 'dataLayer').value({
push(e) {
events.push(e);
},
});

const result = adapter.trackEvent({
const eventPayload = {
event: 'click-button',
category: 'button',
action: 'click',
label: 'nav buttons',
value: 4,
});

const expectedResult = {
event: 'click-button',
eventCategory: 'button',
eventAction: 'click',
eventLabel: 'nav buttons',
eventValue: 4,
};

const result = adapter.trackEvent(eventPayload);

assert.deepEqual(
result,
expectedResult,
eventPayload,
'it sends the correct response shape'
);

assert.deepEqual(
events[0],
eventPayload,
'it pushes the event into the dataLayer'
);
});

test('#trackPage returns the correct response shape', function (assert) {
Expand Down