-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
948d593
commit 5ecc785
Showing
6 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Service from '@ember/service'; | ||
|
||
export default class GtmService extends Service { | ||
_pageHasGTM() { | ||
return window.dataLayer && typeof window.dataLayer === 'object'; | ||
} | ||
|
||
trackGTM(data) { | ||
if (this._pageHasGTM()) { | ||
window.dataLayer.push(data); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-gtm/services/gtm'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
name: require('./package').name | ||
name: require('./package').name, | ||
|
||
contentFor: function(type, config) { | ||
const addonConfig = config['ember-gtm']; | ||
const appId = addonConfig ? addonConfig.appId : null; | ||
if (type === 'head' && appId) { | ||
return `<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | ||
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | ||
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= | ||
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); | ||
})(window,document,'script','dataLayer','${appId}');</script>`; | ||
} else if (type === 'body' && appId) { | ||
return `<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${appId}" | ||
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>` | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupTest } from 'ember-qunit'; | ||
|
||
class FakeDataLayer { | ||
items = []; | ||
|
||
push(item) { | ||
this.items.push(item); | ||
} | ||
} | ||
|
||
module('Unit | Service | gtm', function(hooks) { | ||
setupTest(hooks); | ||
|
||
test('it exists', function(assert) { | ||
let service = this.owner.lookup('service:gtm'); | ||
assert.ok(service); | ||
}); | ||
|
||
test('it only runs if the page has GTM', function(assert) { | ||
const someString = 'string'; | ||
window.dataLayer = someString; // wrong type of object. | ||
let service = this.owner.lookup('service:gtm'); | ||
|
||
const data = { stuff: 'things' }; | ||
|
||
service.trackGTM(data); | ||
assert.equal(window.dataLayer, someString); // didn't push | ||
|
||
window.dataLayer = new FakeDataLayer(); | ||
|
||
service.trackGTM(data); | ||
assert.equal(window.dataLayer.items[0], data); // pushed | ||
}); | ||
}); |