Skip to content

Commit

Permalink
integrated experimentation code
Browse files Browse the repository at this point in the history
  • Loading branch information
ntoccane committed Jul 8, 2024
1 parent 065b203 commit 2cf6df9
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,62 @@ import {
waitForLCP,
loadBlocks,
loadCSS,
getMetadata,
loadScript,
toCamelCase,
toClassName

Check failure on line 17 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
} from './aem.js';

// TODO: 1
// =========
const AUDIENCES = {
mobile: () => window.innerWidth < 600,
desktop: () => window.innerWidth >= 600,
// define your custom audiences here as needed
};

/**
* Gets all the metadata elements that are in the given scope.
* @param {String} scope The scope/prefix for the metadata
* @returns an array of HTMLElement nodes that match the given scope
*/
export function getAllMetadata(scope) {
return [...document.head.querySelectorAll(`meta[property^="${scope}:"],meta[name^="${scope}-"]`)]
.reduce((res, meta) => {
const id = toClassName(meta.name
? meta.name.substring(scope.length + 1)
: meta.getAttribute('property').split(':')[1]);
res[id] = meta.getAttribute('content');
return res;
}, {});
}
// =========

// TODO: 5
// =========
window.hlx.plugins.add('experimentation', {
condition: () => getMetadata('experiment')
|| Object.keys(getAllMetadata('campaign')).length
|| Object.keys(getAllMetadata('audience')).length,
options: { audiences: AUDIENCES },
url: '/plugins/experimentation/src/index.js',
});
// =========

// TODO: 2
// =========
// Define an execution context
const pluginContext = {
getAllMetadata,
getMetadata,
loadCSS,
loadScript,
sampleRUM,
toCamelCase,
toClassName,
};
// =========

const LCP_BLOCKS = []; // add your LCP blocks to the list

/**
Expand Down Expand Up @@ -74,6 +128,19 @@ export function decorateMain(main) {
* @param {Element} doc The container element
*/
async function loadEager(doc) {
// TODO: 3
// =========

// Add below snippet early in the eager phase
if (getMetadata('experiment')
|| Object.keys(getAllMetadata('campaign')).length
|| Object.keys(getAllMetadata('audience')).length) {
// eslint-disable-next-line import/no-relative-packages
const { loadEager: runEager } = await import('../plugins/experimentation/src/index.js');
await runEager(document, { audiences: AUDIENCES }, pluginContext);
}


Check failure on line 143 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

More than 1 blank line not allowed
document.documentElement.lang = 'en';
decorateTemplateAndTheme();
const main = doc.querySelector('main');
Expand Down Expand Up @@ -114,6 +181,18 @@ async function loadLazy(doc) {
sampleRUM('lazy');
sampleRUM.observe(main.querySelectorAll('div[data-block-name]'));
sampleRUM.observe(main.querySelectorAll('picture > img'));

// TODO: 4
// =========
// Add below snippet at the end of the lazy phase
if ((getMetadata('experiment')
|| Object.keys(getAllMetadata('campaign')).length
|| Object.keys(getAllMetadata('audience')).length)) {
// eslint-disable-next-line import/no-relative-packages
const { loadLazy: runLazy } = await import('../plugins/experimentation/src/index.js');
await runLazy(document, { audiences: AUDIENCES }, pluginContext);
}
// =========
}

/**
Expand Down

0 comments on commit 2cf6df9

Please sign in to comment.