@magicpixel/rn-mp-client-sdk 0.6.2 → 0.7.0

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.
Files changed (39) hide show
  1. package/lib/commonjs/common/app-types.js.map +1 -1
  2. package/lib/commonjs/common/data-store.js +10 -0
  3. package/lib/commonjs/common/data-store.js.map +1 -1
  4. package/lib/commonjs/common/network-service.js +11 -5
  5. package/lib/commonjs/common/network-service.js.map +1 -1
  6. package/lib/commonjs/common/utils.js +17 -46
  7. package/lib/commonjs/common/utils.js.map +1 -1
  8. package/lib/commonjs/index.js +29 -2
  9. package/lib/commonjs/index.js.map +1 -1
  10. package/lib/commonjs/processors/tag.processor.js +1 -1
  11. package/lib/commonjs/processors/tag.processor.js.map +1 -1
  12. package/lib/commonjs/processors/visit-id.processor.js +56 -87
  13. package/lib/commonjs/processors/visit-id.processor.js.map +1 -1
  14. package/lib/module/common/app-types.js.map +1 -1
  15. package/lib/module/common/data-store.js +10 -0
  16. package/lib/module/common/data-store.js.map +1 -1
  17. package/lib/module/common/network-service.js +11 -5
  18. package/lib/module/common/network-service.js.map +1 -1
  19. package/lib/module/common/utils.js +18 -47
  20. package/lib/module/common/utils.js.map +1 -1
  21. package/lib/module/index.js +28 -2
  22. package/lib/module/index.js.map +1 -1
  23. package/lib/module/processors/tag.processor.js +1 -1
  24. package/lib/module/processors/tag.processor.js.map +1 -1
  25. package/lib/module/processors/visit-id.processor.js +49 -82
  26. package/lib/module/processors/visit-id.processor.js.map +1 -1
  27. package/lib/typescript/common/app-types.d.ts +1 -0
  28. package/lib/typescript/common/data-store.d.ts +3 -0
  29. package/lib/typescript/common/utils.d.ts +3 -4
  30. package/lib/typescript/index.d.ts +6 -3
  31. package/lib/typescript/processors/visit-id.processor.d.ts +4 -4
  32. package/package.json +27 -25
  33. package/src/common/app-types.ts +2 -0
  34. package/src/common/data-store.ts +10 -0
  35. package/src/common/network-service.ts +8 -2
  36. package/src/common/utils.ts +15 -62
  37. package/src/index.tsx +36 -5
  38. package/src/processors/tag.processor.ts +1 -1
  39. package/src/processors/visit-id.processor.ts +54 -96
package/src/index.tsx CHANGED
@@ -16,6 +16,8 @@ import { NetworkService } from './common/network-service';
16
16
  import { EventBus } from './common/event-bus';
17
17
  import { VisitIdProcessor } from './processors/visit-id.processor';
18
18
 
