Skip to content
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
2 changes: 1 addition & 1 deletion test/utils/experiment-provider.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-underscore-dangle */
import { expect } from '@esm-bundle/chai';
import { getExperimentData, getDecisionScopesForVerb } from '../../unitylibs/utils/experiment-provider.js';
import getExperimentData, { getDecisionScopesForVerb } from '../../unitylibs/utils/experiment-provider.js';

describe('getExperimentData', () => {
// Helper function to setup mock with result and error
Expand Down
56 changes: 38 additions & 18 deletions unitylibs/core/workflow/workflow-acrobat/action-binder.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default class ActionBinder {
this.actionMap = actionMap;
this.limits = {};
this.operations = [];
this.acrobatApiConfig = this.getAcrobatApiConfig();
this.acrobatApiConfig = null;
this.networkUtils = new NetworkUtils();
this.uploadHandler = null;
this.splashScreenEl = null;
Expand All @@ -182,6 +182,9 @@ export default class ActionBinder {
this.multiFileValidationFailure = false;
this.initialize();
this.experimentData = null;
this.experimentViaPageConfig = false;
this.pageConfigLocation = null;
this.pageConfigFetched = false;
}

async initialize() {
Expand Down Expand Up @@ -224,11 +227,13 @@ export default class ActionBinder {
}

getAcrobatApiConfig() {
const base = this.pageConfigLocation ? `${this.pageConfigLocation}/api/v1` : unityConfig.apiEndPoint;
unityConfig.acrobatEndpoint = {
createAsset: `${unityConfig.apiEndPoint}/asset`,
finalizeAsset: `${unityConfig.apiEndPoint}/asset/finalize`,
getMetadata: `${unityConfig.apiEndPoint}/asset/metadata`,
createAsset: `${base}/asset`,
finalizeAsset: `${base}/asset/finalize`,
getMetadata: `${base}/asset/metadata`,
};
unityConfig.connectorApiEndPoint = `${base}/asset/connector`;
return unityConfig;
}

Expand All @@ -242,18 +247,6 @@ export default class ActionBinder {
}

async handlePreloads() {
if (!this.experimentData && this.workflowCfg.targetCfg?.experimentationOn?.includes(this.workflowCfg.enabledFeatures[0])) {
const { getExperimentData, getDecisionScopesForVerb } = await import('../../../utils/experiment-provider.js');
try {
const decisionScopes = await getDecisionScopesForVerb(this.workflowCfg.enabledFeatures[0]);
this.experimentData = await getExperimentData(decisionScopes);
} catch (error) {
await this.dispatchErrorToast('warn_fetch_experiment', null, error.message, true, true, {
code: 'warn_fetch_experiment',
desc: error.message,
});
}
}
const parr = [];
if (this.workflowCfg.targetCfg.showSplashScreen) {
parr.push(
Expand All @@ -263,6 +256,32 @@ export default class ActionBinder {
await priorityLoad(parr);
}

async ensurePageConfig() {
if (this.pageConfigFetched) return;
this.pageConfigFetched = true;
const verb = this.workflowCfg.enabledFeatures[0];
try {
const { fetchPageConfig } = await import('../../../scripts/utils.js');
const { default: getExperimentData } = await import('../../../utils/experiment-provider.js');
const pageConfig = await fetchPageConfig({ product: 'acrobat', verb });
this.pageConfigLocation = pageConfig.location;
if (pageConfig.config?.target?.enabled) {
this.experimentData = await getExperimentData(pageConfig.config.target.decisionScopes);
this.experimentViaPageConfig = true;
} else if (!this.experimentData && this.workflowCfg.targetCfg?.experimentationOn?.includes(verb)) {
const { getDecisionScopesForVerb } = await import('../../../utils/experiment-provider.js');
const decisionScopes = await getDecisionScopesForVerb(verb);
this.experimentData = await getExperimentData(decisionScopes);
}
} catch (error) {
await this.dispatchErrorToast('warn_fetch_experiment', null, error.message, true, true, {
code: 'warn_fetch_experiment',
desc: error.message,
});
}
this.acrobatApiConfig = this.getAcrobatApiConfig();
}

async dispatchErrorToast(errorType, status, info = null, lanaOnly = false, showError = true, errorMetaData = {}) {
if (!showError) return;
const errorMessage = errorType in this.workflowCfg.errors
Expand Down Expand Up @@ -457,7 +476,7 @@ export default class ActionBinder {
redirectUrl = url.href;
}
}
this.redirectUrl = redirectUrl;
this.redirectUrl = redirectUrl;
})
.catch(async (e) => {
await this.showTransitionScreen();
Expand Down Expand Up @@ -486,7 +505,7 @@ export default class ActionBinder {
if (this.multiFileValidationFailure) cOpts.payload.feedback = 'uploaderror';
if (this.showInfoToast) cOpts.payload.feedback = 'nonpdf';
}
if (this.workflowCfg.targetCfg?.experimentationOn?.includes(this.workflowCfg.enabledFeatures[0]) && this.experimentData) {
if (this.experimentData && (this.experimentViaPageConfig || this.workflowCfg.targetCfg?.experimentationOn?.includes(this.workflowCfg.enabledFeatures[0]))) {
cOpts.payload.variationId = this.experimentData.variationId;
}
await this.getRedirectUrl(cOpts);
Expand Down Expand Up @@ -556,6 +575,7 @@ export default class ActionBinder {
if (prevalidatedFiles.length === 0) return;
const { isValid, validFiles } = await this.validateFiles(prevalidatedFiles);
if (!isValid) return;
await this.ensurePageConfig();
await this.initUploadHandler();
if (files.length === 1 || (validFiles.length === 1 && !verbsWithoutFallback.includes(this.workflowCfg.enabledFeatures[0]))) {
await this.handleSingleFileUpload(validFiles);
Expand Down
13 changes: 13 additions & 0 deletions unitylibs/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,14 @@ export const unityConfig = (() => {
prod: {
apiEndPoint: 'https://unity.adobe.io/api/v1',
connectorApiEndPoint: 'https://unity.adobe.io/api/v1/asset/connector',
pageConfigEndPoint: 'https://cdn-unity.adobe.com/api/v1/pageConfig',
env: 'prod',
...commoncfg,
},
stage: {
apiEndPoint: 'https://unity-stage.adobe.io/api/v1',
connectorApiEndPoint: 'https://unity-stage.adobe.io/api/v1/asset/connector',
pageConfigEndPoint: 'https://cdn-unity.stage.adobe.com/api/v1/pageConfig',
env: 'stage',
...commoncfg,
},
Expand Down Expand Up @@ -335,3 +337,14 @@ export function sendAnalyticsEvent(event) {
export function getMatchedDomain(domainMap = {}, hostname = window.location.hostname) {
return Object.keys(domainMap).find((domain) => domainMap[domain].some((pattern) => new RegExp(pattern).test(hostname)));
}

export async function fetchPageConfig({ product, verb }) {
try {
const url = `${unityConfig.pageConfigEndPoint}?product=${product}&verb=${verb}`;
const resp = await fetch(url, { headers: { 'x-api-key': unityConfig.apiKey } });
if (!resp.ok) throw new Error(`PageConfig fetch failed: ${resp.statusText}`);
return resp.json();
} catch (e) {
return {};
}
}
14 changes: 7 additions & 7 deletions unitylibs/utils/experiment-provider.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/* eslint-disable no-underscore-dangle */

export async function getDecisionScopesForVerb(verb) {
const region = await getRegion().catch(() => undefined);
const verbScope = `acom_unity_acrobat_${verb}`;
return region ? [`${verbScope}_${region}`, verbScope] : [verbScope];
}

export async function getRegion() {
const resp = await fetch('https://geo2.adobe.com/json/', { cache: 'no-cache' });
if (!resp.ok) throw new Error(`Failed to resolve region: ${resp.statusText}`);
Expand All @@ -14,7 +8,13 @@ export async function getRegion() {
return country.toLowerCase();
}

export async function getExperimentData(decisionScopes) {
export async function getDecisionScopesForVerb(verb) {
const region = await getRegion().catch(() => undefined);
const verbScope = `acom_unity_acrobat_${verb}`;
return region ? [`${verbScope}_${region}`, verbScope] : [verbScope];
}

export default async function getExperimentData(decisionScopes) {
if (!decisionScopes || decisionScopes.length === 0) {
throw new Error('No decision scopes provided for experiment data fetch');
}
Expand Down
Loading