@mparticle/web-sdk 2.32.1 → 2.32.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts ADDED
@@ -0,0 +1,404 @@
1
+ import Constants from './constants';
2
+ import { isNumber, parseNumber, valueof } from './utils';
3
+
4
+ interface IdentitiesByType {
5
+ [key: number]: string;
6
+ }
7
+
8
+ export const MessageType = {
9
+ SessionStart: 1 as const,
10
+ SessionEnd: 2 as const,
11
+ PageView: 3 as const,
12
+ PageEvent: 4 as const,
13
+ CrashReport: 5 as const,
14
+ OptOut: 6 as const,
15
+ AppStateTransition: 10 as const,
16
+ Profile: 14 as const,
17
+ Commerce: 16 as const,
18
+ Media: 20 as const,
19
+ UserAttributeChange: 17 as const,
20
+ UserIdentityChange: 18 as const,
21
+ };
22
+
23
+ export const EventType = {
24
+ Unknown: 0 as const,
25
+ Navigation: 1 as const,
26
+ Location: 2 as const,
27
+ Search: 3 as const,
28
+ Transaction: 4 as const,
29
+ UserContent: 5 as const,
30
+ UserPreference: 6 as const,
31
+ Social: 7 as const,
32
+ Other: 8 as const,
33
+ Media: 9 as const,
34
+
35
+ getName(id: number): string {
36
+ switch (id) {
37
+ case EventType.Unknown:
38
+ return 'Unknown';
39
+ case EventType.Navigation:
40
+ return 'Navigation';
41
+ case EventType.Location:
42
+ return 'Location';
43
+ case EventType.Search:
44
+ return 'Search';
45
+ case EventType.Transaction:
46
+ return 'Transaction';
47
+ case EventType.UserContent:
48
+ return 'User Content';
49
+ case EventType.UserPreference:
50
+ return 'User Preference';
51
+ case EventType.Social:
52
+ return 'Social';
53
+ case CommerceEventType.ProductAddToCart:
54
+ return 'Product Added to Cart';
55
+ case CommerceEventType.ProductAddToWishlist:
56
+ return 'Product Added to Wishlist';
57
+ case CommerceEventType.ProductCheckout:
58
+ return 'Product Checkout';
59
+ case CommerceEventType.ProductCheckoutOption:
60
+ return 'Product Checkout Options';
61
+ case CommerceEventType.ProductClick:
62
+ return 'Product Click';
63
+ case CommerceEventType.ProductImpression:
64
+ return 'Product Impression';
65
+ case CommerceEventType.ProductPurchase:
66
+ return 'Product Purchased';
67
+ case CommerceEventType.ProductRefund:
68
+ return 'Product Refunded';
69
+ case CommerceEventType.ProductRemoveFromCart:
70
+ return 'Product Removed From Cart';
71
+ case CommerceEventType.ProductRemoveFromWishlist:
72
+ return 'Product Removed from Wishlist';
73
+ case CommerceEventType.ProductViewDetail:
74
+ return 'Product View Details';
75
+ case CommerceEventType.PromotionClick:
76
+ return 'Promotion Click';
77
+ case CommerceEventType.PromotionView:
78
+ return 'Promotion View';
79
+ default:
80
+ return 'Other';
81
+ }
82
+ },
83
+ };
84
+
85
+ // Continuation of EventType enum above, but in seperate object since we don't expose these to end user
86
+ export const CommerceEventType = {
87
+ ProductAddToCart: 10 as const,
88
+ ProductRemoveFromCart: 11 as const,
89
+ ProductCheckout: 12 as const,
90
+ ProductCheckoutOption: 13 as const,
91
+ ProductClick: 14 as const,
92
+ ProductViewDetail: 15 as const,
93
+ ProductPurchase: 16 as const,
94
+ ProductRefund: 17 as const,
95
+ PromotionView: 18 as const,
96
+ PromotionClick: 19 as const,
97
+ ProductAddToWishlist: 20 as const,
98
+ ProductRemoveFromWishlist: 21 as const,
99
+ ProductImpression: 22 as const,
100
+ };
101
+
102
+ export const IdentityType = {
103
+ Other: 0 as const,
104
+ CustomerId: 1 as const,
105
+ Facebook: 2 as const,
106
+ Twitter: 3 as const,
107
+ Google: 4 as const,
108
+ Microsoft: 5 as const,
109
+ Yahoo: 6 as const,
110
+ Email: 7 as const,
111
+ FacebookCustomAudienceId: 9 as const,
112
+ Other2: 10 as const,
113
+ Other3: 11 as const,
114
+ Other4: 12 as const,
115
+ Other5: 13 as const,
116
+ Other6: 14 as const,
117
+ Other7: 15 as const,
118
+ Other8: 16 as const,
119
+ Other9: 17 as const,
120
+ Other10: 18 as const,
121
+ MobileNumber: 19 as const,
122
+ PhoneNumber2: 20 as const,
123
+ PhoneNumber3: 21 as const,
124
+
125
+ isValid(identityType: number): boolean {
126
+ if (typeof identityType === 'number') {
127
+ for (const prop in IdentityType) {
128
+ if (IdentityType.hasOwnProperty(prop)) {
129
+ if (IdentityType[prop] === identityType) {
130
+ return true;
131
+ }
132
+ }
133
+ }
134
+ }
135
+
136
+ return false;
137
+ },
138
+
139
+ getName: (identityType: number): string => {
140
+ switch (identityType) {
141
+ case window.mParticle.IdentityType.CustomerId:
142
+ return 'Customer ID';
143
+ case window.mParticle.IdentityType.Facebook:
144
+ return 'Facebook ID';
145
+ case window.mParticle.IdentityType.Twitter:
146
+ return 'Twitter ID';
147
+ case window.mParticle.IdentityType.Google:
148
+ return 'Google ID';
149
+ case window.mParticle.IdentityType.Microsoft:
150
+ return 'Microsoft ID';
151
+ case window.mParticle.IdentityType.Yahoo:
152
+ return 'Yahoo ID';
153
+ case window.mParticle.IdentityType.Email:
154
+ return 'Email';
155
+ case window.mParticle.IdentityType.FacebookCustomAudienceId:
156
+ return 'Facebook App User ID';
157
+ default:
158
+ return 'Other ID';
159
+ }
160
+ },
161
+
162
+ getIdentityType: (
163
+ identityName: string
164
+ ): valueof<typeof IdentityType> | boolean => {
165
+ switch (identityName) {
166
+ case 'other':
167
+ return IdentityType.Other;
168
+ case 'customerid':
169
+ return IdentityType.CustomerId;
170
+ case 'facebook':
171
+ return IdentityType.Facebook;
172
+ case 'twitter':
173
+ return IdentityType.Twitter;
174
+ case 'google':
175
+ return IdentityType.Google;
176
+ case 'microsoft':
177
+ return IdentityType.Microsoft;
178
+ case 'yahoo':
179
+ return IdentityType.Yahoo;
180
+ case 'email':
181
+ return IdentityType.Email;
182
+ case 'facebookcustomaudienceid':
183
+ return IdentityType.FacebookCustomAudienceId;
184
+ case 'other2':
185
+ return IdentityType.Other2;
186
+ case 'other3':
187
+ return IdentityType.Other3;
188
+ case 'other4':
189
+ return IdentityType.Other4;
190
+ case 'other5':
191
+ return IdentityType.Other5;
192
+ case 'other6':
193
+ return IdentityType.Other6;
194
+ case 'other7':
195
+ return IdentityType.Other7;
196
+ case 'other8':
197
+ return IdentityType.Other8;
198
+ case 'other9':
199
+ return IdentityType.Other9;
200
+ case 'other10':
201
+ return IdentityType.Other10;
202
+ case 'mobile_number':
203
+ return IdentityType.MobileNumber;
204
+ case 'phone_number_2':
205
+ return IdentityType.PhoneNumber2;
206
+ case 'phone_number_3':
207
+ return IdentityType.PhoneNumber3;
208
+ default:
209
+ return false;
210
+ }
211
+ },
212
+
213
+ getIdentityName: (identityType: number): string | null => {
214
+ switch (identityType) {
215
+ case IdentityType.Other:
216
+ return 'other';
217
+ case IdentityType.CustomerId:
218
+ return 'customerid';
219
+ case IdentityType.Facebook:
220
+ return 'facebook';
221
+ case IdentityType.Twitter:
222
+ return 'twitter';
223
+ case IdentityType.Google:
224
+ return 'google';
225
+ case IdentityType.Microsoft:
226
+ return 'microsoft';
227
+ case IdentityType.Yahoo:
228
+ return 'yahoo';
229
+ case IdentityType.Email:
230
+ return 'email';
231
+ case IdentityType.FacebookCustomAudienceId:
232
+ return 'facebookcustomaudienceid';
233
+ case IdentityType.Other2:
234
+ return 'other2';
235
+ case IdentityType.Other3:
236
+ return 'other3';
237
+ case IdentityType.Other4:
238
+ return 'other4';
239
+ case IdentityType.Other5:
240
+ return 'other5';
241
+ case IdentityType.Other6:
242
+ return 'other6';
243
+ case IdentityType.Other7:
244
+ return 'other7';
245
+ case IdentityType.Other8:
246
+ return 'other8';
247
+ case IdentityType.Other9:
248
+ return 'other9';
249
+ case IdentityType.Other10:
250
+ return 'other10';
251
+ case IdentityType.MobileNumber:
252
+ return 'mobile_number';
253
+ case IdentityType.PhoneNumber2:
254
+ return 'phone_number_2';
255
+ case IdentityType.PhoneNumber3:
256
+ return 'phone_number_3';
257
+ default:
258
+ return null;
259
+ }
260
+ },
261
+
262
+ // Strips out functions from Identity Types for easier lookups
263
+ getValuesAsStrings: (): string[] =>
264
+ Object.values(IdentityType)
265
+ .map(value => (isNumber(value) ? value.toString() : undefined))
266
+ .filter(value => value !== undefined) as string[],
267
+
268
+ getNewIdentitiesByName: (
269
+ newIdentitiesByType: IdentitiesByType
270
+ ): IdentitiesByType => {
271
+ const newIdentitiesByName: IdentitiesByType = {};
272
+ const identityTypeValuesAsStrings: string[] = IdentityType.getValuesAsStrings();
273
+
274
+ for (const key in newIdentitiesByType) {
275
+ // IdentityTypes are stored as numbers but are passed in as strings
276
+ if (identityTypeValuesAsStrings.includes(key)) {
277
+ const identityNameKey = IdentityType.getIdentityName(
278
+ parseNumber(key)
279
+ );
280
+ newIdentitiesByName[identityNameKey] = newIdentitiesByType[key];
281
+ }
282
+ }
283
+
284
+ return newIdentitiesByName;
285
+ },
286
+ };
287
+
288
+ export const ProductActionType = {
289
+ Unknown: 0 as const,
290
+ AddToCart: 1 as const,
291
+ RemoveFromCart: 2 as const,
292
+ Checkout: 3 as const,
293
+ CheckoutOption: 4 as const,
294
+ Click: 5 as const,
295
+ ViewDetail: 6 as const,
296
+ Purchase: 7 as const,
297
+ Refund: 8 as const,
298
+ AddToWishlist: 9 as const,
299
+ RemoveFromWishlist: 10 as const,
300
+
301
+ getName: (id: number): string => {
302
+ switch (id) {
303
+ case ProductActionType.AddToCart:
304
+ return 'Add to Cart';
305
+ case ProductActionType.RemoveFromCart:
306
+ return 'Remove from Cart';
307
+ case ProductActionType.Checkout:
308
+ return 'Checkout';
309
+ case ProductActionType.CheckoutOption:
310
+ return 'Checkout Option';
311
+ case ProductActionType.Click:
312
+ return 'Click';
313
+ case ProductActionType.ViewDetail:
314
+ return 'View Detail';
315
+ case ProductActionType.Purchase:
316
+ return 'Purchase';
317
+ case ProductActionType.Refund:
318
+ return 'Refund';
319
+ case ProductActionType.AddToWishlist:
320
+ return 'Add to Wishlist';
321
+ case ProductActionType.RemoveFromWishlist:
322
+ return 'Remove from Wishlist';
323
+ default:
324
+ return 'Unknown';
325
+ }
326
+ },
327
+
328
+ // these are the action names used by server and mobile SDKs when expanding a CommerceEvent
329
+ getExpansionName: (id: number) => {
330
+ switch (id) {
331
+ case ProductActionType.AddToCart:
332
+ return 'add_to_cart';
333
+ case ProductActionType.RemoveFromCart:
334
+ return 'remove_from_cart';
335
+ case ProductActionType.Checkout:
336
+ return 'checkout';
337
+ case ProductActionType.CheckoutOption:
338
+ return 'checkout_option';
339
+ case ProductActionType.Click:
340
+ return 'click';
341
+ case ProductActionType.ViewDetail:
342
+ return 'view_detail';
343
+ case ProductActionType.Purchase:
344
+ return 'purchase';
345
+ case ProductActionType.Refund:
346
+ return 'refund';
347
+ case ProductActionType.AddToWishlist:
348
+ return 'add_to_wishlist';
349
+ case ProductActionType.RemoveFromWishlist:
350
+ return 'remove_from_wishlist';
351
+ default:
352
+ return 'unknown';
353
+ }
354
+ },
355
+ };
356
+
357
+ export const PromotionActionType = {
358
+ Unknown: 0 as const,
359
+ PromotionView: 1 as const,
360
+ PromotionClick: 2 as const,
361
+
362
+ getName: (id: number): string => {
363
+ switch (id) {
364
+ case PromotionActionType.PromotionView:
365
+ return 'view';
366
+ case PromotionActionType.PromotionClick:
367
+ return 'click';
368
+ default:
369
+ return 'unknown';
370
+ }
371
+ },
372
+
373
+ // these are the names that the server and mobile SDKs use while expanding CommerceEvent
374
+ getExpansionName: (id: number): string => {
375
+ switch (id) {
376
+ case PromotionActionType.PromotionView:
377
+ return 'view';
378
+ case PromotionActionType.PromotionClick:
379
+ return 'click';
380
+ default:
381
+ return 'unknown';
382
+ }
383
+ },
384
+ };
385
+
386
+ export const ProfileMessageType = {
387
+ Logout: 3 as const,
388
+ };
389
+
390
+ export const ApplicationTransitionType = {
391
+ AppInit: 1 as const,
392
+ };
393
+
394
+ export default {
395
+ MessageType,
396
+ EventType,
397
+ CommerceEventType,
398
+ IdentityType,
399
+ ProfileMessageType,
400
+ ApplicationTransitionType,
401
+ ProductActionType,
402
+ PromotionActionType,
403
+ Environment: Constants.Environment,
404
+ } as const;
package/src/uploaders.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  type HTTPMethod = 'get' | 'post';
2
2
 
