-
Notifications
You must be signed in to change notification settings - Fork 64
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
77f11bb
commit a0d3c54
Showing
5 changed files
with
120 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as TealiumLoader } from './TealiumLoader'; | ||
Check failure on line 1 in scripts/index.js GitHub Actions / tests
|
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,47 @@ | ||
/** | ||
* @implements {TealiumLoader} | ||
* @memberof module:TealiumLoader | ||
*/ | ||
class TealiumLoader { | ||
constructor({ config }) { | ||
this.account = config.TEALIUM_ACCOUNT; | ||
this.profile = config.TEALIUM_PROFILE; | ||
this.env = config.TEALIUM_ENV; | ||
} | ||
|
||
loadScript() { | ||
if (!this.account && !this.profile && !this.env) { | ||
return; | ||
} | ||
|
||
global.tealiumAnalytics = global.tealiumAnalytics || []; | ||
const { tealiumAnalytics } = global; | ||
|
||
// If the snippet was invoked do nothing. | ||
if (tealiumAnalytics.invoked) { | ||
return; | ||
} | ||
|
||
// Invoked flag, to make sure the snippet | ||
// is never invoked twice. | ||
tealiumAnalytics.invoked = true; | ||
|
||
tealiumAnalytics.load = (account, profile, env) => { | ||
const scriptTealium = document.createElement('script'); | ||
scriptTealium.type = 'text/javascript'; | ||
scriptTealium.innerHTML = ` | ||
(function (a, b, c, d) { a = '//tags.tiqcdn.com/utag/${account}/${profile}/${env}/utag.js'; b = document; | ||
c = 'script'; d = b.createElement(c); d.src = a; d.type = 'text/java' + c; d.async = true; a = b.getElementsByTagName(c)[0]; a.parentNode.insertBefore(d, a) })(); | ||
`; | ||
|
||
// Insert our scripts next to the first script element. | ||
const first = document.getElementsByTagName('script')[0]; | ||
first.parentNode.insertBefore(scriptTealium, first); | ||
}; | ||
|
||
// Load tealiumAnalytics. | ||
tealiumAnalytics.load(this.account, this.profile, this.env); | ||
} | ||
} | ||
|
||
export default TealiumLoader; |
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,69 @@ | ||
import { TealiumLoader } from '.'; | ||
|
||
describe('TealiumLoader', () => { | ||
let body; | ||
let tealiumScriptSrc; | ||
let data; | ||
|
||
beforeEach(() => { | ||
window.tealiumAnalytics = []; | ||
}); | ||
|
||
function loadTealium(scriptData) { | ||
const script = new TealiumLoader(scriptData); | ||
script.loadScript(); | ||
} | ||
|
||
describe('with valid TEALIUM_ACCOUNT, TEALIUM_PROFILE, and TEALIUM_ENV', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = '<script id="stub" />'; | ||
data = { | ||
config: { | ||
TEALIUM_ACCOUNT: 'test-account', | ||
TEALIUM_PROFILE: 'test-profile', | ||
TEALIUM_ENV: 'test-env', | ||
}, | ||
}; | ||
loadTealium(data); | ||
expect(global.tealiumAnalytics.invoked).toBe(true); | ||
body = document.body.innerHTML; | ||
tealiumScriptSrc = `//tags.tiqcdn.com/utag/test-account/test-profile/test-env/utag.js`; | ||
}); | ||
|
||
it('should initialize Tealium', () => { | ||
expect(body).toMatch(tealiumScriptSrc); | ||
}); | ||
|
||
it('should be two snippets', () => { | ||
loadTealium(data); | ||
|
||
expect(global.tealiumAnalytics.invoked).toBe(true); | ||
expect(body).toMatch(tealiumScriptSrc); | ||
|
||
let count = (body.match(new RegExp(tealiumScriptSrc.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) || []).length; | ||
|
||
expect(count).toBe(2); | ||
}); | ||
}); | ||
|
||
describe('with missing TEALIUM_ACCOUNT, TEALIUM_PROFILE, and TEALIUM_ENV', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = '<script id="stub" />'; | ||
data = { | ||
config: { | ||
TEALIUM_ACCOUNT: '', | ||
TEALIUM_PROFILE: '', | ||
TEALIUM_ENV: '', | ||
}, | ||
}; | ||
loadTealium(data); | ||
body = document.body.innerHTML; | ||
tealiumScriptSrc = '//tags.tiqcdn.com/utag/test-account/test-profile/test-env/utag.js'; | ||
expect(global.tealiumAnalytics.invoked).toBeFalsy(); | ||
}); | ||
|
||
it('should not initialize Tealium', () => { | ||
expect(body).not.toMatch(tealiumScriptSrc); | ||
}); | ||
}); | ||
}); |
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,2 +1,3 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
export { default as GoogleAnalyticsLoader } from './GoogleAnalyticsLoader'; | ||
export { default as TealiumLoader } from './TealiumLoader'; |