Skip to content

Commit

Permalink
feat: apply review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsaplin committed May 29, 2024
1 parent da49496 commit 84eb9d5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
1 change: 0 additions & 1 deletion head.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script src="/solutions/scripts/lib-franklin.js" type="module"></script>
<script src="/solutions/scripts/utils.js" type="module"></script>
<script src="/solutions/scripts/target.js" type="module"></script>
<script src="/solutions/scripts/scripts.js" type="module"></script>
<link rel="stylesheet" href="/solutions/styles/styles.css"/>
7 changes: 5 additions & 2 deletions solutions/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
} from './utils.js';

import { loadAnalytics } from './analytics.js';
import runTargetExperiment from './target.js';

const LCP_BLOCKS = ['hero']; // add your LCP blocks to the list
const TRACKED_PRODUCTS = [];
Expand Down Expand Up @@ -516,7 +515,11 @@ async function loadEager(doc) {

await window.hlx.plugins.run('loadEager');

const targetExperimentDetails = await runTargetExperiment(TARGET_TENANT);
let targetExperimentDetails = null;
if (getMetadata('target-experiment') !== '') {
const { runTargetExperiment } = await import('./target.js');
targetExperimentDetails = await runTargetExperiment(TARGET_TENANT);
}

pushPageLoadToDataLayer(targetExperimentDetails);

Expand Down
27 changes: 15 additions & 12 deletions solutions/scripts/target.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ const ADOBE_TARGET_SESSION_ID_PARAM = 'adobeTargetSessionId';
* @param url
* @returns {*|string}
*/
function toRelativeUrl(url) {
try {
const parsedUrl = new URL(url);
return parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;
} catch (e) {
return url;
}
function getPlainPageUrl(url) {
const { pathname, search, hash } = new URL(url, window.location.href);
const plainPagePathname = pathname.endsWith('/') ? `${pathname}index.plain.html` : `${pathname}.plain.html`;
return `${plainPagePathname}${search}${hash}`;
}

/**
Expand Down Expand Up @@ -95,21 +92,27 @@ async function fetchChallengerPageUrl(tenant, targetLocation) {
* @param url The challenger page url.
* @returns {Promise<boolean>}
*/
async function switchToChallengerPage(url) {
const relativePath = toRelativeUrl(url);
const plainPath = relativePath.endsWith('/') ? `${relativePath}index.plain.html` : `${relativePath}.plain.html`;
async function navigateToChallengerPage(url) {
const plainPath = getPlainPageUrl(url);

// eslint-disable-next-line no-console
console.debug(`Navigating to challenger page: ${plainPath}`);

const resp = await fetch(plainPath);
if (!resp.ok) {
throw new Error(`Failed to fetch challenger page: ${resp.status}`);
}

const mainElement = document.querySelector('main');
if (!mainElement) {
throw new Error('Main element not found');
}

mainElement.innerHTML = await resp.text();
}

export default async function runTargetExperiment(clientId) {
// eslint-disable-next-line import/prefer-default-export
export async function runTargetExperiment(clientId) {
try {
const experimentId = getMetadata('target-experiment');
const targetLocation = getMetadata('target-experiment-location');
Expand All @@ -126,7 +129,7 @@ export default async function runTargetExperiment(clientId) {
// eslint-disable-next-line no-console
console.debug(`Challenger page url: ${pageUrl}`);

await switchToChallengerPage(pageUrl);
await navigateToChallengerPage(pageUrl);

sampleRUM('target-experiment', {
source: `target:${experimentId}`,
Expand Down

0 comments on commit 84eb9d5

Please sign in to comment.