@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.
Files changed (138) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +29 -0
  3. package/lib/commonjs/common/app-types.js +6 -0
  4. package/lib/commonjs/common/app-types.js.map +1 -0
  5. package/lib/commonjs/common/constants.js +51 -0
  6. package/lib/commonjs/common/constants.js.map +1 -0
  7. package/lib/commonjs/common/data-store.js +361 -0
  8. package/lib/commonjs/common/data-store.js.map +1 -0
  9. package/lib/commonjs/common/event-bus.js +42 -0
  10. package/lib/commonjs/common/event-bus.js.map +1 -0
  11. package/lib/commonjs/common/logger.js +30 -0
  12. package/lib/commonjs/common/logger.js.map +1 -0
  13. package/lib/commonjs/common/network-service.js +90 -0
  14. package/lib/commonjs/common/network-service.js.map +1 -0
  15. package/lib/commonjs/common/reporter.js +107 -0
  16. package/lib/commonjs/common/reporter.js.map +1 -0
  17. package/lib/commonjs/common/utils.js +276 -0
  18. package/lib/commonjs/common/utils.js.map +1 -0
  19. package/lib/commonjs/coverage/clover.xml +6 -0
  20. package/lib/commonjs/coverage/coverage-final.json +1 -0
  21. package/lib/commonjs/coverage/lcov-report/base.css +224 -0
  22. package/lib/commonjs/coverage/lcov-report/block-navigation.js +83 -0
  23. package/lib/commonjs/coverage/lcov-report/block-navigation.js.map +1 -0
  24. package/lib/commonjs/coverage/lcov-report/favicon.png +0 -0
  25. package/lib/commonjs/coverage/lcov-report/index.html +101 -0
  26. package/lib/commonjs/coverage/lcov-report/prettify.css +1 -0
  27. package/lib/commonjs/coverage/lcov-report/prettify.js +995 -0
  28. package/lib/commonjs/coverage/lcov-report/prettify.js.map +1 -0
  29. package/lib/commonjs/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  30. package/lib/commonjs/coverage/lcov-report/sorter.js +212 -0
  31. package/lib/commonjs/coverage/lcov-report/sorter.js.map +1 -0
  32. package/lib/commonjs/coverage/lcov.info +0 -0
  33. package/lib/commonjs/eedl/eedl.js +262 -0
  34. package/lib/commonjs/eedl/eedl.js.map +1 -0
  35. package/lib/commonjs/index.js +214 -0
  36. package/lib/commonjs/index.js.map +1 -0
  37. package/lib/commonjs/models/mp-client-sdk.js +33 -0
  38. package/lib/commonjs/models/mp-client-sdk.js.map +1 -0
  39. package/lib/commonjs/processors/data-element.processor.js +191 -0
  40. package/lib/commonjs/processors/data-element.processor.js.map +1 -0
  41. package/lib/commonjs/processors/qc.processor.js +111 -0
  42. package/lib/commonjs/processors/qc.processor.js.map +1 -0
  43. package/lib/commonjs/processors/tag.processor.js +432 -0
  44. package/lib/commonjs/processors/tag.processor.js.map +1 -0
  45. package/lib/commonjs/processors/trans-function.processor.js +91 -0
  46. package/lib/commonjs/processors/trans-function.processor.js.map +1 -0
  47. package/lib/commonjs/processors/visit-id.processor.js +172 -0
  48. package/lib/commonjs/processors/visit-id.processor.js.map +1 -0
  49. package/lib/module/common/app-types.js +2 -0
  50. package/lib/module/common/app-types.js.map +1 -0
  51. package/lib/module/common/constants.js +41 -0
  52. package/lib/module/common/constants.js.map +1 -0
  53. package/lib/module/common/data-store.js +346 -0
  54. package/lib/module/common/data-store.js.map +1 -0
  55. package/lib/module/common/event-bus.js +31 -0
  56. package/lib/module/common/event-bus.js.map +1 -0
  57. package/lib/module/common/logger.js +21 -0
  58. package/lib/module/common/logger.js.map +1 -0
  59. package/lib/module/common/network-service.js +73 -0
  60. package/lib/module/common/network-service.js.map +1 -0
  61. package/lib/module/common/reporter.js +88 -0
  62. package/lib/module/common/reporter.js.map +1 -0
  63. package/lib/module/common/utils.js +263 -0
  64. package/lib/module/common/utils.js.map +1 -0
  65. package/lib/module/coverage/clover.xml +6 -0
  66. package/lib/module/coverage/coverage-final.json +1 -0
  67. package/lib/module/coverage/lcov-report/base.css +224 -0
  68. package/lib/module/coverage/lcov-report/block-navigation.js +81 -0
  69. package/lib/module/coverage/lcov-report/block-navigation.js.map +1 -0
  70. package/lib/module/coverage/lcov-report/favicon.png +0 -0
  71. package/lib/module/coverage/lcov-report/index.html +101 -0
  72. package/lib/module/coverage/lcov-report/prettify.css +1 -0
  73. package/lib/module/coverage/lcov-report/prettify.js +993 -0
  74. package/lib/module/coverage/lcov-report/prettify.js.map +1 -0
  75. package/lib/module/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  76. package/lib/module/coverage/lcov-report/sorter.js +210 -0
  77. package/lib/module/coverage/lcov-report/sorter.js.map +1 -0
  78. package/lib/module/coverage/lcov.info +0 -0
  79. package/lib/module/eedl/eedl.js +246 -0
  80. package/lib/module/eedl/eedl.js.map +1 -0
  81. package/lib/module/index.js +176 -0
  82. package/lib/module/index.js.map +1 -0
  83. package/lib/module/models/mp-client-sdk.js +24 -0
  84. package/lib/module/models/mp-client-sdk.js.map +1 -0
  85. package/lib/module/processors/data-element.processor.js +176 -0
  86. package/lib/module/processors/data-element.processor.js.map +1 -0
  87. package/lib/module/processors/qc.processor.js +90 -0
  88. package/lib/module/processors/qc.processor.js.map +1 -0
  89. package/lib/module/processors/tag.processor.js +396 -0
  90. package/lib/module/processors/tag.processor.js.map +1 -0
  91. package/lib/module/processors/trans-function.processor.js +73 -0
  92. package/lib/module/processors/trans-function.processor.js.map +1 -0
  93. package/lib/module/processors/visit-id.processor.js +144 -0
  94. package/lib/module/processors/visit-id.processor.js.map +1 -0
  95. package/lib/typescript/common/app-types.d.ts +101 -0
  96. package/lib/typescript/common/constants.d.ts +21 -0
  97. package/lib/typescript/common/data-store.d.ts +81 -0
  98. package/lib/typescript/common/event-bus.d.ts +6 -0
  99. package/lib/typescript/common/logger.d.ts +5 -0
  100. package/lib/typescript/common/network-service.d.ts +8 -0
  101. package/lib/typescript/common/reporter.d.ts +12 -0
  102. package/lib/typescript/common/utils.d.ts +38 -0
  103. package/lib/typescript/eedl/eedl.d.ts +46 -0
  104. package/lib/typescript/index.d.ts +18 -0
  105. package/lib/typescript/models/mp-client-sdk.d.ts +157 -0
  106. package/lib/typescript/processors/data-element.processor.d.ts +12 -0
  107. package/lib/typescript/processors/qc.processor.d.ts +4 -0
  108. package/lib/typescript/processors/tag.processor.d.ts +27 -0
  109. package/lib/typescript/processors/trans-function.processor.d.ts +12 -0
  110. package/lib/typescript/processors/visit-id.processor.d.ts +7 -0
  111. package/package.json +170 -0
  112. package/src/common/app-types.ts +128 -0
  113. package/src/common/constants.ts +43 -0
  114. package/src/common/data-store.ts +333 -0
  115. package/src/common/event-bus.ts +35 -0
  116. package/src/common/logger.ts +19 -0
  117. package/src/common/network-service.ts +85 -0
  118. package/src/common/reporter.ts +110 -0
  119. package/src/common/utils.ts +281 -0
  120. package/src/coverage/clover.xml +6 -0
  121. package/src/coverage/coverage-final.json +1 -0
  122. package/src/coverage/lcov-report/base.css +224 -0
  123. package/src/coverage/lcov-report/block-navigation.js +87 -0
  124. package/src/coverage/lcov-report/favicon.png +0 -0
  125. package/src/coverage/lcov-report/index.html +101 -0
  126. package/src/coverage/lcov-report/prettify.css +1 -0
  127. package/src/coverage/lcov-report/prettify.js +2 -0
  128. package/src/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  129. package/src/coverage/lcov-report/sorter.js +196 -0
  130. package/src/coverage/lcov.info +0 -0
  131. package/src/eedl/eedl.ts +233 -0
  132. package/src/index.tsx +258 -0
  133. package/src/models/mp-client-sdk.ts +174 -0
  134. package/src/processors/data-element.processor.ts +249 -0
  135. package/src/processors/qc.processor.ts +115 -0
  136. package/src/processors/tag.processor.ts +527 -0
  137. package/src/processors/trans-function.processor.ts +85 -0
  138. package/src/processors/visit-id.processor.ts +164 -0
