-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-native-userreport-sdk.js
More file actions
140 lines (111 loc) · 3.96 KB
/
react-native-userreport-sdk.js
File metadata and controls
140 lines (111 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import ReactNative from 'react-native'; // eslint-disable-line import/no-unresolved
import ReactNativeDeviceInfo from 'react-native-device-info'; // eslint-disable-line import/no-unresolved
/* debug */
let useDebug = false;
const debugInfo = (value) => {
if (useDebug) console.info(`UR SDK :: ${value}`); // eslint-disable-line no-console
};
const debugError = (value) => {
if (useDebug) console.error(`UR SDK :: ${value}`); // eslint-disable-line no-console
};
export const setDebug = (value) => {
if (value) { // log if debug enabled
useDebug = value;
}
debugInfo(`Debug ${value ? 'enabled' : 'disabled'}`);
if (!value) { // log if debug disabled
useDebug = value;
}
};
/* dnt */
let useAnonymousTracking = false;
export const setAnonymousTracking = (value) => {
useAnonymousTracking = value;
debugInfo(`Anonymous tracking ${value ? 'enabled' : 'disabled'}`);
};
/* idfa dialog */
let useIdfaDialog = true;
export const setIdfaDialog = (value) => {
useIdfaDialog = value;
debugInfo(`IDFA dialog ${value ? 'enabled' : 'disabled'}`);
};
const loadIdfa = async () => {
const nativeModule = ReactNative.NativeModules.RNAdvertisingId;
const nativeMethod = ReactNative.Platform.OS !== 'ios' || useIdfaDialog
? nativeModule.getAdvertisingId : nativeModule.getAdvertisingIdLegacy;
try {
const data = await nativeMethod();
return data.advertisingId || '';
} catch (error) {
return '';
}
};
/* configure */
let configurePromiseResolve;
let configurePromiseReject;
const configurePromise = new Promise((resolve, reject) => {
configurePromiseResolve = resolve;
configurePromiseReject = reject;
});
export const configure = async (publisherId, mediaId) => {
const domain = useAnonymousTracking
? 'sak.dnt-userreport.com'
: 'sak.userreport.com';
const platformOs = ReactNative.Platform.OS;
if (platformOs !== 'ios' && platformOs !== 'android') {
debugError(`Platform ${platformOs} is not supported`);
}
const url = `https://${domain}/${publisherId}/media/${mediaId}/${platformOs}.json`;
debugInfo(`Loading media configuration ${url}`);
try {
const result = await fetch(url);
const json = await result.json();
return configurePromiseResolve(json);
} catch (error) {
return configurePromiseReject(error);
}
};
/* tracking */
const fireTrackingPixel = async (trackingCode, consentString) => {
const domain = useAnonymousTracking
? 'visitanalytics.dnt-userreport.com'
: 'visitanalytics.userreport.com';
const random = Math.random().toString(36).slice(2);
const idfa = await loadIdfa();
const idfv = ReactNativeDeviceInfo.getUniqueId();
const bundleId = ReactNativeDeviceInfo.getBundleId();
const appVersion = ReactNativeDeviceInfo.getVersion();
const path = `https://${domain}/hit.gif`;
const params = `?t=${encodeURIComponent(trackingCode)}` // eslint-disable-line prefer-template
+ `&rnd=${random}`
+ (!useAnonymousTracking && idfa ? `&d=${encodeURIComponent(idfa)}` : '')
+ (!useAnonymousTracking && idfv ? `&idfv=${encodeURIComponent(idfv)}` : '')
+ `&appid=${encodeURIComponent(bundleId)}`
+ `&appver=${encodeURIComponent(appVersion)}`
+ (consentString ? `&gdpr_consent=${encodeURIComponent(consentString)}` : '');
const url = path + params;
debugInfo(`Firing tracking pixel ${url}`);
return fetch(url);
};
export const trackScreenView = async () => {
const { kitTcode, hardcodedConsent } = await configurePromise;
return fireTrackingPixel(kitTcode, hardcodedConsent);
};
export const trackSectionScreenView = async (sectionId) => {
const { sections, hardcodedConsent } = await configurePromise;
const sectionTcode = sections && sections[sectionId];
if (!sectionTcode) {
debugError(`Unable to find section for ${sectionId}`);
return Promise.reject();
}
return fireTrackingPixel(sectionTcode, hardcodedConsent);
};
/* exports */
export default {
setDebug,
setAnonymousTracking,
setIdfaDialog,
configure,
trackScreenView,
trackSectionScreenView,
};