Skip to content

Commit

Permalink
feat: added script loader
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Jun 18, 2024
1 parent 77f11bb commit a0d3c54
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
1 change: 1 addition & 0 deletions scripts/index.js
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

View workflow job for this annotation

GitHub Actions / tests

Prefer default export on a file with single export

Check failure on line 1 in scripts/index.js

View workflow job for this annotation

GitHub Actions / tests

Unable to resolve path to module './TealiumLoader'
4 changes: 2 additions & 2 deletions src/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import {
import {
configure as configureAnalytics, SegmentAnalyticsService, identifyAnonymousUser, identifyAuthenticatedUser,
} from './analytics';
import { GoogleAnalyticsLoader } from './scripts';
import { GoogleAnalyticsLoader, TealiumLoader } from './scripts';
import {
getAuthenticatedHttpClient,
configure as configureAuth,
Expand Down Expand Up @@ -290,7 +290,7 @@ export async function initialize({
analyticsService = SegmentAnalyticsService,
authService = AxiosJwtAuthService,
authMiddleware = [],
externalScripts = [GoogleAnalyticsLoader],
externalScripts = [GoogleAnalyticsLoader, TealiumLoader],
requireAuthenticatedUser: requireUser = false,
hydrateAuthenticatedUser: hydrateUser = false,
messages,
Expand Down
47 changes: 47 additions & 0 deletions src/scripts/TealiumLoader.js
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;
69 changes: 69 additions & 0 deletions src/scripts/TealiumLoader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { TealiumLoader } from '.';

describe('TealiumLoader', () => {
let body;

Check failure on line 4 in src/scripts/TealiumLoader.test.js

View workflow job for this annotation

GitHub Actions / tests

Expected indentation of 2 spaces but found 4
let tealiumScriptSrc;

Check failure on line 5 in src/scripts/TealiumLoader.test.js

View workflow job for this annotation

GitHub Actions / tests

Expected indentation of 2 spaces but found 4
let data;

Check failure on line 6 in src/scripts/TealiumLoader.test.js

View workflow job for this annotation

GitHub Actions / tests

Expected indentation of 2 spaces but found 4

beforeEach(() => {

Check failure on line 8 in src/scripts/TealiumLoader.test.js

View workflow job for this annotation

GitHub Actions / tests

Expected indentation of 2 spaces but found 4
window.tealiumAnalytics = [];

Check failure on line 9 in src/scripts/TealiumLoader.test.js

View workflow job for this annotation

GitHub Actions / tests

Expected indentation of 4 spaces but found 8
});

Check failure on line 10 in src/scripts/TealiumLoader.test.js

View workflow job for this annotation

GitHub Actions / tests

Expected indentation of 2 spaces but found 4

function loadTealium(scriptData) {

Check failure on line 12 in src/scripts/TealiumLoader.test.js

View workflow job for this annotation

GitHub Actions / tests

Expected indentation of 2 spaces but found 4
const script = new TealiumLoader(scriptData);

Check failure on line 13 in src/scripts/TealiumLoader.test.js

View workflow job for this annotation

GitHub Actions / tests

Expected indentation of 4 spaces but found 8
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);
});
});
});
1 change: 1 addition & 0 deletions src/scripts/index.js
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';

0 comments on commit a0d3c54

Please sign in to comment.