From 814985f961d84c80736ac78e37b3c4a3ab66bceb Mon Sep 17 00:00:00 2001 From: Ian <> Date: Tue, 16 Apr 2024 11:11:28 -0500 Subject: [PATCH] Add Adobe Launch --- scripts/delayed.js | 14 +++++++++++++- scripts/lib-franklin.js | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/scripts/delayed.js b/scripts/delayed.js index 7039682..431d0b4 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -1,10 +1,22 @@ // eslint-disable-next-line import/no-cycle -import { loadCSS, sampleRUM } from './lib-franklin.js'; +import { loadCSS, loadScript, sampleRUM } from './lib-franklin.js'; // Core Web Vitals RUM collection sampleRUM('cwv'); // add more delayed functionality here + +async function loadAdobeLaunch() { + const adobedtmSrc = 'https://assets.adobedtm.com/9273d4aedcd2/6eb97addd328/launch-1dd947b3f935.min.js'; + + await loadScript(adobedtmSrc, { + type: 'text/javascript', + async: true, + }); +} + store.emit('delayed:loaded'); loadCSS(`${window.hlx.codeBasePath}/styles/icons.css`); + +await loadAdobeLaunch(); diff --git a/scripts/lib-franklin.js b/scripts/lib-franklin.js index 6ccfbf6..0f798df 100644 --- a/scripts/lib-franklin.js +++ b/scripts/lib-franklin.js @@ -117,6 +117,31 @@ export function loadCSS(href, callback) { } } +/** + * Loads a non module JS file. + * @param {string} src URL to the JS file + * @param {Object} attrs additional optional attributes + */ +export async function loadScript(src, attrs) { + return new Promise((resolve, reject) => { + if (!document.querySelector(`head > script[src="${src}"]`)) { + const script = document.createElement('script'); + script.src = src; + if (attrs) { + // eslint-disable-next-line no-restricted-syntax, guard-for-in + for (const attr in attrs) { + script.setAttribute(attr, attrs[attr]); + } + } + script.onload = resolve; + script.onerror = reject; + document.head.append(script); + } else { + resolve(); + } + }); +} + /** * Retrieves the content of metadata tags. * @param {string} name The metadata name (or property)