@mparticle/web-sdk 2.21.0 → 2.21.2
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/dist/mparticle.common.js +41 -53
- package/dist/mparticle.esm.js +41 -53
- package/dist/mparticle.js +84 -360
- package/package.json +23 -11
- package/src/apiClient.ts +8 -71
- package/src/constants.ts +0 -1
- package/src/identity.js +35 -62
- package/src/mockBatchCreator.ts +6 -8
- package/src/mp-instance.js +3 -27
- package/src/persistence.interfaces.ts +3 -9
- package/src/persistence.js +5 -30
- package/src/sdkRuntimeModels.ts +5 -1
- package/src/store.ts +5 -17
- package/src/utils.ts +22 -0
- package/src/migrations.js +0 -279
package/src/utils.ts
CHANGED
|
@@ -5,6 +5,12 @@ type valueof<T> = T[keyof T];
|
|
|
5
5
|
// Placeholder for Dictionary-like Types
|
|
6
6
|
export type Dictionary<V = any> = Record<string, V>;
|
|
7
7
|
|
|
8
|
+
const createCookieString = (value: string): string =>
|
|
9
|
+
replaceCommasWithPipes(replaceQuotesWithApostrophes(value));
|
|
10
|
+
|
|
11
|
+
const revertCookieString = (value: string): string =>
|
|
12
|
+
replacePipesWithCommas(replaceApostrophesWithQuotes(value));
|
|
13
|
+
|
|
8
14
|
const inArray = (items: any[], name: string): boolean => {
|
|
9
15
|
let i = 0;
|
|
10
16
|
|
|
@@ -134,6 +140,16 @@ const parseStringOrNumber = (
|
|
|
134
140
|
}
|
|
135
141
|
};
|
|
136
142
|
|
|
143
|
+
const replaceCommasWithPipes = (value: string): string => value.replace(/,/g, '|');
|
|
144
|
+
|
|
145
|
+
const replacePipesWithCommas = (value: string): string => value.replace(/\|/g, ',');
|
|
146
|
+
|
|
147
|
+
const replaceApostrophesWithQuotes = (value: string): string =>
|
|
148
|
+
value.replace(/\'/g, '"');
|
|
149
|
+
|
|
150
|
+
const replaceQuotesWithApostrophes = (value: string): string =>
|
|
151
|
+
value.replace(/\"/g, "'");
|
|
152
|
+
|
|
137
153
|
// FIXME: REFACTOR for V3
|
|
138
154
|
// only used in store.js to sanitize server-side formatting of
|
|
139
155
|
// booleans when checking for `isDevelopmentMode`
|
|
@@ -172,6 +188,8 @@ const isEmpty = (value: Dictionary<any> | null | undefined): boolean =>
|
|
|
172
188
|
value == null || !(Object.keys(value) || value).length;
|
|
173
189
|
|
|
174
190
|
export {
|
|
191
|
+
createCookieString,
|
|
192
|
+
revertCookieString,
|
|
175
193
|
valueof,
|
|
176
194
|
converted,
|
|
177
195
|
decoded,
|
|
@@ -184,6 +202,10 @@ export {
|
|
|
184
202
|
isStringOrNumber,
|
|
185
203
|
parseNumber,
|
|
186
204
|
parseStringOrNumber,
|
|
205
|
+
replaceCommasWithPipes,
|
|
206
|
+
replacePipesWithCommas,
|
|
207
|
+
replaceApostrophesWithQuotes,
|
|
208
|
+
replaceQuotesWithApostrophes,
|
|
187
209
|
returnConvertedBoolean,
|
|
188
210
|
isString,
|
|
189
211
|
isNumber,
|
package/src/migrations.js
DELETED
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
import Constants from './constants';
|
|
2
|
-
import Polyfill from './polyfill';
|
|
3
|
-
|
|
4
|
-
var StorageNames = Constants.StorageNames,
|
|
5
|
-
Base64 = Polyfill.Base64,
|
|
6
|
-
CookiesGlobalSettingsKeys = {
|
|
7
|
-
das: 1,
|
|
8
|
-
},
|
|
9
|
-
MPIDKeys = {
|
|
10
|
-
ui: 1,
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default function Migrations(mpInstance) {
|
|
14
|
-
var self = this;
|
|
15
|
-
// if there is a cookie or localStorage:
|
|
16
|
-
// 1. determine which version it is ('mprtcl-api', 'mprtcl-v2', 'mprtcl-v3', 'mprtcl-v4')
|
|
17
|
-
// 2. return if 'mprtcl-v4', otherwise migrate to mprtclv4 schema
|
|
18
|
-
// 3. if 'mprtcl-api', could be JSSDKv2 or JSSDKv1. JSSDKv2 cookie has a 'globalSettings' key on it
|
|
19
|
-
this.migrate = function() {
|
|
20
|
-
try {
|
|
21
|
-
migrateCookies();
|
|
22
|
-
} catch (e) {
|
|
23
|
-
mpInstance._Persistence.expireCookies(StorageNames.cookieNameV3);
|
|
24
|
-
mpInstance._Persistence.expireCookies(StorageNames.cookieNameV4);
|
|
25
|
-
mpInstance.Logger.error('Error migrating cookie: ' + e);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (mpInstance._Store.isLocalStorageAvailable) {
|
|
29
|
-
try {
|
|
30
|
-
migrateLocalStorage();
|
|
31
|
-
} catch (e) {
|
|
32
|
-
localStorage.removeItem(StorageNames.localStorageNameV3);
|
|
33
|
-
localStorage.removeItem(StorageNames.localStorageNameV4);
|
|
34
|
-
mpInstance.Logger.error('Error migrating localStorage: ' + e);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
function migrateCookies() {
|
|
40
|
-
var cookies = window.document.cookie.split('; '),
|
|
41
|
-
foundCookie,
|
|
42
|
-
i,
|
|
43
|
-
l,
|
|
44
|
-
parts,
|
|
45
|
-
name,
|
|
46
|
-
cookie;
|
|
47
|
-
|
|
48
|
-
mpInstance.Logger.verbose(
|
|
49
|
-
Constants.Messages.InformationMessages.CookieSearch
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
for (i = 0, l = cookies.length; i < l; i++) {
|
|
53
|
-
try {
|
|
54
|
-
parts = cookies[i].split('=');
|
|
55
|
-
name = mpInstance._Helpers.decoded(parts.shift());
|
|
56
|
-
cookie = mpInstance._Helpers.decoded(parts.join('='));
|
|
57
|
-
} catch (e) {
|
|
58
|
-
mpInstance.Logger.verbose(
|
|
59
|
-
'Unable to parse cookie: ' + name + '. Skipping.'
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
//most recent version needs no migration
|
|
64
|
-
if (name === mpInstance._Store.storageName) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (name === StorageNames.cookieNameV4) {
|
|
68
|
-
// adds cookies to new namespace, removes previous cookie
|
|
69
|
-
finishCookieMigration(cookie, StorageNames.cookieNameV4);
|
|
70
|
-
if (mpInstance._Store.isLocalStorageAvailable) {
|
|
71
|
-
migrateProductsToNameSpace();
|
|
72
|
-
}
|
|
73
|
-
return;
|
|
74
|
-
// migration path for SDKv1CookiesV3, doesn't need to be encoded
|
|
75
|
-
}
|
|
76
|
-
if (name === StorageNames.cookieNameV3) {
|
|
77
|
-
foundCookie = self.convertSDKv1CookiesV3ToSDKv2CookiesV4(
|
|
78
|
-
cookie
|
|
79
|
-
);
|
|
80
|
-
finishCookieMigration(foundCookie, StorageNames.cookieNameV3);
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function finishCookieMigration(cookie, cookieName) {
|
|
87
|
-
var date = new Date(),
|
|
88
|
-
cookieDomain = mpInstance._Persistence.getCookieDomain(),
|
|
89
|
-
expires,
|
|
90
|
-
domain;
|
|
91
|
-
|
|
92
|
-
expires = new Date(
|
|
93
|
-
date.getTime() + StorageNames.CookieExpiration * 24 * 60 * 60 * 1000
|
|
94
|
-
).toGMTString();
|
|
95
|
-
|
|
96
|
-
if (cookieDomain === '') {
|
|
97
|
-
domain = '';
|
|
98
|
-
} else {
|
|
99
|
-
domain = ';domain=' + cookieDomain;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
mpInstance.Logger.verbose(
|
|
103
|
-
Constants.Messages.InformationMessages.CookieSet
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
window.document.cookie =
|
|
107
|
-
encodeURIComponent(mpInstance._Store.storageName) +
|
|
108
|
-
'=' +
|
|
109
|
-
cookie +
|
|
110
|
-
';expires=' +
|
|
111
|
-
expires +
|
|
112
|
-
';path=/' +
|
|
113
|
-
domain;
|
|
114
|
-
|
|
115
|
-
mpInstance._Persistence.expireCookies(cookieName);
|
|
116
|
-
mpInstance._Store.migratingToIDSyncCookies = true;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
this.convertSDKv1CookiesV3ToSDKv2CookiesV4 = function(SDKv1CookiesV3) {
|
|
120
|
-
SDKv1CookiesV3 = mpInstance._Persistence.replacePipesWithCommas(
|
|
121
|
-
mpInstance._Persistence.replaceApostrophesWithQuotes(SDKv1CookiesV3)
|
|
122
|
-
);
|
|
123
|
-
var parsedSDKv1CookiesV3 = JSON.parse(SDKv1CookiesV3);
|
|
124
|
-
var parsedCookiesV4 = JSON.parse(restructureToV4Cookie(SDKv1CookiesV3));
|
|
125
|
-
|
|
126
|
-
if (parsedSDKv1CookiesV3.mpid) {
|
|
127
|
-
parsedCookiesV4.gs.csm.push(parsedSDKv1CookiesV3.mpid);
|
|
128
|
-
// all other values are already encoded, so we have to encode any new values
|
|
129
|
-
parsedCookiesV4.gs.csm = Base64.encode(
|
|
130
|
-
JSON.stringify(parsedCookiesV4.gs.csm)
|
|
131
|
-
);
|
|
132
|
-
migrateProductsFromSDKv1ToSDKv2CookiesV4(
|
|
133
|
-
parsedSDKv1CookiesV3,
|
|
134
|
-
parsedSDKv1CookiesV3.mpid
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return JSON.stringify(parsedCookiesV4);
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
function restructureToV4Cookie(cookies) {
|
|
142
|
-
try {
|
|
143
|
-
var cookiesV4Schema = { gs: { csm: [] } };
|
|
144
|
-
cookies = JSON.parse(cookies);
|
|
145
|
-
|
|
146
|
-
for (var key in cookies) {
|
|
147
|
-
if (cookies.hasOwnProperty(key)) {
|
|
148
|
-
if (CookiesGlobalSettingsKeys[key]) {
|
|
149
|
-
cookiesV4Schema.gs[key] = cookies[key];
|
|
150
|
-
} else if (key === 'mpid') {
|
|
151
|
-
cookiesV4Schema.cu = cookies[key];
|
|
152
|
-
} else if (cookies.mpid) {
|
|
153
|
-
cookiesV4Schema[cookies.mpid] =
|
|
154
|
-
cookiesV4Schema[cookies.mpid] || {};
|
|
155
|
-
if (MPIDKeys[key]) {
|
|
156
|
-
cookiesV4Schema[cookies.mpid][key] = cookies[key];
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return JSON.stringify(cookiesV4Schema);
|
|
162
|
-
} catch (e) {
|
|
163
|
-
mpInstance.Logger.error(
|
|
164
|
-
'Failed to restructure previous cookie into most current cookie schema'
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function migrateProductsToNameSpace() {
|
|
170
|
-
var lsProdV4Name = StorageNames.localStorageProductsV4;
|
|
171
|
-
var products = localStorage.getItem(
|
|
172
|
-
StorageNames.localStorageProductsV4
|
|
173
|
-
);
|
|
174
|
-
localStorage.setItem(mpInstance._Store.prodStorageName, products);
|
|
175
|
-
localStorage.removeItem(lsProdV4Name);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function migrateProductsFromSDKv1ToSDKv2CookiesV4(cookies, mpid) {
|
|
179
|
-
if (!mpInstance._Store.isLocalStorageAvailable) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
var localStorageProducts = {};
|
|
184
|
-
localStorageProducts[mpid] = {};
|
|
185
|
-
if (cookies.cp) {
|
|
186
|
-
try {
|
|
187
|
-
localStorageProducts[mpid].cp = JSON.parse(
|
|
188
|
-
Base64.decode(cookies.cp)
|
|
189
|
-
);
|
|
190
|
-
} catch (e) {
|
|
191
|
-
localStorageProducts[mpid].cp = cookies.cp;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
if (!Array.isArray(localStorageProducts[mpid].cp)) {
|
|
195
|
-
localStorageProducts[mpid].cp = [];
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
localStorage.setItem(
|
|
200
|
-
mpInstance._Store.prodStorageName,
|
|
201
|
-
Base64.encode(JSON.stringify(localStorageProducts))
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function migrateLocalStorage() {
|
|
206
|
-
var cookies,
|
|
207
|
-
v3LSName = StorageNames.localStorageNameV3,
|
|
208
|
-
v4LSName = StorageNames.localStorageNameV4,
|
|
209
|
-
currentVersionLSData = window.localStorage.getItem(
|
|
210
|
-
mpInstance._Store.storageName
|
|
211
|
-
),
|
|
212
|
-
v4LSData,
|
|
213
|
-
v3LSData,
|
|
214
|
-
v3LSDataStringCopy;
|
|
215
|
-
|
|
216
|
-
if (currentVersionLSData) {
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
v4LSData = window.localStorage.getItem(v4LSName);
|
|
221
|
-
if (v4LSData) {
|
|
222
|
-
finishLSMigration(v4LSData, v4LSName);
|
|
223
|
-
migrateProductsToNameSpace();
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
v3LSData = window.localStorage.getItem(v3LSName);
|
|
228
|
-
if (v3LSData) {
|
|
229
|
-
mpInstance._Store.migratingToIDSyncCookies = true;
|
|
230
|
-
v3LSDataStringCopy = v3LSData.slice();
|
|
231
|
-
v3LSData = JSON.parse(
|
|
232
|
-
mpInstance._Persistence.replacePipesWithCommas(
|
|
233
|
-
mpInstance._Persistence.replaceApostrophesWithQuotes(
|
|
234
|
-
v3LSData
|
|
235
|
-
)
|
|
236
|
-
)
|
|
237
|
-
);
|
|
238
|
-
// localStorage may contain only products, or the full persistence
|
|
239
|
-
// when there is an MPID on the cookie, it is the full persistence
|
|
240
|
-
if (v3LSData.mpid) {
|
|
241
|
-
v3LSData = JSON.parse(
|
|
242
|
-
self.convertSDKv1CookiesV3ToSDKv2CookiesV4(
|
|
243
|
-
v3LSDataStringCopy
|
|
244
|
-
)
|
|
245
|
-
);
|
|
246
|
-
finishLSMigration(JSON.stringify(v3LSData), v3LSName);
|
|
247
|
-
return;
|
|
248
|
-
// if no MPID, it is only the products
|
|
249
|
-
} else if ((v3LSData.cp || v3LSData.pb) && !v3LSData.mpid) {
|
|
250
|
-
cookies = mpInstance._Persistence.getCookie();
|
|
251
|
-
if (cookies) {
|
|
252
|
-
migrateProductsFromSDKv1ToSDKv2CookiesV4(
|
|
253
|
-
v3LSData,
|
|
254
|
-
cookies.cu
|
|
255
|
-
);
|
|
256
|
-
localStorage.removeItem(StorageNames.localStorageNameV3);
|
|
257
|
-
return;
|
|
258
|
-
} else {
|
|
259
|
-
localStorage.removeItem(StorageNames.localStorageNameV3);
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function finishLSMigration(data, lsName) {
|
|
266
|
-
try {
|
|
267
|
-
window.localStorage.setItem(
|
|
268
|
-
encodeURIComponent(mpInstance._Store.storageName),
|
|
269
|
-
data
|
|
270
|
-
);
|
|
271
|
-
} catch (e) {
|
|
272
|
-
mpInstance.Logger.error(
|
|
273
|
-
'Error with setting localStorage item.'
|
|
274
|
-
);
|
|
275
|
-
}
|
|
276
|
-
window.localStorage.removeItem(encodeURIComponent(lsName));
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
}
|