-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathroktManager.ts
More file actions
425 lines (360 loc) · 16.2 KB
/
roktManager.ts
File metadata and controls
425 lines (360 loc) · 16.2 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { IKitConfigs } from "./configAPIClient";
import { UserAttributeFilters } from "./forwarders.interfaces";
import { IMParticleUser } from "./identity-user-interfaces";
import KitFilterHelper from "./kitFilterHelper";
import {
Dictionary,
parseSettingsString,
generateUniqueId,
isFunction,
AttributeValue,
isEmpty,
} from "./utils";
import { SDKIdentityApi } from "./identity.interfaces";
import { SDKLoggerApi } from "./sdkRuntimeModels";
import { IStore, LocalSessionAttributes } from "./store";
import { UserIdentities } from "@mparticle/web-sdk";
import { IdentityType } from "./types";
// https://docs.rokt.com/developers/integration-guides/web/library/attributes
export interface IRoktPartnerAttributes {
[key: string]: string | number | boolean | undefined | null;
}
export interface IRoktPartnerExtensionData<T> {
[extensionName: string]: T;
}
// https://docs.rokt.com/developers/integration-guides/web/library/select-placements-options
export interface IRoktSelectPlacementsOptions {
attributes: IRoktPartnerAttributes;
identifier?: string;
}
interface IRoktPlacement {}
export interface IRoktSelection {
close: () => void;
getPlacements: () => Promise<IRoktPlacement[]>;
}
export interface IRoktLauncher {
selectPlacements: (options: IRoktSelectPlacementsOptions) => Promise<IRoktSelection>;
hashAttributes: (attributes: IRoktPartnerAttributes) => Promise<Record<string, string>>;
use: <T>(name: string) => Promise<T>;
}
export interface IRoktMessage {
messageId: string;
methodName: string;
payload: any;
resolve?: Function;
reject?: Function;
}
export interface RoktKitFilterSettings {
userAttributeFilters?: UserAttributeFilters;
filterUserAttributes?: (userAttributes: Dictionary<string>, filterList: number[]) => Dictionary<string>;
filteredUser?: IMParticleUser | null;
}
export interface IRoktKit {
filters: RoktKitFilterSettings;
filteredUser: IMParticleUser | null;
launcher: IRoktLauncher | null;
userAttributes: Dictionary<string>;
hashAttributes: (attributes: IRoktPartnerAttributes) => Promise<Record<string, string>>;
selectPlacements: (options: IRoktSelectPlacementsOptions) => Promise<IRoktSelection>;
setExtensionData<T>(extensionData: IRoktPartnerExtensionData<T>): void;
use: <T>(name: string) => Promise<T>;
launcherOptions?: Dictionary<any>;
}
export interface IRoktOptions {
sandbox?: boolean;
launcherOptions?: IRoktLauncherOptions;
domain?: string;
}
export type IRoktLauncherOptions = Dictionary<any>;
// The purpose of this class is to create a link between the Core mParticle SDK and the
// Rokt Web SDK via a Web Kit.
// The Rokt Manager should load before the Web Kit and stubs out many of the
// Rokt Web SDK functions with an internal message queue in case a Rokt function
// is requested before the Rokt Web Kit or SDK is finished loaded.
// Once the Rokt Kit is attached to the Rokt Manager, we can consider the
// Rokt Manager in a "ready" state and it can begin sending data to the kit.
//
// https://github.com/mparticle-integrations/mparticle-javascript-integration-rokt
export default class RoktManager {
public kit: IRoktKit = null;
public filters: RoktKitFilterSettings = {};
private currentUser: IMParticleUser | null = null;
private messageQueue: Map<string, IRoktMessage> = new Map();
private sandbox: boolean | null = null;
private placementAttributesMapping: Dictionary<string>[] = [];
private identityService: SDKIdentityApi;
private store: IStore;
private launcherOptions?: IRoktLauncherOptions;
private logger: SDKLoggerApi;
private domain?: string;
private mappedEmailShaIdentityType?: string | null;
/**
* Initializes the RoktManager with configuration settings and user data.
*
* @param {IKitConfigs} roktConfig - Configuration object containing user attribute filters and settings
* @param {IMParticleUser} filteredUser - User object with filtered attributes
* @param {SDKIdentityApi} identityService - The mParticle Identity instance
* @param {SDKLoggerApi} logger - The mParticle Logger instance
* @param {IRoktOptions} options - Options for the RoktManager
*
* @throws Logs error to console if placementAttributesMapping parsing fails
*/
public init(
roktConfig: IKitConfigs,
filteredUser: IMParticleUser,
identityService: SDKIdentityApi,
store: IStore,
logger?: SDKLoggerApi,
options?: IRoktOptions
): void {
const { userAttributeFilters, settings } = roktConfig || {};
const { placementAttributesMapping, hashedEmailUserIdentityType } = settings || {};
this.mappedEmailShaIdentityType = hashedEmailUserIdentityType?.toLowerCase() ?? null;
this.identityService = identityService;
this.store = store;
this.logger = logger;
this.filters = {
userAttributeFilters,
filterUserAttributes: KitFilterHelper.filterUserAttributes,
filteredUser: filteredUser,
};
try {
this.placementAttributesMapping = parseSettingsString(placementAttributesMapping);
} catch (error) {
this.logger.error('Error parsing placement attributes mapping from config: ' + error);
}
// This is the global setting for sandbox mode
// It is set here and passed in to the createLauncher method in the Rokt Kit
// This is not to be confused for the `sandbox` flag in the selectPlacements attributes
// as that is independent of this setting, though they share the same name.
const sandbox = options?.sandbox || false;
// Launcher options are set here for the kit to pick up and pass through
// to the Rokt Launcher.
this.launcherOptions = { sandbox, ...options?.launcherOptions };
if (options?.domain) {
this.domain = options.domain;
}
}
public attachKit(kit: IRoktKit): void {
this.kit = kit;
this.processMessageQueue();
}
/**
* Renders ads based on the options provided
*
* @param {IRoktSelectPlacementsOptions} options - The options for selecting placements, including attributes and optional identifier
* @returns {Promise<IRoktSelection>} A promise that resolves to the selection
*
* @example
* // Correct usage with await
* await window.mParticle.Rokt.selectPlacements({
* attributes: {
* email: 'user@example.com',
* customAttr: 'value'
* }
* });
*/
public async selectPlacements(options: IRoktSelectPlacementsOptions): Promise<IRoktSelection> {
if (!this.isReady()) {
return this.deferredCall<IRoktSelection>('selectPlacements', options);
}
try {
const { attributes } = options;
const sandboxValue = attributes?.sandbox || null;
const mappedAttributes = this.mapPlacementAttributes(attributes, this.placementAttributesMapping);
// Get current user identities
this.currentUser = this.identityService.getCurrentUser();
const currentUserIdentities = this.currentUser?.getUserIdentities()?.userIdentities || {};
const currentEmail = currentUserIdentities.email;
const newEmail = mappedAttributes.email as string;
let currentHashedEmail: string | undefined;
let newHashedEmail: string | undefined;
// Hashed email identity is valid if it is set to Other-Other10
if(this.mappedEmailShaIdentityType && IdentityType.getIdentityType(this.mappedEmailShaIdentityType) !== false) {
currentHashedEmail = currentUserIdentities[this.mappedEmailShaIdentityType];
newHashedEmail = mappedAttributes['emailsha256'] as string || mappedAttributes[this.mappedEmailShaIdentityType] as string || undefined;
}
const emailChanged = this.hasIdentityChanged(currentEmail, newEmail);
const hashedEmailChanged = this.hasIdentityChanged(currentHashedEmail, newHashedEmail);
const newIdentities: UserIdentities = {};
if (emailChanged) {
newIdentities.email = newEmail;
if (newEmail) {
this.logger.warning(`Email mismatch detected. Current email differs from email passed to selectPlacements call. Proceeding to call identify with email from selectPlacements call. Please verify your implementation.`);
}
}
if (hashedEmailChanged) {
newIdentities[this.mappedEmailShaIdentityType] = newHashedEmail;
this.logger.warning(`emailsha256 mismatch detected. Current mParticle hashedEmail differs from hashedEmail passed to selectPlacements call. Proceeding to call identify with hashedEmail from selectPlacements call. Please verify your implementation.`);
}
if (!isEmpty(newIdentities)) {
// Call identify with the new user identities
try {
await new Promise<void>((resolve, reject) => {
this.identityService.identify({
userIdentities: {
...currentUserIdentities,
...newIdentities
}
}, () => {
resolve();
});
});
} catch (error) {
this.logger.error('Failed to identify user with new email: ' + JSON.stringify(error));
}
}
this.setUserAttributes(mappedAttributes);
const enrichedAttributes = {
...mappedAttributes,
...(sandboxValue !== null ? { sandbox: sandboxValue } : {}),
};
const enrichedOptions = {
...options,
attributes: enrichedAttributes,
};
return this.kit.selectPlacements(enrichedOptions);
} catch (error) {
return Promise.reject(error instanceof Error ? error : new Error('Unknown error occurred'));
}
}
public hashAttributes(attributes: IRoktPartnerAttributes): Promise<IRoktPartnerAttributes> {
if (!this.isReady()) {
return this.deferredCall<IRoktPartnerAttributes>('hashAttributes', attributes);
}
try {
return this.kit.hashAttributes(attributes);
} catch (error) {
return Promise.reject(error instanceof Error ? error : new Error('Unknown error occurred'));
}
}
public setExtensionData<T>(extensionData: IRoktPartnerExtensionData<T>): void {
if (!this.isReady()) {
this.deferredCall<void>('setExtensionData', extensionData);
return;
}
try {
this.kit.setExtensionData<T>(extensionData);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error('Error setting extension data: ' + errorMessage);
}
}
public use<T>(name: string): Promise<T> {
if (!this.isReady()) {
return this.deferredCall<T>('use', name);
}
try {
return this.kit.use<T>(name);
} catch (error) {
return Promise.reject(error instanceof Error ? error : new Error('Error using extension: ' + name));
}
}
public getLocalSessionAttributes(): LocalSessionAttributes {
return this.store.getLocalSessionAttributes();
}
public setLocalSessionAttribute(key: string, value: AttributeValue): void {
this.store.setLocalSessionAttribute(key, value);
}
private isReady(): boolean {
// The Rokt Manager is ready when a kit is attached and has a launcher
return Boolean(this.kit && this.kit.launcher);
}
private setUserAttributes(attributes: IRoktPartnerAttributes): void {
const reservedAttributes = ['sandbox'];
const filteredAttributes = {};
for (const key in attributes) {
if (attributes.hasOwnProperty(key) && reservedAttributes.indexOf(key) === -1) {
filteredAttributes[key] = attributes[key];
}
}
try {
this.currentUser.setUserAttributes(filteredAttributes);
} catch (error) {
this.logger.error('Error setting user attributes: ' + error);
}
}
private mapPlacementAttributes(attributes: IRoktPartnerAttributes, placementAttributesMapping: Dictionary<string>[]): IRoktPartnerAttributes {
const mappingLookup: { [key: string]: string } = {};
for (const mapping of placementAttributesMapping) {
mappingLookup[mapping.map] = mapping.value;
}
const mappedAttributes: IRoktPartnerAttributes = {};
for (const key in attributes) {
if (attributes.hasOwnProperty(key)) {
const newKey = mappingLookup[key] || key;
mappedAttributes[newKey] = attributes[key];
}
}
return mappedAttributes;
}
private processMessageQueue(): void {
if (!this.isReady() || this.messageQueue.size === 0) {
return;
}
this.logger?.verbose(`RoktManager: Processing ${this.messageQueue.size} queued messages`);
this.messageQueue.forEach((message) => {
if(!(message.methodName in this) || !isFunction(this[message.methodName])) {
this.logger?.error(`RoktManager: Method ${message.methodName} not found`);
return;
}
this.logger?.verbose(`RoktManager: Processing queued message: ${message.methodName} with payload: ${JSON.stringify(message.payload)}`);
try {
const result = (this[message.methodName] as Function)(message.payload);
this.completePendingPromise(message.messageId, result);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
this.logger?.error(`RoktManager: Error processing message '${message.methodName}': ${errorMessage}`);
this.completePendingPromise(message.messageId, Promise.reject(error));
}
});
// Clear the queue after processing all messages
this.messageQueue.clear();
}
private queueMessage(message: IRoktMessage): void {
this.messageQueue.set(message.messageId, message);
}
private deferredCall<T>(methodName: string, payload: any): Promise<T> {
return new Promise<T>((resolve, reject) => {
const messageId = `${methodName}_${generateUniqueId()}`;
this.queueMessage({
messageId,
methodName,
payload,
resolve,
reject,
});
});
}
private completePendingPromise(messageId: string | undefined, resultOrError: any): void {
if (!messageId || !this.messageQueue.has(messageId)) {
return;
}
const message = this.messageQueue.get(messageId)!;
if (message.resolve) {
Promise.resolve(resultOrError)
.then((result) => message.resolve!(result))
.catch((error) => message.reject!(error));
}
this.messageQueue.delete(messageId);
}
/**
* Checks if an identity value has changed by comparing current and new values
*
* @param {string | undefined} currentValue - The current identity value
* @param {string | undefined} newValue - The new identity value to compare against
* @returns {boolean} True if the identity has changed (new value exists and differs from current), false otherwise
*/
private hasIdentityChanged(currentValue: string | undefined, newValue: string | undefined): boolean {
if (!newValue) {
return false;
}
if (!currentValue) {
return true; // New value exists but no current value
}
if (currentValue !== newValue) {
return true; // Values are different
}
return false; // Values are the same
}
}