-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathserverModel.ts
More file actions
507 lines (460 loc) · 18.6 KB
/
Copy pathserverModel.ts
File metadata and controls
507 lines (460 loc) · 18.6 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// TODO: This file is no longer the server model because the web SDK payload
// is now the server-to-server model. We should rename this to
// something more appropriate.
import Types from './types';
import Constants from './constants';
import {
BaseEvent,
SDKEvent,
SDKEventCustomFlags,
SDKGeoLocation,
SDKProduct,
} from './sdkRuntimeModels';
import {
parseNumber,
parseStringOrNumber,
Dictionary,
isValidCustomFlagProperty,
extend,
getHref,
} from './utils';
import { IntegrationAttributes, ServerSettings } from './store';
import { MPID } from '@mparticle/web-sdk';
import {
IConsentStateV2DTO,
IGDPRConsentStateV2DTO,
IPrivacyV2DTO,
SDKConsentState,
} from './consent';
import { IMParticleUser, ISDKUserIdentity } from './identity-user-interfaces';
import { IMParticleWebSDKInstance } from './mp-instance';
import { appendUserInfo } from './user-utils';
const MessageType = Types.MessageType;
const ApplicationTransitionType = Types.ApplicationTransitionType;
// TODO: Confirm which attributes are optional
export interface IServerV2DTO {
id: string | number;
nm: string | number;
pr: number;
qt: number;
br: string | number;
va: string | number;
ca: string | number;
ps: number;
cc: string | number;
attrs: Record<string, string> | null;
dp_id?: string;
dp_v?: number;
n?: string;
et?: number;
// https://go.mparticle.com/work/SQDSDKS-5196
ua?: Dictionary<string | string[]>;
ui?: ISDKUserIdentity[];
ia?: Dictionary<Dictionary<string>>;
str?: ServerSettings;
sdk?: string;
sid?: string;
sl?: number;
ssd?: number;
dt?: number;
dbg?: boolean;
ct?: number;
lc?: SDKGeoLocation;
o?: boolean;
eec?: number;
av?: string;
cgid?: string;
das?: string;
mpid?: MPID;
smpids?: MPID[];
con?: IConsentStateV2DTO;
fr?: boolean;
iu?: boolean; // isUpgrade
at?: number;
lr?: string;
flags?: Dictionary<string[]>;
cu?: string;
sc?: {
pl: IProductV2DTO[];
};
pd?: {
an: number;
cs: number;
co: string;
pl: IProductV2DTO[];
ta: string;
ti: string;
tcc: string;
tr: number;
ts: number;
tt: number;
};
pm?: {
an: string;
pl: IPromotionV2DTO[];
};
pi?: IProductImpressionV2DTO[];
pet?: number;
}
export interface IProductV2DTO {
id: string | number;
nm: string | number;
pr: number;
qt: number;
br: string | number;
va: string | number;
ca: string | number;
ps: number;
cc: string | number;
tpa: number;
attrs: Dictionary<string> | null;
}
export interface IPromotionV2DTO {
id: string | number;
nm: string | number;
ps: string | number;
cr: string;
}
export interface IProductImpressionV2DTO {
pil: string;
pl: IProductV2DTO[];
}
export interface IUploadObject extends SDKEvent {
// TODO: References to `ClientGeneratedId` can be removed when we remove the
// V2 event upload path because it does not exist in the V3 payload
ClientGeneratedId: string;
// FIXME: This reference to Store is misleading and can be removed whenever
// we deprecate v2 related code
Store: ServerSettings;
ExpandedEventCount: number;
ProfileMessageType: number;
}
export interface IServerModel {
convertEventToV2DTO: (event: IUploadObject) => IServerV2DTO;
createEventObject: (event: BaseEvent, user?: IMParticleUser) => SDKEvent;
convertToConsentStateV2DTO: (state: SDKConsentState) => IConsentStateV2DTO;
}
// TODO: Make this a pure function that returns a new object
function convertCustomFlags(event: SDKEvent, dto: IServerV2DTO) {
var valueArray: string[] = [];
dto.flags = {};
for (var prop in event.CustomFlags) {
valueArray = [];
if (event.CustomFlags.hasOwnProperty(prop)) {
if (Array.isArray(event.CustomFlags[prop])) {
event.CustomFlags[prop].forEach(customFlagProperty => {
if (isValidCustomFlagProperty(customFlagProperty)) {
valueArray.push(customFlagProperty.toString());
}
});
} else if (isValidCustomFlagProperty(event.CustomFlags[prop])) {
valueArray.push(event.CustomFlags[prop].toString());
}
if (valueArray.length) {
dto.flags[prop] = valueArray;
}
}
}
}
function convertProductToV2DTO(product: SDKProduct): IProductV2DTO {
return {
id: parseStringOrNumber(product.Sku),
nm: parseStringOrNumber(product.Name),
pr: parseNumber(product.Price),
qt: parseNumber(product.Quantity),
br: parseStringOrNumber(product.Brand),
va: parseStringOrNumber(product.Variant),
ca: parseStringOrNumber(product.Category),
ps: parseNumber(product.Position),
cc: parseStringOrNumber(product.CouponCode),
tpa: parseNumber(product.TotalAmount),
attrs: product.Attributes as Dictionary<string> | null,
};
}
function convertProductListToV2DTO(productList: SDKProduct[]): IProductV2DTO[] {
if (!productList) {
return [];
}
return productList.map(function(product) {
return convertProductToV2DTO(product);
});
}
export default function ServerModel(
this: IServerModel,
mpInstance: IMParticleWebSDKInstance
) {
var self = this;
this.convertToConsentStateV2DTO = function(
state: SDKConsentState
): IConsentStateV2DTO {
if (!state) {
return null;
}
var jsonObject: IConsentStateV2DTO = {};
var gdprConsentState = state.getGDPRConsentState();
if (gdprConsentState) {
var gdpr: IGDPRConsentStateV2DTO = {};
jsonObject.gdpr = gdpr;
for (var purpose in gdprConsentState) {
if (gdprConsentState.hasOwnProperty(purpose)) {
var gdprConsent = gdprConsentState[purpose];
jsonObject.gdpr[purpose] = {} as IPrivacyV2DTO;
if (typeof gdprConsent.Consented === 'boolean') {
gdpr[purpose].c = gdprConsent.Consented;
}
if (typeof gdprConsent.Timestamp === 'number') {
gdpr[purpose].ts = gdprConsent.Timestamp;
}
if (typeof gdprConsent.ConsentDocument === 'string') {
gdpr[purpose].d = gdprConsent.ConsentDocument;
}
if (typeof gdprConsent.Location === 'string') {
gdpr[purpose].l = gdprConsent.Location;
}
if (typeof gdprConsent.HardwareId === 'string') {
gdpr[purpose].h = gdprConsent.HardwareId;
}
}
}
}
var ccpaConsentState = state.getCCPAConsentState();
if (ccpaConsentState) {
jsonObject.ccpa = {
data_sale_opt_out: {
c: ccpaConsentState.Consented,
ts: ccpaConsentState.Timestamp,
d: ccpaConsentState.ConsentDocument,
l: ccpaConsentState.Location,
h: ccpaConsentState.HardwareId,
},
};
}
return jsonObject as IConsentStateV2DTO;
};
this.createEventObject = function(
event: BaseEvent,
user?: IMParticleUser
): SDKEvent | IUploadObject {
var uploadObject: Partial<IUploadObject> = {};
var eventObject: Partial<SDKEvent> = {};
// The `optOut` variable is passed later in this method to the `uploadObject`
// so that it can be used to denote whether or not a user has "opted out" of being
// tracked. If this is an `optOut` Event, we set `optOut` to the inverse of the SDK's
// `isEnabled` boolean which is controlled via `MPInstanceManager.setOptOut`.
var optOut =
event.messageType === Types.MessageType.OptOut
? !mpInstance._Store.isEnabled
: null;
// TODO: Why is Webview Bridge Enabled or Opt Out necessary here?
if (
mpInstance._Store.sessionId ||
event.messageType === Types.MessageType.OptOut ||
mpInstance._Store.webviewBridgeEnabled
) {
let customFlags: SDKEventCustomFlags = {...event.customFlags};
let integrationAttributes: IntegrationAttributes = mpInstance._Store.integrationAttributes;
const { getFeatureFlag } = mpInstance._Helpers;
// https://go.mparticle.com/work/SQDSDKS-5053
// https://go.mparticle.com/work/SQDSDKS-7639
const integrationSpecificIds = getFeatureFlag && (getFeatureFlag(Constants.FeatureFlags.CaptureIntegrationSpecificIds) as boolean);
const integrationSpecificIdsV2 = getFeatureFlag && ((getFeatureFlag(Constants.FeatureFlags.CaptureIntegrationSpecificIdsV2) as string) || '');
const isIntegrationCaptureEnabled = (integrationSpecificIdsV2 && integrationSpecificIdsV2 !== Constants.CaptureIntegrationSpecificIdsV2Modes.None) || integrationSpecificIds === true;
if (isIntegrationCaptureEnabled) {
// Attempt to recapture click IDs in case a third party integration
// has added or updated new click IDs since the last event was sent.
mpInstance._IntegrationCapture.capture();
const transformedClickIDs = mpInstance._IntegrationCapture.getClickIdsAsCustomFlags();
customFlags = { ...transformedClickIDs, ...customFlags };
const transformedIntegrationAttributes = mpInstance._IntegrationCapture.getClickIdsAsIntegrationAttributes();
integrationAttributes = { ...transformedIntegrationAttributes, ...integrationAttributes };
}
if (event.hasOwnProperty('toEventAPIObject')) {
eventObject = event.toEventAPIObject();
} else {
eventObject = {
// This is an artifact from v2 events where SessionStart/End and AST event
// names are numbers (1, 2, or 10), but going forward with v3, these lifecycle
// events do not have names, but are denoted by their `event_type`
EventName:
event.name ||
String(event.messageType),
EventCategory: event.eventType,
EventAttributes: mpInstance._Helpers.sanitizeAttributes(
event.data,
event.name
),
ActiveTimeOnSite: mpInstance._timeOnSiteTimer?.getTimeInForeground(),
PageUrl: getHref() || null,
SourceMessageId:
event.sourceMessageId ||
mpInstance._Helpers.generateUniqueId(),
EventDataType: event.messageType,
CustomFlags: customFlags,
UserAttributeChanges: event.userAttributeChanges,
UserIdentityChanges: event.userIdentityChanges,
};
}
// TODO: Should we move this side effect outside of this method?
if (event.messageType !== Types.MessageType.SessionEnd) {
mpInstance._Store.dateLastEventSent = new Date();
}
uploadObject = {
// FIXME: Deprecate when we get rid of V2
Store: mpInstance._Store.serverSettings,
SDKVersion: Constants.sdkVersion,
SessionId: mpInstance._Store.sessionId,
SessionStartDate: mpInstance._Store.sessionStartDate
? mpInstance._Store.sessionStartDate.getTime()
: 0,
Debug: mpInstance._Store.SDKConfig.isDevelopmentMode,
Location: mpInstance._Store.currentPosition,
OptOut: optOut,
ExpandedEventCount: 0,
AppVersion: mpInstance.getAppVersion(),
AppName: mpInstance.getAppName(),
Package: mpInstance._Store.SDKConfig.package,
ClientGeneratedId: mpInstance._Store.clientId,
DeviceId: mpInstance._Store.deviceId,
IntegrationAttributes: integrationAttributes,
CurrencyCode: mpInstance._Store.currencyCode,
DataPlan: mpInstance._Store.SDKConfig.dataPlan
? mpInstance._Store.SDKConfig.dataPlan
: {},
};
if (eventObject.EventDataType === MessageType.AppStateTransition) {
eventObject.IsFirstRun = mpInstance._Store.isFirstRun;
eventObject.LaunchReferral = window.location.href || null;
}
// FIXME: Remove duplicate occurence
eventObject.CurrencyCode = mpInstance._Store.currencyCode;
var currentUser = user || mpInstance.Identity.getCurrentUser();
appendUserInfo(currentUser, eventObject as SDKEvent);
if (event.messageType === Types.MessageType.SessionEnd) {
eventObject.SessionLength =
mpInstance._Store.dateLastEventSent.getTime() -
mpInstance._Store.sessionStartDate.getTime();
eventObject.currentSessionMPIDs =
mpInstance._Store.currentSessionMPIDs;
// Session attributes are assigned on a session level, but only uploaded
// when a session ends. As there is no way to attach event attributes to
// a `SessionEnd` event, we are uploading the session level attributes
// as event level attributes in a `SessionEnd` event.
eventObject.EventAttributes =
mpInstance._Store.sessionAttributes;
// TODO: We should move this out of here to avoid side effects
mpInstance._Store.currentSessionMPIDs = [];
mpInstance._Store.sessionStartDate = null;
}
uploadObject.Timestamp = mpInstance._Store.dateLastEventSent.getTime();
return extend({}, eventObject, uploadObject);
}
return null;
};
this.convertEventToV2DTO = function(event: IUploadObject): IServerV2DTO {
var dto: Partial<IServerV2DTO> = {
n: event.EventName,
et: event.EventCategory,
ua: event.UserAttributes,
ui: event.UserIdentities,
ia: event.IntegrationAttributes,
str: event.Store,
attrs: event.EventAttributes,
sdk: event.SDKVersion,
sid: event.SessionId,
sl: event.SessionLength,
ssd: event.SessionStartDate,
dt: event.EventDataType,
dbg: event.Debug,
ct: event.Timestamp,
lc: event.Location,
o: event.OptOut,
eec: event.ExpandedEventCount,
av: event.AppVersion,
cgid: event.ClientGeneratedId,
das: event.DeviceId,
mpid: event.MPID,
smpids: event.currentSessionMPIDs,
};
if (event.DataPlan && event.DataPlan.PlanId) {
dto.dp_id = event.DataPlan.PlanId;
if (event.DataPlan.PlanVersion) {
dto.dp_v = event.DataPlan.PlanVersion;
}
}
var consent = self.convertToConsentStateV2DTO(event.ConsentState);
if (consent) {
dto.con = consent;
}
if (event.EventDataType === MessageType.AppStateTransition) {
dto.fr = event.IsFirstRun;
dto.iu = false;
dto.at = ApplicationTransitionType.AppInit;
dto.lr = event.LaunchReferral;
// Nullify Attributes in case AST Was logged manually or
// via logBaseEvent. AST should not have any attributes
dto.attrs = null;
}
if (event.CustomFlags) {
convertCustomFlags(event, dto as IServerV2DTO);
}
if (event.EventDataType === MessageType.Commerce) {
dto.cu = event.CurrencyCode;
// TODO: If Cart is deprecated, we should deprecate this too
if (event.ShoppingCart) {
dto.sc = {
pl: convertProductListToV2DTO(
event.ShoppingCart.ProductList
),
};
}
if (event.ProductAction) {
dto.pd = {
an: event.ProductAction.ProductActionType,
cs: mpInstance._Helpers.parseNumber(
event.ProductAction.CheckoutStep
),
co: event.ProductAction.CheckoutOptions,
pl: convertProductListToV2DTO(
event.ProductAction.ProductList
),
ti: event.ProductAction.TransactionId,
ta: event.ProductAction.Affiliation,
tcc: event.ProductAction.CouponCode,
tr: mpInstance._Helpers.parseNumber(
event.ProductAction.TotalAmount
),
ts: mpInstance._Helpers.parseNumber(
event.ProductAction.ShippingAmount
),
tt: mpInstance._Helpers.parseNumber(
event.ProductAction.TaxAmount
),
};
} else if (event.PromotionAction) {
dto.pm = {
an: event.PromotionAction.PromotionActionType,
pl: event.PromotionAction.PromotionList.map(function(
promotion
) {
return {
id: promotion.Id,
nm: promotion.Name,
cr: promotion.Creative,
ps: promotion.Position ? promotion.Position : 0,
};
}),
};
} else if (event.ProductImpressions) {
dto.pi = event.ProductImpressions.map(function(impression) {
return {
pil: impression.ProductImpressionList,
pl: convertProductListToV2DTO(impression.ProductList),
};
});
}
} else if (event.EventDataType === MessageType.Profile) {
dto.pet = event.ProfileMessageType;
}
return dto as IServerV2DTO;
};
}