@@ -0,0 +1,333 @@
1
+ import AsyncStorage from '@react-native-async-storage/async-storage';
2
+ import type {
3
+ DeJsvHelper,
4
+ MapLike,
5
+ QcInfoItem,
6
+ RunQueueModel,
7
+ SdkInitOptions,
8
+ UrlInfo,
9
+ VisitInfo,
10
+ VisitorInfo,
11
+ } from './app-types';
12
+ import { Logger } from './logger';
13
+ import type {
14
+ ClientSdkDeItem,
15
+ ClientSdkPrItem,
16
+ ClientSdkQcItem,
17
+ ClientSdkTagItem,
18
+ MpClientSdk,
19
+ } from '../models/mp-client-sdk';
20
+ import { Platform } from 'react-native';
21
+ import { DeviceType, getDeviceTypeAsync } from 'expo-device';
22
+
23
+ export class DataStore {
24
+ private static isDataStoreInitialized = false;
25
+ private static dataElements: MapLike = {};
26
+ private static validQcList: string[];
27
+ private static validQcInfoList: Array<QcInfoItem> = [];
28
+ private static transFunctions: MapLike = {};
29
+ private static prStatus = true;
30
+ private static clientSdk: MpClientSdk;
31
+ private static urls: UrlInfo;
32
+ private static debugId: string;
33
+ private static visitorInfo: VisitorInfo;
34
+ private static visitInfo: VisitInfo;
35
+ private static pageLang = 'en';
36
+ private static mpEnvShort: 'stg' | 'prd';
37
+ private static devicePlatform = 'unknown';
38
+ private static deviceType = 'mobile';
39
+ private static runQueue: RunQueueModel[] = [];
40
+ private static isProcessing = false;
41
+ private static sdkInitOptions: SdkInitOptions;
42
+
43
+ static async init(clientSdk: MpClientSdk): Promise<void> {
44
+ this.clientSdk = clientSdk;
45
+ this.setMpEnv(clientSdk?.s?.ev === 'staging' ? 'stg' : 'prd');
46
+ if (typeof Platform !== 'undefined' && typeof Platform.OS !== 'undefined') {
47
+ this.devicePlatform = Platform.OS.toLowerCase();
48
+ }
49
+
50
+ const dType = await getDeviceTypeAsync();
51
+ if (dType === DeviceType.PHONE) {
52
+ this.deviceType = 'mobile';
53
+ } else if (dType === DeviceType.TABLET) {
54
+ this.deviceType = 'tablet';
55
+ } else {
56
+ this.deviceType = 'unknown';
57
+ }
58
+
59
+ this.resetCollectorUrl(
60
+ clientSdk.s.c_url,
61
+ clientSdk.s.ev,
62
+ clientSdk.s.v_id,
63
+ clientSdk.s.p_id
64
+ );
65
+ this.isDataStoreInitialized = true;
66
+ }
67
+
68
+ static resetCollectorUrl(
69
+ baseCollectorUrl: string,
70
+ sdkEnv: string,
71
+ vendorId: string,
72
+ projectId: string
73
+ ): boolean {
74
+ const tagsBaseUrl = `${baseCollectorUrl}/${
75
+ sdkEnv === 'staging' ? 's' : 'p'
76
+ }/${vendorId}/${projectId}`;
77
+ this.urls = {
78
+ tfUrl: tagsBaseUrl + '/sst',
79
+ errUrl: tagsBaseUrl + '/err',
80
+ rptUrl: tagsBaseUrl + '/report',
81
+ infoUrl: tagsBaseUrl + '/info',
82
+ baseUrl: baseCollectorUrl,
83
+ attrInfoUrl: baseCollectorUrl + `/idl/${vendorId}/attrInfo`,
84
+ idlUrl:
85
+ baseCollectorUrl +
86
+ `/idl/${vendorId}/idl${sdkEnv === 'staging' ? '-staging' : ''}`,
87
+ };
88
+ return true;
89
+ }
90
+
91
+ static enQueueTMFire(
92
+ sseOnly: boolean,
93
+ name: string,
94
+ dcrName: string,
95
+ eventId: string,
96
+ dcrPayload?: Record<string, any>
97
+ ): void {
98
+ this.runQueue.push({ sseOnly, name, dcrName, eventId, dcrPayload });
99
+ }
100
+
101
+ static isDataStoreReady(): boolean {
102
+ return this.isDataStoreInitialized;
103
+ }
104
+
105
+ static getSdkPcKey(): string {
106
+ return this.clientSdk.pcKey;
107
+ }
108
+
109
+ static getSdkInitOptions(): SdkInitOptions {
110
+ return this.sdkInitOptions;
111
+ }
112
+
113
+ static setSdkInitOptions(options: SdkInitOptions): void {
114
+ this.sdkInitOptions = options;
115
+ }
116
+
117
+ static deQueueTMFire(): RunQueueModel {
118
+ return this.runQueue.length > 0 ? this.runQueue.pop() : null;
119
+ }
120
+
121
+ static isTagManagerProcessing(): boolean {
122
+ return this.isProcessing;
123
+ }
124
+
125
+ static setTagManagerProcessing(status: boolean): void {
126
+ this.isProcessing = status;
127
+ }
128
+
129
+ static getSdkDataElements(): MapLike<ClientSdkDeItem> {
130
+ return this.clientSdk?.d || {};
131
+ }
132
+
133
+ static getSdkQC(): ClientSdkQcItem[] {
134
+ return this.clientSdk?.qc || [];
135
+ }
136
+
137
+ static hasOneSSTTag(): boolean {
138
+ return this.clientSdk.s.sst;
139
+ }
140
+
141
+ static getSSTDownStream(): 'j' | 'n' {
142
+ return this.clientSdk.s.dws_s_t;
143
+ }
144
+
145
+ static getClientDownStream(): 'j' | 'n' {
146
+ return this.clientSdk.s.dws_c_t;
147
+ }
148
+
149
+ static getClientSdk(): MpClientSdk {
150
+ return this.clientSdk;
151
+ }
152
+
153
+ static getSSTUrl(): string {
154
+ return this.urls.tfUrl;
155
+ }
156
+
157
+ static getAttrInfoUrl(): string {
158
+ return this.urls.attrInfoUrl;
159
+ }
160
+
161
+ static setVisitInfo(visitInfo: VisitInfo): void {
162
+ this.visitInfo = visitInfo;
163
+ }
164
+
165
+ static visitInfoToString(): string {
166
+ return this.visitInfo
167
+ ? `${this.visitInfo.visitVer}|${this.visitInfo.visitId}|${this.visitInfo.visitTs}|${this.visitInfo.visitCt}|${this.visitInfo.visitDepth}`
168
+ : undefined;
169
+ }
170
+
171
+ static setVisitorInfo(visitorInfo: VisitorInfo): void {
172
+ this.visitorInfo = visitorInfo;
173
+ }
174
+
175
+ static getDebugId(): string {
176
+ return this.debugId;
177
+ }
178
+
179
+ static getErrorReportUrl(): string {
180
+ return this.urls.errUrl;
181
+ }
182
+
183
+ static getIdlUrl(): string {
184
+ return this.urls?.idlUrl;
185
+ }
186
+
187
+ static getSdkVersion(): string {
188
+ // TODO: Fix this
189
+ return 'na';
190
+ }
191
+
192
+ static setDebugId(dbgId: string): void {
193
+ this.debugId = dbgId;
194
+ }
195
+
196
+ static shouldFireSstForEvent(eventName: string): boolean {
197
+ return this.clientSdk?.sseMpDlE?.indexOf(eventName) > -1;
198
+ }
199
+
200
+ static getUrlInfo(): UrlInfo {
201
+ return this.urls;
202
+ }
203
+
204
+ static getSdkTags(): MapLike<ClientSdkTagItem> {
205
+ return this?.clientSdk?.t ?? {};
206
+ }
207
+
208
+ static getSdkProviders(): MapLike<ClientSdkPrItem> {
209
+ return this?.clientSdk?.p ?? {};
210
+ }
211
+
212
+ static getPrivacyCompliance(): boolean {
213
+ return this.prStatus;
214
+ }
215
+
216
+ static setPrivacyCompliance(status: boolean): void {
217
+ this.prStatus = status;
218
+ }
219
+
220
+ static async storeData(key: string, value: any): Promise<void> {
221
+ await AsyncStorage.setItem(key, JSON.stringify(value));
222
+ }
223
+
224
+ static async getDataFromStorage<T>(key: string): Promise<T> {
225
+ const value = await AsyncStorage.getItem(key);
226
+ return value ? JSON.parse(value) : value;
227
+ }
228
+
229
+ static getDataElements(): MapLike {
230
+ return this.dataElements;
231
+ }
232
+
233
+ static setDataElements(de: MapLike): void {
234
+ this.dataElements = de;
235
+ }
236
+
237
+ static getPageLang(): string {
238
+ return this.pageLang;
239
+ }
240
+
241
+ static getDeviceOs(): string {
242
+ return this.devicePlatform;
243
+ }
244
+
245
+ static getDataElementHelpers(): DeJsvHelper {
246
+ return {
247
+ log: Logger.logDbg,
248
+ err: Logger.logError,
249
+ };
250
+ }
251
+
252
+ static getPageName(): string {
253
+ return (this.getDataElementValue('page_name') as string) || 'none';
254
+ }
255
+
256
+ static getDataElementValue(param: string): string | boolean | number {
257
+ return this.dataElements[param];
258
+ }
259
+
260
+ static setPageLang(lang: string): void {
261
+ this.pageLang = lang || 'en';
262
+ }
263
+
264
+ static getSdkPageLangKey(): string {
265
+ return this.clientSdk.langKey;
266
+ }
267
+
268
+ static setDataElement(key: string, value: any): void {
269
+ this.dataElements[key] = value;
270
+ }
271
+
272
+ static setValidQc(qcList: string[], qcInfo: QcInfoItem[]): void {
273
+ this.validQcList = qcList;
274
+ this.validQcInfoList = qcInfo;
275
+ }
276
+
277
+ static getValidQcList(): string[] {
278
+ return this.validQcList || [];
279
+ }
280
+
281
+ static getValidQcInfo(): QcInfoItem[] {
282
+ return this.validQcInfoList || [];
283
+ }
284
+
285
+ static setTransFunctions(elements: MapLike): MapLike {
286
+ this.transFunctions = elements;
287
+ return this.transFunctions;
288
+ }
289
+
290
+ static getTransFunctions(): MapLike {
291
+ return this.transFunctions;
292
+ }
293
+
294
+ static getOperatingSystem(): string {
295
+ return this.devicePlatform;
296
+ }
297
+
298
+ static getVisitId(): string {
299
+ return this.visitInfo?.visitId;
300
+ }
301
+
302
+ static getDeviceType(): string {
303
+ return this.deviceType;
304
+ }
305
+
306
+ static getDeviceId(): string {
307
+ return this.visitorInfo?.dId;
308
+ }
309
+
310
+ static getMpId(): string {
311
+ return this.visitorInfo?.mId;
312
+ }
313
+
314
+ static getVisitorInfoAsString(): string | undefined {
315
+ return this.visitorInfo ? JSON.stringify(this.visitorInfo) : undefined;
316
+ }
317
+
318
+ static getOrgId(): string {
319
+ return this?.clientSdk?.s?.v_id;
320
+ }
321
+
322
+ static getCoreVersion(): string {
323
+ return this?.clientSdk?.s?.v;
324
+ }
325
+
326
+ static getMpEnv(): 'stg' | 'prd' {
327
+ return this.mpEnvShort;
328
+ }
329
+
330
+ static setMpEnv(shortEnv: 'stg' | 'prd'): void {
331
+ this.mpEnvShort = shortEnv;
332
+ }
333
+ }
@@ -0,0 +1,35 @@
1
+ import type { EventBusCallback } from './app-types';
2
+ import { Logger } from './logger';
3
+
4
+ export class EventBus {
5
+ private static eventRegister: Map<string, Array<EventBusCallback>> = new Map<
6
+ string,
7
+ Array<EventBusCallback>
8
+ >();
9
+
10
+ static on(eventName: string, callback: EventBusCallback): void {
11
+ Logger.logDbg(`Registering Listener for event: ${eventName}`);
12
+ if (!this.eventRegister.has(eventName)) {
13
+ this.eventRegister.set(eventName, [callback]);
14
+ } else {
15
+ this.eventRegister.get(eventName).push(callback);
16
+ }
17
+ }
18
+
19
+ static triggerEvent(eventName: string, payload: any): void {
20
+ if (this.eventRegister.has(eventName)) {
21
+ for (const cb of this.eventRegister.get(eventName)) {
22
+ try {
23
+ Logger.logDbg(
24
+ `Sending event ${eventName} to all listeners. Count = ${
25
+ this.eventRegister.get(eventName).length
26
+ }`
27
+ );
28
+ cb(eventName, payload);
29
+ } catch (err) {
30
+ Logger.logError('Unable to process callback for event: ' + eventName);
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,19 @@
1
+ export class Logger {
2
+ static logDbg(...args: any): void {
3
+ if (console && console.log) {
4
+ console.log(...args);
5
+ }
6
+ }
7
+
8
+ static logWarn(...args: any): void {
9
+ if (console && console.warn) {
10
+ console.warn(...args);
11
+ }
12
+ }
13
+
14
+ static logError(...args: any): void {
15
+ if (console && console.error) {
16
+ console.error(...args);
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,85 @@
1
+ import { Logger } from './logger';
2
+ import type { MapLike, SdkInitOptions, VisitorInfo } from './app-types';
3
+ import { DataStore } from './data-store';
4
+ import { Reporter } from './reporter';
5
+ import { Utils } from './utils';
6
+ import type { MpClientSdk } from '../models/mp-client-sdk';
7
+
8
+ export class NetworkService {
9
+ public static async fetchIdlInfo(url: string): Promise<VisitorInfo> {
10
+ const response = await NetworkService.sendNetworkRequest('get', url);
11
+ if (response) {
12
+ return response.json();
13
+ } else {
14
+ return null;
15
+ }
16
+ }
17
+
18
+ public static async refreshClientSdkJson(
19
+ sdkInitOptions: SdkInitOptions
20
+ ): Promise<void> {
21
+ try {
22
+ const response = await this.sendNetworkRequest(
23
+ 'get',
24
+ `${sdkInitOptions.baseUrl}/${sdkInitOptions.projectId}${
25
+ sdkInitOptions.env === 'staging' ? '-staging' : ''
26
+ }.json?cb=${Utils.getUniqueID()}`
27
+ );
28
+
29
+ if (response) {
30
+ const sdkJson: MpClientSdk = await response.json();
31
+ Logger.logDbg('Client Sdk:: ', sdkJson);
32
+ // sdkJson.s.c_url = 'http://localhost:3001';
33
+ await DataStore.init(sdkJson);
34
+ }
35
+ } catch (err) {
36
+ Reporter.reportError('refreshClientSdk', err);
37
+ throw new Error('Unable to fetch MagicPixel SDK at this time.');
38
+ }
39
+ }
40
+
41
+ public static sendPostRequest(url: string, body: MapLike): void {
42
+ this.sendNetworkRequest('post', url, body).catch((err) => {
43
+ Logger.logError(err);
44
+ });
45
+ }
46
+
47
+ public static sendGetRequest(url: string): void {
48
+ this.sendNetworkRequest('get', url).catch((err) => {
49
+ Logger.logError(err);
50
+ });
51
+ }
52
+
53
+ private static async sendNetworkRequest(
54
+ method: 'get' | 'post',
55
+ url: string,
56
+ body?: MapLike
57
+ ): Promise<any> {
58
+ try {
59
+ Logger.logDbg(
60
+ `Sending ${method} request to ${url} with body ${JSON.stringify(body)}`
61
+ );
62
+ if (method === 'get') {
63
+ return await fetch(url, {
64
+ headers: {
65
+ 'cache-control': 'no-store',
66
+ },
67
+ });
68
+ } else if (method === 'post') {
69
+ return await fetch(url, {
70
+ method: 'POST',
71
+ headers: {
72
+ 'Content-Type': 'text/plain',
73
+ 'cache-control': 'no-store',
74
+ },
75
+ body: JSON.stringify(body ?? '{}'),
76
+ });
77
+ } else {
78
+ throw new Error('Unsupported method: ' + method);
79
+ }
80
+ } catch (err) {
81
+ Logger.logError(err);
82
+ return null;
83
+ }
84
+ }
85
+ }
@@ -0,0 +1,110 @@
1
+ import type { MapLike, Report, ReportTagItem } from './app-types';
2
+ import { Logger } from './logger';
3
+ import { DataStore } from './data-store';
4
+ import { Utils } from './utils';
5
+ import { NetworkService } from './network-service';
6
+
7
+ export class Reporter {
8
+ static reportMap: MapLike<Report> = {};
9
+
10
+ static initReporter(
11
+ envName: string,
12
+ envId: string,
13
+ deviceType: string,
14
+ language: string,
15
+ deviceOs: string, // device os is being used as a browser proxy
16
+ pageName: string,
17
+ evtName: string,
18
+ evtId: string,
19
+ clientTagsDownStream: 'j' | 'n',
20
+ clientSdkVersion: string,
21
+ initialItems: ReportTagItem[] = []
22
+ ): void {
23
+ this.reportMap[evtId] = {
24
+ envName,
25
+ envId,
26
+ pname: pageName || 'none',
27
+ evt: evtName,
28
+ evtId: evtId,
29
+ bw: Utils.bwToNum(deviceOs),
30
+ dt: Utils.dtToNum(deviceType),
31
+ ln: Utils.langToNum(language),
32
+ dm: 'na',
33
+ items: initialItems || [],
34
+ dws: clientTagsDownStream,
35
+ v: clientSdkVersion,
36
+ tagCt: 0,
37
+ };
38
+ }
39
+
40
+ static setExpectedTagCount(ct: number, evtId: string): void {
41
+ const reportParent = this.reportMap[evtId];
42
+ if (reportParent) {
43
+ reportParent.tagCt = ct;
44
+ }
45
+ }
46
+
47
+ static reportItem(item: ReportTagItem, evtId: string): void {
48
+ const reportParent = this.reportMap[evtId];
49
+ if (reportParent) {
50
+ reportParent.items.push(item);
51
+ if (
52
+ reportParent?.items?.length === reportParent?.tagCt &&
53
+ reportParent?.items?.length > 0
54
+ ) {
55
+ Logger.logDbg('Hit expected tag count. Reporting');
56
+ this.publishReport(evtId);
57
+ }
58
+ }
59
+ }
60
+
61
+ static publishReport(evtId: string): void {
62
+ const data = (this.reportMap[evtId] || {}) as MapLike;
63
+ const postUrl = `${DataStore.getUrlInfo().rptUrl}/${
64
+ data.dws ?? 'n'
65
+ }/${Utils.getUniqueID()}`;
66
+ Logger.logDbg('Posting Info: ', postUrl);
67
+ data.debugId = DataStore.getDebugId();
68
+ NetworkService.sendPostRequest(postUrl, data);
69
+ }
70
+
71
+ static reportError(methodMetaData: string, err: any): void {
72
+ Logger.logDbg('Reporting...', methodMetaData, err);
73
+ const data = {
74
+ message: err?.message,
75
+ stack: err?.stack || undefined,
76
+ version: DataStore.getSdkVersion(),
77
+ };
78
+
79
+ NetworkService.sendPostRequest(
80
+ `${DataStore.getErrorReportUrl()}/${Utils.getUniqueID()}`,
81
+ data
82
+ );
83
+ }
84
+
85
+ static async postSST(
86
+ data: MapLike,
87
+ envName: string,
88
+ envId: string,
89
+ serverTagDownStream: 'j' | 'n',
90
+ eventName: string,
91
+ evtId: string
92
+ ): Promise<void> {
93
+ data.env = envName;
94
+ data.envId = envId;
95
+ data.evtName = eventName;
96
+ data.dws = serverTagDownStream;
97
+ data.debugId = DataStore.getDebugId();
98
+ data.dm = 'n/a';
99
+ data.v = DataStore.getCoreVersion();
100
+ // return postHttp(`${getUrlInfo().tfUrl}/${getUniqueID()}`, 'POST', data);
101
+ NetworkService.sendPostRequest(
102
+ `${DataStore.getSSTUrl()}/${data.dws ?? 'n'}/${evtId}`,
103
+ data
104
+ );
105
+ }
106
+
107
+ static postAttrInfo(payload: MapLike): void {
108
+ return NetworkService.sendPostRequest(DataStore.getAttrInfoUrl(), payload);
109
+ }
110
+ }