19
+ const DL_INIT_EVENT = 'page_load';
20
+
19
21
  class MagicPixelImpl {
20
22
  private static dl: MpDataLayerHelper;
21
23
  private static customerInfo: AppCustomerInfo;
@@ -23,7 +25,7 @@ class MagicPixelImpl {
23
25
  private static firstAppLaunch = true;
24
26
 
25
27
  static async init(options: SdkInitOptions): Promise<void> {
26
- this.dl = new MpDataLayerHelper('mpDlEvent', 'manual', 'page_load');
28
+ this.dl = new MpDataLayerHelper('mpDlEvent', 'manual', DL_INIT_EVENT);
27
29
 
28
30
  await NetworkService.refreshClientSdkJson(options);
29
31
 
@@ -39,7 +41,9 @@ class MagicPixelImpl {
39
41
  DataStore.overrideDeviceType(options.device_type);
40
42
  }
41
43
 
42
- await VisitIdProcessor.init();
44
+ this.setAppVersion(options.app_version);
45
+
46
+ await VisitIdProcessor.init(options?.orgId);
43
47
 
44
48
  const fbp = await VisitIdProcessor.getFacebookFBP();
45
49
  if (fbp) {
@@ -92,7 +96,11 @@ class MagicPixelImpl {
92
96
  this.dl.ready();
93
97
  }
94
98
 
95
- static async checkAndFireTM(): Promise<void> {
99
+ static getDebugId(): string {
100
+ return DataStore.getDebugId();
101
+ }
102
+
103
+ private static async checkAndFireTM(): Promise<void> {
96
104
  try {
97
105
  if (!DataStore.isTagManagerProcessing()) {
98
106
  const item = DataStore.deQueueTMFire();
@@ -126,7 +134,7 @@ class MagicPixelImpl {
126
134
  }
127
135
  }
128
136
 
129
- static async runTM(
137
+ private static async runTM(
130
138
  sseOnly: boolean,
131
139
  triggerName: string,
132
140
  evtName: string,
@@ -228,7 +236,23 @@ class MagicPixelImpl {
228
236
  }
229
237
  }
230
238
 
231
- static _fireTM(
239
+ private static _fireTM(
240
+ envName: string,
241
+ envId: string,
242
+ evtName: string,
243
+ evtId: string
244
+ ): void {
245
+ // increment visit_depth if event name is page_load because that's the only way we can track page views in an app for now
246
+ if (evtName === DL_INIT_EVENT) {
247
+ VisitIdProcessor.incrementVisitDepth().finally(() => {
248
+ this._fireTMPrivate(envName, envId, evtName, evtId);
249
+ });
250
+ } else {
251
+ this._fireTMPrivate(envName, envId, evtName, evtId);
252
+ }
253
+ }
254
+
255
+ private static _fireTMPrivate(
232
256
  envName: string,
233
257
  envId: string,
234
258
  evtName: string,
@@ -259,6 +283,7 @@ class MagicPixelImpl {
259
283
 
260
284
  static setCustomerInfo(customerInfo: AppCustomerInfo): void {
261
285
  this.customerInfo = customerInfo;
286
+ this.recordEvent('idl_attribution_link', customerInfo);
262
287
  }
263
288
 
264
289
  static setFirebaseAppInstanceId(instanceId: string): void {
@@ -269,6 +294,12 @@ class MagicPixelImpl {
269
294
  this.customerIdentifiers.fbp = fbp;
270
295
  }
271
296
 
297
+ private static setAppVersion(appVersion?: string): void {
298
+ if (appVersion && appVersion?.trim()?.length > 0) {
299
+ this.customerIdentifiers.app_version = appVersion;
300
+ }
301
+ }
302
+
272
303
  static recordPageLoad(pageLoadInfo: AppPageLoad): void {
273
304
  pageLoadInfo.is_entry = this.firstAppLaunch ? 1 : 0;
274
305
  if (this.customerInfo) {
@@ -373,7 +373,7 @@ export class TagProcessor {
373
373
  }
374
374
  }
375
375
 
376
- const op = Utils.unflattenObject(paramValueArray);
376
+ const op = Utils.unFlattenObject(paramValueArray);
377
377
 
378
378
  return {
379
379
  isInError: false,
@@ -5,74 +5,12 @@ import { Reporter } from '../common/reporter';
5
5
  import { NetworkService } from '../common/network-service';
6
6
  import type { VisitInfo, VisitorInfo } from '../common/app-types';
7
7
  import { Utils } from '../common/utils';
8
- import { EventBus } from '../common/event-bus';
9
8
 
10
9
  export class VisitIdProcessor {
11
- static async init(): Promise<void> {
12
- await this.initIdlEventListener();
13
- await this.initDebugId();
10
+ static async init(orgId: string): Promise<void> {
11
+ await this.initDebugId(orgId);
14
12
  await this.setOrResetVisitorId();
15
- await this.setOrResetVisitInfo();
16
- }
17
-
18
- static async initIdlEventListener(): Promise<void> {
19
- EventBus.on(
20
- 'mp_idl_app_event',
21
- (channelName: string, eventPayload: Record<string, string>) => {
22
- try {
23
- if (!channelName || !eventPayload) {
24
- Logger.logError(
25
- 'Both event name and event payload is mandatory for processing idl events'
26
- );
27
- return;
28
- }
29
-
30
- const eventName = eventPayload.client_ev_name;
31
- const attrInfoPayload: any = eventPayload.client_ev_payload ?? {};
32
- const deepPayload: any = attrInfoPayload?.payload ?? {};
33
-
34
- // is_entry, app_type,
35
- try {
36
- deepPayload['org_id'] = eventPayload?.org_id;
37
- deepPayload['event_id'] = eventPayload?.event_id;
38
- deepPayload['event_ts'] = eventPayload?.event_ts;
39
- } catch (e) {
40
- Logger.logError('Error enhancing internal attr payload');
41
- }
42
-
43
- const attrPayload: any = {
44
- pu: attrInfoPayload?.page_url,
45
- ru: attrInfoPayload?.campaign_ref,
46
- pt: attrInfoPayload?.page_title,
47
- en: attrInfoPayload.event_name,
48
- cid: attrInfoPayload?.campaign_id,
49
- rs: attrInfoPayload?.report_suite,
50
- v: attrInfoPayload?.client_sdk_version,
51
- vidl: attrInfoPayload?.idl_version,
52
- ev: attrInfoPayload?.event_val,
53
- ep: deepPayload,
54
- at: attrInfoPayload?.app_type,
55
- evId: attrInfoPayload?.event_id,
56
- evTs: attrInfoPayload?.event_ts,
57
- orgId: attrInfoPayload?.org_id,
58
- _mp_vid: DataStore.visitInfoToString(),
59
- _lmid: DataStore.getMpId(),
60
- _ldid: DataStore.getDeviceId(),
61
- _mpidl: JSON.stringify(DataStore.getVisitorInfoAsString()),
62
- is_entry: attrInfoPayload.is_entry ?? 0,
63
- app_type: attrInfoPayload.app_type,
64
- };
65
-
66
- if (eventName === 'al') {
67
- attrPayload.pid = eventPayload?.profile_id;
68
- }
69
-
70
- Reporter.postAttrInfo(attrPayload);
71
- } catch (err) {
72
- Logger.logError('Error processing idl event: ', err);
73
- }
74
- }
75
- );
13
+ await this.setOrResetVisitInfo(false);
76
14
  }
77
15
 
78
16
  static async getFacebookFBP(): Promise<string> {
@@ -84,6 +22,9 @@ export class VisitIdProcessor {
84
22
  }
85
23
 
86
24
  await DataStore.storeData(Constants.KEY_FBP, fbp);
25
+
26
+ DataStore.addCommonCookie('_fbp', fbp);
27
+
87
28
  return fbp;
88
29
  } catch (err) {
89
30
  Logger.logError('Error initializing debug id.', err);
@@ -91,14 +32,21 @@ export class VisitIdProcessor {
91
32
  return '';
92
33
  }
93
34
 
94
- static async initDebugId(): Promise<void> {
35
+ static async initDebugId(orgId: string): Promise<void> {
95
36
  try {
96
37
  let debugId = await DataStore.getDataFromStorage<string>(
97
38
  Constants.KEY_MP_DEBUG_ID
98
39
  );
40
+
41
+ if (debugId && debugId.indexOf(orgId) !== 0) {
42
+ // re-generate debug id if it is not of the simple variety
43
+ debugId = undefined;
44
+ }
45
+
99
46
  if (!debugId) {
100
- debugId = Utils.getUniqueID();
47
+ debugId = Utils.getSimpleDebugId(orgId);
101
48
  }
49
+
102
50
  await DataStore.storeData(Constants.KEY_MP_DEBUG_ID, debugId);
103
51
  DataStore.setDebugId(debugId);
104
52
  } catch (err) {
@@ -106,39 +54,37 @@ export class VisitIdProcessor {
106
54
  }
107
55
  }
108
56
 
109
- static async setOrResetVisitInfo(): Promise<void> {
57
+ static async setOrResetVisitInfo(incrementDepth: boolean): Promise<void> {
110
58
  try {
111
- const visitInfo: VisitInfo = await DataStore.getDataFromStorage(
59
+ let visitInfo: VisitInfo = await DataStore.getDataFromStorage(
112
60
  Constants.KEY_MP_IDL_VISIT_ID_INFO
113
61
  );
114
62
 
115
63
  if (!visitInfo) {
116
- // may be first time visit, setup a new visit info object and return
117
- const newVisitInfo: VisitInfo = {
64
+ // may be first time visit, set up a new visit info object and return
65
+ visitInfo = {
118
66
  visitId: Utils.getUniqueID(),
119
67
  visitCt: 1,
120
68
  visitDepth: 1,
121
69
  visitTs: Date.now(),
122
70
  visitVer: 1,
123
71
  };
124
- DataStore.setVisitInfo(newVisitInfo);
125
- await DataStore.storeData(
126
- Constants.KEY_MP_IDL_VISIT_ID_INFO,
127
- newVisitInfo
128
- );
129
72
  } else {
130
73
  // reset visit id anyway, but retain the visitCt and depth setting
131
74
  visitInfo.visitId = Utils.getUniqueID();
132
- visitInfo.visitCt = visitInfo.visitCt + 1;
133
- visitInfo.visitDepth = 1;
75
+ if (incrementDepth) {
76
+ visitInfo.visitDepth = (visitInfo?.visitDepth ?? 0) + 1;
77
+ } else {
78
+ visitInfo.visitCt = visitInfo.visitCt + 1;
79
+ visitInfo.visitDepth = 1;
80
+ }
134
81
  visitInfo.visitTs = Date.now();
135
82
  visitInfo.visitVer = 1;
136
- DataStore.setVisitInfo(visitInfo);
137
- await DataStore.storeData(
138
- Constants.KEY_MP_IDL_VISIT_ID_INFO,
139
- visitInfo
140
- );
141
83
  }
84
+
85
+ DataStore.setVisitInfo(visitInfo);
86
+ await DataStore.storeData(Constants.KEY_MP_IDL_VISIT_ID_INFO, visitInfo);
87
+ DataStore.addCommonCookie('_mp_vid', DataStore.visitInfoToString());
142
88
  } catch (err) {
143
89
  Logger.logError(err);
144
90
  DataStore.setVisitInfo(null);
@@ -146,30 +92,42 @@ export class VisitIdProcessor {
146
92
  }
147
93
  }
148
94
 
95
+ static async incrementVisitDepth(): Promise<void> {
96
+ return this.setOrResetVisitInfo(true);
97
+ }
98
+
149
99
  static async setOrResetVisitorId(): Promise<void> {
150
100
  try {
151
- const visitorInfoData: VisitorInfo = await DataStore.getDataFromStorage(
101
+ let visitorInfoData: VisitorInfo = await DataStore.getDataFromStorage(
152
102
  Constants.KEY_MP_IDL_VISITOR_ID_INFO
153
103
  );
154
104
 
155
- if (!visitorInfoData) {
105
+ if (
106
+ !visitorInfoData ||
107
+ (visitorInfoData && visitorInfoData?.mId.indexOf('-') === -1)
108
+ ) {
156
109
  Logger.logDbg('VisitorInfo not found. Refreshing from server');
157
110
  // get a new visit id and device id from server and store it in storage
158
- const apiVisitInfo = await NetworkService.fetchIdlInfo(
111
+ visitorInfoData = await NetworkService.fetchIdlInfo(
159
112
  DataStore.getIdlUrl()
160
113
  );
161
- if (!apiVisitInfo) {
162
- return;
163
- } else {
164
- DataStore.setVisitorInfo(apiVisitInfo);
165
- // store to db as well
166
- await DataStore.storeData(
167
- Constants.KEY_MP_IDL_VISITOR_ID_INFO,
168
- apiVisitInfo
169
- );
170
- }
114
+ }
115
+
116
+ if (!visitorInfoData) {
117
+ return;
171
118
  } else {
172
119
  DataStore.setVisitorInfo(visitorInfoData);
120
+ // store to db as well
121
+ await DataStore.storeData(
122
+ Constants.KEY_MP_IDL_VISITOR_ID_INFO,
123
+ visitorInfoData
124
+ );
125
+
126
+ const cookieValue = encodeURIComponent(JSON.stringify(visitorInfoData));
127
+ DataStore.addCommonCookie('_mplidl', cookieValue);
128
+ DataStore.addCommonCookie('_mpidl', cookieValue);
129
+ DataStore.addCommonCookie('_lmid', visitorInfoData.mId);
130
+ DataStore.addCommonCookie('_ldid', visitorInfoData.dId);
173
131
  }
174
132
  } catch (err) {
175
133
  Logger.logError(err);