generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathmartech.js
215 lines (187 loc) · 6.36 KB
/
martech.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {
getConfig, loadIms, loadLink, loadScript, getMepEnablement, getMetadata,
} from '../utils/utils.js';
const ALLOY_SEND_EVENT = 'alloy_sendEvent';
const ALLOY_SEND_EVENT_ERROR = 'alloy_sendEvent_error';
const ENTITLEMENT_TIMEOUT = 3000;
const TARGET_TIMEOUT_MS = 4000;
const params = new URL(window.location.href).searchParams;
const timeout = parseInt(params.get('target-timeout'), 10)
|| parseInt(getMetadata('target-timeout'), 10)
|| TARGET_TIMEOUT_MS;
const setDeep = (obj, path, value) => {
const pathArr = path.split('.');
let currentObj = obj;
for (const key of pathArr.slice(0, -1)) {
if (!currentObj[key] || typeof currentObj[key] !== 'object') {
currentObj[key] = {};
}
currentObj = currentObj[key];
}
currentObj[pathArr[pathArr.length - 1]] = value;
};
// eslint-disable-next-line max-len
const waitForEventOrTimeout = (eventName, timeoutLocal, returnValIfTimeout) => new Promise((resolve) => {
const listener = (event) => {
// eslint-disable-next-line no-use-before-define
clearTimeout(timer);
resolve(event.detail);
};
const errorListener = () => {
// eslint-disable-next-line no-use-before-define
clearTimeout(timer);
resolve({ error: true });
};
const timer = setTimeout(() => {
window.removeEventListener(eventName, listener);
if (returnValIfTimeout !== undefined) {
resolve(returnValIfTimeout);
} else {
resolve({ timeout: true });
}
}, timeoutLocal);
window.addEventListener(eventName, listener, { once: true });
window.addEventListener(ALLOY_SEND_EVENT_ERROR, errorListener, { once: true });
});
function roundToQuarter(num) {
return Math.ceil(num / 250) / 4;
}
function calculateResponseTime(responseStart) {
const responseTime = Date.now() - responseStart;
return roundToQuarter(responseTime);
}
export const getTargetPersonalization = async (
{ handleAlloyResponse, sendTargetResponseAnalytics },
) => {
const responseStart = Date.now();
window.addEventListener(ALLOY_SEND_EVENT, () => {
const responseTime = calculateResponseTime(responseStart);
try {
window.lana.log(`target response time: ${responseTime}`, { tags: 'martech', errorType: 'i' });
} catch (e) {
// eslint-disable-next-line no-console
console.error('Error logging target response time:', e);
}
}, { once: true });
let targetManifests = [];
let targetPropositions = [];
const response = await waitForEventOrTimeout(ALLOY_SEND_EVENT, timeout);
if (response.error) {
try {
window.lana.log('target response time: ad blocker', { tags: 'martech', errorType: 'i' });
} catch (e) {
// eslint-disable-next-line no-console
console.error('Error logging target response time for ad blocker:', e);
}
return { targetManifests, targetPropositions };
}
if (response.timeout) {
waitForEventOrTimeout(ALLOY_SEND_EVENT, 5100 - timeout)
.then(() => sendTargetResponseAnalytics(true, responseStart, timeout));
} else {
sendTargetResponseAnalytics(false, responseStart, timeout);
targetManifests = handleAlloyResponse(response.result);
targetPropositions = response.result?.propositions || [];
}
return { targetManifests, targetPropositions };
};
const setupEntitlementCallback = () => {
const setEntitlements = async (destinations) => {
const { getEntitlements } = await import('../features/personalization/personalization.js');
return getEntitlements(destinations);
};
const getEntitlements = (resolve) => {
const handleEntitlements = (detail) => {
if (detail?.result?.destinations?.length) {
resolve(setEntitlements(detail.result.destinations));
} else {
resolve([]);
}
};
waitForEventOrTimeout(ALLOY_SEND_EVENT, ENTITLEMENT_TIMEOUT, [])
.then(handleEntitlements)
.catch(() => resolve([]));
};
const { miloLibs, codeRoot, entitlements: resolveEnt } = getConfig();
getEntitlements(resolveEnt);
loadLink(
`${miloLibs || codeRoot}/features/personalization/personalization.js`,
{ as: 'script', rel: 'modulepreload' },
);
};
function isProxied() {
return /^(www|milo|business|blog|news)(\.stage)?\.adobe\.com$/.test(window.location.hostname);
}
let filesLoadedPromise = false;
const loadMartechFiles = async (config) => {
if (filesLoadedPromise) return filesLoadedPromise;
filesLoadedPromise = async () => {
if (getMepEnablement('xlg') === 'loggedout') {
setupEntitlementCallback();
} else {
loadIms()
.then(() => {
if (window.adobeIMS.isSignedInUser()) setupEntitlementCallback();
})
.catch(() => { });
}
setDeep(
window,
'alloy_all.data._adobe_corpnew.digitalData.page.pageInfo.language',
{ locale: config.locale.prefix.replace('/', ''), langCode: config.locale.ietf },
);
setDeep(window, 'digitalData.diagnostic.franklin.implementation', 'milo');
const launchUrl = config.env.consumer?.marTechUrl || (
isProxied()
? '/marketingtech'
: 'https://assets.adobedtm.com'
) + (
config.env.name === 'prod'
? '/d4d114c60e50/a0e989131fd5/launch-5dd5dd2177e6.min.js'
: '/d4d114c60e50/a0e989131fd5/launch-2c94beadc94f-development.min.js'
);
loadLink(launchUrl, { as: 'script', rel: 'preload' });
window.marketingtech = {
adobe: {
launch: {
url: launchUrl,
controlPageLoad: true,
},
alloy: {
edgeConfigId: config.env.consumer?.edgeConfigId || config.env.edgeConfigId,
edgeDomain: (
isProxied()
? window.location.hostname
: 'sstats.adobe.com'
),
edgeBasePath: (
isProxied()
? 'experienceedge'
: 'ee'
),
},
target: false,
},
milo: true,
};
window.edgeConfigId = config.env.edgeConfigId;
await loadScript((
isProxied()
? ''
: 'https://www.adobe.com'
) + (
config.env.name === 'prod'
? '/marketingtech/main.standard.min.js'
: '/marketingtech/main.standard.qa.min.js'
));
// eslint-disable-next-line no-underscore-dangle
window._satellite.track('pageload');
};
await filesLoadedPromise();
return filesLoadedPromise;
};
export default async function init() {
const config = getConfig();
const martechPromise = loadMartechFiles(config);
return martechPromise;
}