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

1412 poc adobe target #1424

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
helix-importer-ui
scripts/ffetch.js
campaigns
campaigns
scripts/at.js
18 changes: 18 additions & 0 deletions scripts/at.js

Large diffs are not rendered by default.

93 changes: 92 additions & 1 deletion scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,89 @@ export async function decorateMain(main) {
addHreflangTags();
}

function initATJS(path, config) {
window.targetGlobalSettings = config;
window.targetPageParams = function getTargetPageParams() {
return {
at_property: 'e16bf999-0c8d-7270-6252-fe4e2f4a9523',
};
};
return new Promise((resolve) => {
import(path).then(resolve);
});
}

function onDecoratedElement(fn) {
// Apply propositions to all already decorated blocks/sections
if (document.querySelector('[data-block-status="loaded"],[data-section-status="loaded"]')) {
fn();
}

const observer = new MutationObserver((mutations) => {
if (mutations.some((m) => m.target.tagName === 'BODY'
|| m.target.dataset.sectionStatus === 'loaded'
|| m.target.dataset.blockStatus === 'loaded')) {
fn();
}
});
// Watch sections and blocks being decorated async
observer.observe(document.querySelector('main'), {
subtree: true,
attributes: true,
attributeFilter: ['data-block-status', 'data-section-status'],
});
// Watch anything else added to the body
observer.observe(document.querySelector('body'), { childList: true });
}

function toCssSelector(selector) {
return selector.replace(/(\.\S+)?:eq\((\d+)\)/g, (_, clss, i) => `:nth-child(${Number(i) + 1}${clss ? ` of ${clss})` : ''}`);
}

async function getElementForOffer(offer) {
const selector = offer.cssSelector || toCssSelector(offer.selector);
return document.querySelector(selector);
}

async function getElementForMetric(metric) {
const selector = toCssSelector(metric.selector);
return document.querySelector(selector);
}

async function getAndApplyOffers() {
const response = await window.adobe.target.getOffers({ request: { execute: { pageLoad: {} } } });
const { options = [], metrics = [] } = response.execute.pageLoad;
onDecoratedElement(() => {
window.adobe.target.applyOffers({ response });
// keeping track of offers that were already applied
// eslint-disable-next-line no-return-assign
options.forEach((o) => o.content = o.content.filter((c) => !getElementForOffer(c)));
// keeping track of metrics that were already applied
// eslint-disable-next-line no-confusing-arrow
metrics.map((m, i) => getElementForMetric(m) ? i : -1)
.filter((i) => i >= 0)
.reverse()
.map((i) => metrics.splice(i, 1));
});
}

let atjsPromise = Promise.resolve();
atjsPromise = initATJS('./at.js', {
clientCode: 'danaher',
serverDomain: 'danaher.tt.omtrdc.net',
imsOrgId: '08333E7B636A2D4D0A495C34@AdobeOrg',
bodyHidingEnabled: false,
cookieDomain: window.location.hostname,
pageLoadEnabled: false,
secureOnly: true,
viewsEnabled: false,
withWebGLRenderer: false,
}).catch((e) => {
// eslint-disable-next-line no-console
console.error('Error loading at.js', e);
});
document.addEventListener('at-library-loaded', () => getAndApplyOffers());

function isHomepage() {
return window.location.pathname === '/';
}
Expand All @@ -837,7 +920,15 @@ async function loadEager(doc) {
await window.hlx.plugins.run('loadEager');
await decorateMain(main);
createBreadcrumbsSpace(main);
await waitForLCP(LCP_BLOCKS);
await atjsPromise;

await new Promise((resolve) => {
window.requestAnimationFrame(async () => {
document.body.classList.add('appear');
await waitForLCP(LCP_BLOCKS);
resolve();
});
});
}
if (window.innerWidth >= 900) loadCSS(`${window.hlx.codeBasePath}/styles/fonts.css`);

Expand Down
Loading