@magicpixel/rn-mp-client-sdk 0.2.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.
- package/LICENSE +20 -0
- package/README.md +29 -0
- package/lib/commonjs/common/app-types.js +6 -0
- package/lib/commonjs/common/app-types.js.map +1 -0
- package/lib/commonjs/common/constants.js +51 -0
- package/lib/commonjs/common/constants.js.map +1 -0
- package/lib/commonjs/common/data-store.js +361 -0
- package/lib/commonjs/common/data-store.js.map +1 -0
- package/lib/commonjs/common/event-bus.js +42 -0
- package/lib/commonjs/common/event-bus.js.map +1 -0
- package/lib/commonjs/common/logger.js +30 -0
- package/lib/commonjs/common/logger.js.map +1 -0
- package/lib/commonjs/common/network-service.js +90 -0
- package/lib/commonjs/common/network-service.js.map +1 -0
- package/lib/commonjs/common/reporter.js +107 -0
- package/lib/commonjs/common/reporter.js.map +1 -0
- package/lib/commonjs/common/utils.js +276 -0
- package/lib/commonjs/common/utils.js.map +1 -0
- package/lib/commonjs/coverage/clover.xml +6 -0
- package/lib/commonjs/coverage/coverage-final.json +1 -0
- package/lib/commonjs/coverage/lcov-report/base.css +224 -0
- package/lib/commonjs/coverage/lcov-report/block-navigation.js +83 -0
- package/lib/commonjs/coverage/lcov-report/block-navigation.js.map +1 -0
- package/lib/commonjs/coverage/lcov-report/favicon.png +0 -0
- package/lib/commonjs/coverage/lcov-report/index.html +101 -0
- package/lib/commonjs/coverage/lcov-report/prettify.css +1 -0
- package/lib/commonjs/coverage/lcov-report/prettify.js +995 -0
- package/lib/commonjs/coverage/lcov-report/prettify.js.map +1 -0
- package/lib/commonjs/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/lib/commonjs/coverage/lcov-report/sorter.js +212 -0
- package/lib/commonjs/coverage/lcov-report/sorter.js.map +1 -0
- package/lib/commonjs/coverage/lcov.info +0 -0
- package/lib/commonjs/eedl/eedl.js +262 -0
- package/lib/commonjs/eedl/eedl.js.map +1 -0
- package/lib/commonjs/index.js +214 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/models/mp-client-sdk.js +33 -0
- package/lib/commonjs/models/mp-client-sdk.js.map +1 -0
- package/lib/commonjs/processors/data-element.processor.js +191 -0
- package/lib/commonjs/processors/data-element.processor.js.map +1 -0
- package/lib/commonjs/processors/qc.processor.js +111 -0
- package/lib/commonjs/processors/qc.processor.js.map +1 -0
- package/lib/commonjs/processors/tag.processor.js +432 -0
- package/lib/commonjs/processors/tag.processor.js.map +1 -0
- package/lib/commonjs/processors/trans-function.processor.js +91 -0
- package/lib/commonjs/processors/trans-function.processor.js.map +1 -0
- package/lib/commonjs/processors/visit-id.processor.js +172 -0
- package/lib/commonjs/processors/visit-id.processor.js.map +1 -0
- package/lib/module/common/app-types.js +2 -0
- package/lib/module/common/app-types.js.map +1 -0
- package/lib/module/common/constants.js +41 -0
- package/lib/module/common/constants.js.map +1 -0
- package/lib/module/common/data-store.js +346 -0
- package/lib/module/common/data-store.js.map +1 -0
- package/lib/module/common/event-bus.js +31 -0
- package/lib/module/common/event-bus.js.map +1 -0
- package/lib/module/common/logger.js +21 -0
- package/lib/module/common/logger.js.map +1 -0
- package/lib/module/common/network-service.js +73 -0
- package/lib/module/common/network-service.js.map +1 -0
- package/lib/module/common/reporter.js +88 -0
- package/lib/module/common/reporter.js.map +1 -0
- package/lib/module/common/utils.js +263 -0
- package/lib/module/common/utils.js.map +1 -0
- package/lib/module/coverage/clover.xml +6 -0
- package/lib/module/coverage/coverage-final.json +1 -0
- package/lib/module/coverage/lcov-report/base.css +224 -0
- package/lib/module/coverage/lcov-report/block-navigation.js +81 -0
- package/lib/module/coverage/lcov-report/block-navigation.js.map +1 -0
- package/lib/module/coverage/lcov-report/favicon.png +0 -0
- package/lib/module/coverage/lcov-report/index.html +101 -0
- package/lib/module/coverage/lcov-report/prettify.css +1 -0
- package/lib/module/coverage/lcov-report/prettify.js +993 -0
- package/lib/module/coverage/lcov-report/prettify.js.map +1 -0
- package/lib/module/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/lib/module/coverage/lcov-report/sorter.js +210 -0
- package/lib/module/coverage/lcov-report/sorter.js.map +1 -0
- package/lib/module/coverage/lcov.info +0 -0
- package/lib/module/eedl/eedl.js +246 -0
- package/lib/module/eedl/eedl.js.map +1 -0
- package/lib/module/index.js +176 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/models/mp-client-sdk.js +24 -0
- package/lib/module/models/mp-client-sdk.js.map +1 -0
- package/lib/module/processors/data-element.processor.js +176 -0
- package/lib/module/processors/data-element.processor.js.map +1 -0
- package/lib/module/processors/qc.processor.js +90 -0
- package/lib/module/processors/qc.processor.js.map +1 -0
- package/lib/module/processors/tag.processor.js +396 -0
- package/lib/module/processors/tag.processor.js.map +1 -0
- package/lib/module/processors/trans-function.processor.js +73 -0
- package/lib/module/processors/trans-function.processor.js.map +1 -0
- package/lib/module/processors/visit-id.processor.js +144 -0
- package/lib/module/processors/visit-id.processor.js.map +1 -0
- package/lib/typescript/common/app-types.d.ts +101 -0
- package/lib/typescript/common/constants.d.ts +21 -0
- package/lib/typescript/common/data-store.d.ts +81 -0
- package/lib/typescript/common/event-bus.d.ts +6 -0
- package/lib/typescript/common/logger.d.ts +5 -0
- package/lib/typescript/common/network-service.d.ts +8 -0
- package/lib/typescript/common/reporter.d.ts +12 -0
- package/lib/typescript/common/utils.d.ts +38 -0
- package/lib/typescript/eedl/eedl.d.ts +46 -0
- package/lib/typescript/index.d.ts +18 -0
- package/lib/typescript/models/mp-client-sdk.d.ts +157 -0
- package/lib/typescript/processors/data-element.processor.d.ts +12 -0
- package/lib/typescript/processors/qc.processor.d.ts +4 -0
- package/lib/typescript/processors/tag.processor.d.ts +27 -0
- package/lib/typescript/processors/trans-function.processor.d.ts +12 -0
- package/lib/typescript/processors/visit-id.processor.d.ts +7 -0
- package/package.json +170 -0
- package/src/common/app-types.ts +128 -0
- package/src/common/constants.ts +43 -0
- package/src/common/data-store.ts +333 -0
- package/src/common/event-bus.ts +35 -0
- package/src/common/logger.ts +19 -0
- package/src/common/network-service.ts +85 -0
- package/src/common/reporter.ts +110 -0
- package/src/common/utils.ts +281 -0
- package/src/coverage/clover.xml +6 -0
- package/src/coverage/coverage-final.json +1 -0
- package/src/coverage/lcov-report/base.css +224 -0
- package/src/coverage/lcov-report/block-navigation.js +87 -0
- package/src/coverage/lcov-report/favicon.png +0 -0
- package/src/coverage/lcov-report/index.html +101 -0
- package/src/coverage/lcov-report/prettify.css +1 -0
- package/src/coverage/lcov-report/prettify.js +2 -0
- package/src/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/src/coverage/lcov-report/sorter.js +196 -0
- package/src/coverage/lcov.info +0 -0
- package/src/eedl/eedl.ts +233 -0
- package/src/index.tsx +258 -0
- package/src/models/mp-client-sdk.ts +174 -0
- package/src/processors/data-element.processor.ts +249 -0
- package/src/processors/qc.processor.ts +115 -0
- package/src/processors/tag.processor.ts +527 -0
- package/src/processors/trans-function.processor.ts +85 -0
- package/src/processors/visit-id.processor.ts +164 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process all the data elements at once, or if a set of keys are specified, processes only
|
|
3
|
+
* those keys and updates the dataElements object in global-fns file
|
|
4
|
+
* @param dataElements
|
|
5
|
+
* @param sdkTransFunctions
|
|
6
|
+
* @param optTfnKeys
|
|
7
|
+
*/
|
|
8
|
+
import type { MapLike } from '../common/app-types';
|
|
9
|
+
import type { ClientSdkTfnItem } from '../models/mp-client-sdk';
|
|
10
|
+
import { DataStore } from '../common/data-store';
|
|
11
|
+
import { Logger } from '../common/logger';
|
|
12
|
+
import { Utils } from '../common/utils';
|
|
13
|
+
import { Reporter } from '../common/reporter';
|
|
14
|
+
|
|
15
|
+
export class TransFunctionProcessor {
|
|
16
|
+
static processTransformationFunctions(
|
|
17
|
+
dataElements: MapLike<any>,
|
|
18
|
+
sdkTransFunctions: MapLike<ClientSdkTfnItem>,
|
|
19
|
+
optTfnKeys?: string[]
|
|
20
|
+
): MapLike<any> {
|
|
21
|
+
const tfn = DataStore.getTransFunctions();
|
|
22
|
+
const tfnKeys =
|
|
23
|
+
optTfnKeys && optTfnKeys.length > 0
|
|
24
|
+
? optTfnKeys
|
|
25
|
+
: Object.keys(sdkTransFunctions);
|
|
26
|
+
try {
|
|
27
|
+
for (const key of tfnKeys) {
|
|
28
|
+
try {
|
|
29
|
+
const tfnItem = sdkTransFunctions[key];
|
|
30
|
+
/**
|
|
31
|
+
* How to process transformation functions
|
|
32
|
+
* 1) Find all the data elements in the resource params of trans function
|
|
33
|
+
* 2) make them into an object (key value) where key is the resourceKey and value is the value of data elements
|
|
34
|
+
* 3) Invoke the tfn and get the value and store it against the tfn key
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
if (tfnItem) {
|
|
38
|
+
if (tfnItem.rParams) {
|
|
39
|
+
const tfnFnInput: any = {};
|
|
40
|
+
for (const paramKey of Object.keys(tfnItem.rParams)) {
|
|
41
|
+
// tfn can have only data elements as of now.
|
|
42
|
+
const rParam = tfnItem.rParams[paramKey];
|
|
43
|
+
const deId = rParam?.de;
|
|
44
|
+
if (deId) {
|
|
45
|
+
let paramValue = dataElements[deId] || '';
|
|
46
|
+
if (rParam.rqd && !paramValue) {
|
|
47
|
+
Logger.logDbg(
|
|
48
|
+
`Value required for: ${paramKey}, in transformation function. But not found`
|
|
49
|
+
);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
if (rParam.tf) {
|
|
53
|
+
paramValue = Utils.applyTransformationResourceParam(
|
|
54
|
+
paramValue,
|
|
55
|
+
rParam.tf
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
tfnFnInput[paramKey] = paramValue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// invoke the function and get the value
|
|
62
|
+
tfn[key] = tfnItem.fn(tfnFnInput);
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
tfn[key] = '';
|
|
66
|
+
}
|
|
67
|
+
} catch (err) {
|
|
68
|
+
Logger.logError(
|
|
69
|
+
'Error processing transformation function: ',
|
|
70
|
+
key,
|
|
71
|
+
' with error: ',
|
|
72
|
+
err
|
|
73
|
+
);
|
|
74
|
+
// TODO: Report as metric to client
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} catch (err) {
|
|
78
|
+
Logger.logError('Error processing transformation functions: ', err);
|
|
79
|
+
Reporter.reportError('m::processTransformationFunctions', err);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
DataStore.setTransFunctions(tfn);
|
|
83
|
+
return tfn;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Logger } from '../common/logger';
|
|
2
|
+
import { DataStore } from '../common/data-store';
|
|
3
|
+
import { Constants } from '../common/constants';
|
|
4
|
+
import { Reporter } from '../common/reporter';
|
|
5
|
+
import { NetworkService } from '../common/network-service';
|
|
6
|
+
import type { VisitInfo, VisitorInfo } from '../common/app-types';
|
|
7
|
+
import { Utils } from '../common/utils';
|
|
8
|
+
import { EventBus } from '../common/event-bus';
|
|
9
|
+
|
|
10
|
+
export class VisitIdProcessor {
|
|
11
|
+
static async init(): Promise<void> {
|
|
12
|
+
await this.initIdlEventListener();
|
|
13
|
+
await this.initDebugId();
|
|
14
|
+
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
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static async initDebugId(): Promise<void> {
|
|
79
|
+
try {
|
|
80
|
+
let debugId = await DataStore.getDataFromStorage<string>(
|
|
81
|
+
Constants.KEY_MP_DEBUG_ID
|
|
82
|
+
);
|
|
83
|
+
if (!debugId) {
|
|
84
|
+
debugId = Utils.getUniqueID();
|
|
85
|
+
}
|
|
86
|
+
await DataStore.storeData(Constants.KEY_MP_DEBUG_ID, debugId);
|
|
87
|
+
DataStore.setDebugId(debugId);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
Logger.logError('Error initializing debug id.', err);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static async setOrResetVisitInfo(): Promise<void> {
|
|
94
|
+
try {
|
|
95
|
+
const visitInfo: VisitInfo = await DataStore.getDataFromStorage(
|
|
96
|
+
Constants.KEY_MP_IDL_VISIT_ID_INFO
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
if (!visitInfo) {
|
|
100
|
+
// may be first time visit, setup a new visit info object and return
|
|
101
|
+
const newVisitInfo: VisitInfo = {
|
|
102
|
+
visitId: Utils.getUniqueID(),
|
|
103
|
+
visitCt: 1,
|
|
104
|
+
visitDepth: 1,
|
|
105
|
+
visitTs: Date.now(),
|
|
106
|
+
visitVer: 1,
|
|
107
|
+
};
|
|
108
|
+
DataStore.setVisitInfo(newVisitInfo);
|
|
109
|
+
await DataStore.storeData(
|
|
110
|
+
Constants.KEY_MP_IDL_VISIT_ID_INFO,
|
|
111
|
+
newVisitInfo
|
|
112
|
+
);
|
|
113
|
+
} else {
|
|
114
|
+
// reset visit id anyway, but retain the visitCt and depth setting
|
|
115
|
+
visitInfo.visitId = Utils.getUniqueID();
|
|
116
|
+
visitInfo.visitCt = visitInfo.visitCt + 1;
|
|
117
|
+
visitInfo.visitDepth = 1;
|
|
118
|
+
visitInfo.visitTs = Date.now();
|
|
119
|
+
visitInfo.visitVer = 1;
|
|
120
|
+
DataStore.setVisitInfo(visitInfo);
|
|
121
|
+
await DataStore.storeData(
|
|
122
|
+
Constants.KEY_MP_IDL_VISIT_ID_INFO,
|
|
123
|
+
visitInfo
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
} catch (err) {
|
|
127
|
+
Logger.logError(err);
|
|
128
|
+
DataStore.setVisitInfo(null);
|
|
129
|
+
Reporter.reportError('setOrResetVisitInfo', err);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
static async setOrResetVisitorId(): Promise<void> {
|
|
134
|
+
try {
|
|
135
|
+
const visitorInfoData: VisitorInfo = await DataStore.getDataFromStorage(
|
|
136
|
+
Constants.KEY_MP_IDL_VISITOR_ID_INFO
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
if (!visitorInfoData) {
|
|
140
|
+
Logger.logDbg('VisitorInfo not found. Refreshing from server');
|
|
141
|
+
// get a new visit id and device id from server and store it in storage
|
|
142
|
+
const apiVisitInfo = await NetworkService.fetchIdlInfo(
|
|
143
|
+
DataStore.getIdlUrl()
|
|
144
|
+
);
|
|
145
|
+
if (!apiVisitInfo) {
|
|
146
|
+
return;
|
|
147
|
+
} else {
|
|
148
|
+
DataStore.setVisitorInfo(apiVisitInfo);
|
|
149
|
+
// store to db as well
|
|
150
|
+
await DataStore.storeData(
|
|
151
|
+
Constants.KEY_MP_IDL_VISITOR_ID_INFO,
|
|
152
|
+
apiVisitInfo
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
DataStore.setVisitorInfo(visitorInfoData);
|
|
157
|
+
}
|
|
158
|
+
} catch (err) {
|
|
159
|
+
Logger.logError(err);
|
|
160
|
+
DataStore.setVisitorInfo(null);
|
|
161
|
+
Reporter.reportError('setOrResetVisitorId', err);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|