This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
forked from valora-inc/wallet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathValoraAnalytics.ts
299 lines (264 loc) · 9.59 KB
/
ValoraAnalytics.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import Analytics, { Analytics as analytics } from '@segment/analytics-react-native'
import Adjust from '@segment/analytics-react-native-adjust'
import CleverTapSegment from '@segment/analytics-react-native-clevertap'
import Firebase from '@segment/analytics-react-native-firebase'
import { sha256FromString } from 'ethereumjs-util'
import { Platform } from 'react-native'
import DeviceInfo from 'react-native-device-info'
import { check, PERMISSIONS, request, RESULTS } from 'react-native-permissions'
import { AppEvents } from 'src/analytics/Events'
import { AnalyticsPropertiesList } from 'src/analytics/Properties'
import { getCurrentUserTraits } from 'src/analytics/selectors'
import { DEFAULT_TESTNET, FIREBASE_ENABLED, isE2EEnv, SEGMENT_API_KEY } from 'src/config'
import { store } from 'src/redux/store'
import Logger from 'src/utils/Logger'
import { isPresent } from 'src/utils/typescript'
const TAG = 'ValoraAnalytics'
async function getDeviceInfo() {
return {
AppName: DeviceInfo.getApplicationName(),
Brand: DeviceInfo.getBrand(),
BuildNumber: DeviceInfo.getBuildNumber(),
BundleId: DeviceInfo.getBundleId(),
Carrier: await DeviceInfo.getCarrier(),
DeviceId: DeviceInfo.getDeviceId(),
FirstInstallTime: await DeviceInfo.getFirstInstallTime(),
FontScale: await DeviceInfo.getFontScale(),
FreeDiskStorage: await DeviceInfo.getFreeDiskStorage(),
InstallReferrer: await DeviceInfo.getInstallReferrer(),
InstanceID: await DeviceInfo.getInstanceId(),
LastUpdateTime: await DeviceInfo.getLastUpdateTime(),
Manufacturer: await DeviceInfo.getManufacturer(),
MaxMemory: await DeviceInfo.getMaxMemory(),
Model: DeviceInfo.getModel(),
ReadableVersion: DeviceInfo.getReadableVersion(),
SystemName: DeviceInfo.getSystemName(),
SystemVersion: DeviceInfo.getSystemVersion(),
TotalDiskCapacity: await DeviceInfo.getTotalDiskCapacity(),
TotalMemory: await DeviceInfo.getTotalMemory(),
UniqueID: DeviceInfo.getUniqueId(),
UserAgent: await DeviceInfo.getUserAgent(),
Version: DeviceInfo.getVersion(),
isEmulator: await DeviceInfo.isEmulator(),
isTablet: DeviceInfo.isTablet(),
UsedMemory: await DeviceInfo.getUsedMemory(),
}
}
const SEGMENT_OPTIONS: analytics.Configuration = {
using: [FIREBASE_ENABLED ? Firebase : undefined, Adjust, CleverTapSegment].filter(isPresent),
flushAt: 20,
debug: __DEV__,
trackAppLifecycleEvents: true,
recordScreenViews: true,
trackAttributionData: true,
ios: {
trackAdvertising: false,
trackDeepLinks: true,
},
}
class ValoraAnalytics {
sessionId: string = ''
deviceInfo: object = {}
private currentScreenId: string | undefined
private prevScreenId: string | undefined
async init() {
try {
if (!SEGMENT_API_KEY) {
throw Error('API Key not present, likely due to environment. Skipping enabling')
}
await Analytics.setup(SEGMENT_API_KEY, SEGMENT_OPTIONS)
try {
const deviceInfo = await getDeviceInfo()
this.deviceInfo = deviceInfo
this.sessionId = sha256FromString(
'0x' + deviceInfo.UniqueID.split('-').join('') + String(Date.now())
)
.toString('hex')
.slice(2)
} catch (error) {
Logger.error(TAG, 'getDeviceInfo error', error)
}
Logger.info(TAG, 'Segment Analytics Integration initialized!')
} catch (error) {
Logger.error(TAG, `Segment setup error: ${error.message}\n`, error)
}
}
isEnabled() {
// Remove __DEV__ here to test analytics in dev builds
return !__DEV__ && store.getState().app.analyticsEnabled
}
startSession(
eventName: typeof AppEvents.app_launched,
eventProperties: AnalyticsPropertiesList[AppEvents.app_launched]
) {
this.track(eventName, {
deviceInfo: this.deviceInfo,
...eventProperties,
})
this.requestTrackingPermissionIfNeeded().catch((error) => {
Logger.error(TAG, 'Failure while requesting tracking permission', error)
})
}
getSessionId() {
return this.sessionId
}
track<EventName extends keyof AnalyticsPropertiesList>(
...args: undefined extends AnalyticsPropertiesList[EventName]
? [EventName] | [EventName, AnalyticsPropertiesList[EventName]]
: [EventName, AnalyticsPropertiesList[EventName]]
) {
const [eventName, eventProperties] = args
if (!this.isEnabled()) {
Logger.debug(TAG, `Analytics is disabled, not tracking event ${eventName}`)
return
}
if (!SEGMENT_API_KEY) {
Logger.debug(TAG, `No API key, not tracking event ${eventName}`)
return
}
const props: {} = {
...this.getSuperProps(),
...eventProperties,
}
Logger.info(TAG, `Tracking event ${eventName} with properties: ${JSON.stringify(props)}`)
Analytics.track(eventName, props).catch((err) => {
Logger.error(TAG, `Failed to track event ${eventName}`, err)
})
}
identify(userID: string | null, traits: {}) {
if (!this.isEnabled()) {
Logger.debug(TAG, `Analytics is disabled, not tracking user ${userID}`)
return
}
if (!SEGMENT_API_KEY) {
Logger.debug(TAG, `No API key, not tracking user ${userID}`)
return
}
// Only identify user if userID (walletAddress) is set
if (!userID) {
return
}
Analytics.identify(userID, traits).catch((err) => {
Logger.error(TAG, `Failed to identify user ${userID}`, err)
})
}
page(screenId: string, eventProperties = {}) {
if (!this.isEnabled || !SEGMENT_API_KEY) {
return
}
if (screenId !== this.currentScreenId) {
this.prevScreenId = this.currentScreenId
this.currentScreenId = screenId
}
const props: {} = {
...this.getSuperProps(),
...eventProperties,
}
Analytics.screen(screenId, props).catch((err) => {
Logger.error(TAG, 'Error tracking page', err)
})
}
async reset() {
try {
await Analytics.flush()
await Analytics.reset()
} catch (error) {
Logger.error(TAG, 'Error resetting analytics', error)
}
}
private async requestTrackingPermissionIfNeeded() {
// TODO: remove `isE2EEnv` and set permission via Detox when we upgrade
if (Platform.OS !== 'ios' || isE2EEnv) {
return
}
const appTrackingPermission = await check(PERMISSIONS.IOS.APP_TRACKING_TRANSPARENCY)
Logger.debug(TAG, `iOS tracking permission: ${appTrackingPermission}`)
if (appTrackingPermission !== RESULTS.DENIED) {
// The permission has already been requested / is not requestable
// See https://github.com/zoontek/react-native-permissions#permissions-statuses
return
}
Logger.debug(TAG, `iOS requesting tracking permission`)
this.track(AppEvents.request_tracking_permission_started, {
currentPermission: appTrackingPermission,
})
const newAppTrackingPermission = await request(PERMISSIONS.IOS.APP_TRACKING_TRANSPARENCY)
Logger.debug(TAG, `iOS tracking permission after request: ${newAppTrackingPermission}`)
if (newAppTrackingPermission === RESULTS.GRANTED) {
this.track(AppEvents.request_tracking_permission_accepted, {
newPermission: newAppTrackingPermission,
})
} else {
this.track(AppEvents.request_tracking_permission_declined, {
newPermission: newAppTrackingPermission,
})
}
}
// Super props, i.e. props sent with all events
private getSuperProps() {
const traits = getCurrentUserTraits(store.getState())
// Prefix super props with `s` so they don't clash with events props
const prefixedSuperProps = Object.fromEntries(
Object.entries({
...traits,
currentScreenId: this.currentScreenId,
prevScreenId: this.prevScreenId,
}).map(([key, value]) => [`s${key.charAt(0).toUpperCase() + key.slice(1)}`, value])
)
return {
// Legacy super props
timestamp: Date.now(),
sessionId: this.sessionId,
userAddress: traits.walletAddress,
celoNetwork: DEFAULT_TESTNET,
// Prefixed super props
...prefixedSuperProps,
}
}
}
let isInitialized = false
type KeysOfType<T, TProp> = { [P in keyof T]: T[P] extends TProp ? P : never }[keyof T]
type ValoraAnalyticsKeyFunction = KeysOfType<ValoraAnalytics, Function>
// Type checked function keys to queue until `init` has finished
const funcsToQueue = new Set<ValoraAnalyticsKeyFunction>([
'startSession',
'track',
'identify',
'page',
])
let queuedCalls: Function[] = []
function isFuncToQueue(prop: string | number | symbol): prop is ValoraAnalyticsKeyFunction {
return funcsToQueue.has(prop as ValoraAnalyticsKeyFunction)
}
/**
* Use a proxy to queue specific calls until async `init` has finished
* So all events are sent with the right props to our initialized analytics integrations
*/
export default new Proxy(new ValoraAnalytics(), {
get: function (target, prop, receiver) {
if (!isInitialized) {
if (prop === 'init') {
return new Proxy(target[prop], {
apply: (target, thisArg, argumentsList) => {
return Reflect.apply(target, thisArg, argumentsList).finally(() => {
isInitialized = true
// Init finished, we can now process queued calls
for (const fn of queuedCalls) {
fn()
}
queuedCalls = []
})
},
})
} else if (isFuncToQueue(prop)) {
return new Proxy(target[prop], {
apply: (target, thisArg, argumentsList) => {
queuedCalls.push(() => Reflect.apply(target, thisArg, argumentsList))
Logger.debug(TAG, `Queued call to ${prop}`, ...argumentsList)
},
})
}
}
return Reflect.get(target, prop, receiver)
},
})