3
- export interface fetchPayload {
3
+ export interface IFetchPayload {
4
4
  method: string;
5
5
  headers: {
6
6
  Accept: string;
@@ -12,7 +12,7 @@ export interface fetchPayload {
12
12
  export abstract class AsyncUploader {
13
13
  url: string;
14
14
  public abstract upload(
15
- fetchPayload: fetchPayload,
15
+ fetchPayload: IFetchPayload,
16
16
  url?: string
17
17
  ): Promise<Response>;
18
18
 
@@ -23,7 +23,7 @@ export abstract class AsyncUploader {
23
23
 
24
24
  export class FetchUploader extends AsyncUploader {
25
25
  public async upload(
26
- fetchPayload: fetchPayload,
26
+ fetchPayload: IFetchPayload,
27
27
  _url?: string
28
28
  ): Promise<Response> {
29
29
  const url = _url || this.url;
@@ -32,7 +32,7 @@ export class FetchUploader extends AsyncUploader {
32
32
  }
33
33
 
34
34
  export class XHRUploader extends AsyncUploader {
35
- public async upload(fetchPayload: fetchPayload): Promise<Response> {
35
+ public async upload(fetchPayload: IFetchPayload): Promise<Response> {
36
36
  const response: Response = await this.makeRequest(
37
37
  this.url,
38
38
  fetchPayload.body,
package/src/utils.ts CHANGED
@@ -8,7 +8,7 @@ type valueof<T> = T[keyof T];
8
8
  // Placeholder for Dictionary-like Types
9
9
  export type Dictionary<V = any> = Record<string, V>;
10
10
 
11
- export type Environment = 'development' | 'production';
11
+ export type Environment = valueof<typeof Constants.Environment>;
12
12
 
13
13
  const createCookieString = (value: string): string =>
14
14
  replaceCommasWithPipes(replaceQuotesWithApostrophes(value));
@@ -1,90 +0,0 @@
1
- import { IAliasRequest, IAliasCallback } from "./identity.interfaces";
2
- import { MParticleWebSDK } from "./sdkRuntimeModels";
3
- import Constants from './constants';
4
- import { FetchUploader, XHRUploader } from './uploaders';
5
- import { HTTP_ACCEPTED, HTTP_OK } from "./constants";
6
- import { IIdentityApiClientSendAliasRequest } from "./identityApiClient.interfaces";
7
-
8
-
9
- const { HTTPCodes, Messages } = Constants;
10
-
11
- interface IAliasResponseBody {
12
- message?: string
13
- }
14
-
15
- export const sendAliasRequest: IIdentityApiClientSendAliasRequest = async function (mpInstance: MParticleWebSDK, aliasRequest: IAliasRequest, aliasCallback: IAliasCallback): Promise<void> {
16
- const { verbose, error } = mpInstance.Logger;
17
- const { invokeAliasCallback } = mpInstance._Helpers;
18
- const { aliasUrl } = mpInstance._Store.SDKConfig;
19
- const { devToken: apiKey } = mpInstance._Store;
20
-
21
- verbose(Messages.InformationMessages.SendAliasHttp);
22
-
23
- // https://go.mparticle.com/work/SQDSDKS-6750
24
- const uploadUrl = `https://${aliasUrl}${apiKey}/Alias`;
25
- const uploader = window.fetch
26
- ? new FetchUploader(uploadUrl)
27
- : new XHRUploader(uploadUrl);
28
-
29
-
30
- // https://go.mparticle.com/work/SQDSDKS-6568
31
- const uploadPayload = {
32
- method: 'post',
33
- headers: {
34
- Accept: 'text/plain;charset=UTF-8',
35
- 'Content-Type': 'application/json',
36
- },
37
- body: JSON.stringify(aliasRequest),
38
- };
39
-
40
- try {
41
- const response = await uploader.upload(uploadPayload);
42
-
43
- let message: string;
44
- let aliasResponseBody: IAliasResponseBody;
45
-
46
- // FetchUploader returns the response as a JSON object that we have to await
47
- if (response.json) {
48
- // HTTP responses of 202, 200, and 403 do not have a response. response.json will always exist on a fetch, but can only be await-ed when the response is not empty, otherwise it will throw an error.
49
- try {
50
- aliasResponseBody = await response.json();
51
- } catch (e) {
52
- verbose('The request has no response body');
53
- }
54
- } else {
55
- // https://go.mparticle.com/work/SQDSDKS-6568
56
- // XHRUploader returns the response as a string that we need to parse
57
- const xhrResponse = response as unknown as XMLHttpRequest;
58
-
59
- aliasResponseBody = xhrResponse.responseText
60
- ? JSON.parse(xhrResponse.responseText)
61
- : '';
62
- }
63
-
64
- let errorMessage: string;
65
-
66
- switch (response.status) {
67
- case HTTP_OK:
68
- case HTTP_ACCEPTED:
69
- // https://go.mparticle.com/work/SQDSDKS-6670
70
- message =
71
- 'Successfully sent forwarding stats to mParticle Servers';
72
- break;
73
- default:
74
- // 400 has an error message, but 403 doesn't
75
- if (aliasResponseBody?.message) {
76
- errorMessage = aliasResponseBody.message;
77
- }
78
- message =
79
- 'Issue with sending Alias Request to mParticle Servers, received HTTP Code of ' +
80
- response.status;
81
- }
82
-
83
- verbose(message);
84
- invokeAliasCallback(aliasCallback, response.status, errorMessage);
85
- } catch (e) {
86
- const err = e as Error;
87
- error('Error sending alias request to mParticle servers. ' + err);
88
- invokeAliasCallback(aliasCallback, HTTPCodes.noHttpCoverage, (err.message));
89
- }
90
- };
@@ -1,44 +0,0 @@
1
- import { IdentityApiData, MPID, UserIdentities } from '@mparticle/web-sdk';
2
- import {
3
- IdentityCallback,
4
- IIdentityResponse,
5
- } from './identity-user-interfaces';
6
- import {
7
- IAliasRequest,
8
- IAliasCallback,
9
- IIdentityRequest,
10
- IdentityAPIMethod,
11
- IIdentity,
12
- } from './identity.interfaces';
13
- import { MParticleWebSDK } from './sdkRuntimeModels';
14
-
15
- export interface IIdentityApiClient {
16
- sendAliasRequest: (
17
- aliasRequest: IAliasRequest,
18
- aliasCallback: IAliasCallback
19
- ) => Promise<void>;
20
- sendIdentityRequest: (
21
- identityApiRequest: IIdentityRequest,
22
- method: IdentityAPIMethod,
23
- callback: IdentityCallback,
24
- originalIdentityApiData: IdentityApiData,
25
- parseIdentityResponse: IIdentity['parseIdentityResponse'],
26
- mpid: MPID,
27
- knownIdentities: UserIdentities
28
- ) => Promise<void>;
29
- getUploadUrl: (method: IdentityAPIMethod, mpid: MPID) => string;
30
- getIdentityResponseFromFetch: (
31
- response: Response,
32
- responseBody: string
33
- ) => IIdentityResponse;
34
- getIdentityResponseFromXHR: (response: Response) => IIdentityResponse;
35
- }
36
-
37
- // https://go.mparticle.com/work/SQDSDKS-6568
38
- // https://go.mparticle.com/work/SQDSDKS-6679
39
- // Combine with `sendIdentityRequest` above once module is fully migrated
40
- export type IIdentityApiClientSendAliasRequest = (
41
- mpInstance: MParticleWebSDK,
42
- aliasRequest: IAliasRequest,
43
- aliasCallback: IAliasCallback
44
- ) => Promise<void>;