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

Add Adobe Launch #240

Merged
merged 2 commits into from
Apr 19, 2024
Merged
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
14 changes: 13 additions & 1 deletion scripts/delayed.js
Original file line number Diff line number Diff line change
@@ -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`);

loadAdobeLaunch();
25 changes: 25 additions & 0 deletions scripts/lib-franklin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading