Created
July 24, 2019 20:39
-
-
Save ronal2do/ad014e4d8f805a5e3ec832dd83886852 to your computer and use it in GitHub Desktop.
services/Analytics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Constants from 'expo-constants'; | |
import * as Amplitude from 'expo-analytics-amplitude'; | |
import { TrackingOptions, normalizeTrackingOptions } from './AnalyticsUtils'; | |
import { ENV } from '../environment'; | |
let isInitialized = false; | |
const apiKey = ENV.AMPLITUDE; | |
const { installationId, deviceName, deviceId, deviceYearClass, expoVersion, nativeAppVersion, nativeBuildVersion, platform, manifest: { version }} = Constants | |
export const TrackingOpts = { | |
installationId, | |
deviceName, | |
deviceId, | |
deviceYearClass, | |
expoVersion, | |
nativeAppVersion, | |
nativeBuildVersion, | |
platform, | |
version, | |
} | |
export const events = { | |
BOOTSTRAP: 'BOOTSTRAP', | |
START_GAME: 'START_GAME', | |
ERROR: 'ERROR', | |
// user | |
USER_LOGGED_IN: 'USER_LOGGED_IN', | |
USER_LOGGED_OUT: 'USER_LOGGED_OUT', | |
USER_CREATED_ACCOUNT: 'USER_CREATED_ACCOUNT', | |
USER_RESET_PASSWORD: 'USER_RESET_PASSWORD', | |
USER_FORGET_PASSWORD: 'USER_FORGET_PASSWORD', | |
// etc... | |
}; | |
export function initialize(): void { | |
if (isInitialized || !apiKey) { | |
return; | |
} | |
Amplitude.initialize(apiKey); | |
isInitialized = true; | |
} | |
export function identify(id: string | null, options?: TrackingOptions) { | |
initialize(); | |
const properties = normalizeTrackingOptions(options); | |
if (id) { | |
Amplitude.setUserId(id); | |
if (properties) { | |
Amplitude.setUserProperties(properties); | |
} | |
} else { | |
Amplitude.clearUserProperties(); | |
} | |
} | |
export function track(event: string, options?: TrackingOptions): void { | |
initialize(); | |
const properties = normalizeTrackingOptions(options); | |
if (properties) { | |
Amplitude.logEventWithProperties(event, properties); | |
} else { | |
Amplitude.logEvent(event); | |
} | |
} | |
export default { | |
events, | |
initialize, | |
identify, | |
track, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment