/
Segment.ts
116 lines (100 loc) · 3.67 KB
/
Segment.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
import { UnavailabilityError } from '@unimodules/core';
import { Platform } from 'react-native';
import ExponentSegment from './ExponentSegment';
export type SegmentOptions = {
androidWriteKey?: string;
iosWriteKey?: string;
};
export function initialize(options: SegmentOptions): void {
if (!ExponentSegment.initialize) {
throw new UnavailabilityError('expo-analytics-segment', 'initialize');
}
const platformWriteKey = Platform.select({
ios: options.iosWriteKey,
android: options.androidWriteKey,
});
if (platformWriteKey) {
ExponentSegment.initialize(platformWriteKey);
} else {
throw new Error('You must provide a platform-specific write key to initialize Segment.');
}
}
export function identify(userId: string): void {
if (!ExponentSegment.identify) {
throw new UnavailabilityError('expo-analytics-segment', 'identify');
}
ExponentSegment.identify(userId);
}
export function identifyWithTraits(userId: string, traits: { [key: string]: any }): void {
if (!ExponentSegment.identifyWithTraits) {
throw new UnavailabilityError('expo-analytics-segment', 'identifyWithTraits');
}
ExponentSegment.identifyWithTraits(userId, traits);
}
export function group(groupId: string): void {
if (!ExponentSegment.group) {
throw new UnavailabilityError('expo-analytics-segment', 'group');
}
ExponentSegment.group(groupId);
}
export function groupWithTraits(groupId: string, traits: { [key: string]: any }): void {
if (!ExponentSegment.groupWithTraits) {
throw new UnavailabilityError('expo-analytics-segment', 'groupWithTraits');
}
ExponentSegment.groupWithTraits(groupId, traits);
}
export async function alias(newId: string, options?: { [key: string]: any }): Promise<boolean> {
if (!ExponentSegment.alias) {
throw new UnavailabilityError('expo-analytics-segment', 'alias');
}
return await ExponentSegment.alias(newId, options);
}
export function reset(): void {
if (!ExponentSegment.reset) {
throw new UnavailabilityError('expo-analytics-segment', 'reset');
}
ExponentSegment.reset();
}
export function track(event: string): void {
if (!ExponentSegment.track) {
throw new UnavailabilityError('expo-analytics-segment', 'track');
}
ExponentSegment.track(event);
}
export function trackWithProperties(event: string, properties: { [key: string]: any }): void {
if (!ExponentSegment.trackWithProperties) {
throw new UnavailabilityError('expo-analytics-segment', 'trackWithProperties');
}
ExponentSegment.trackWithProperties(event, properties);
}
export function screen(screenName: string): void {
if (!ExponentSegment.screen) {
throw new UnavailabilityError('expo-analytics-segment', 'screen');
}
ExponentSegment.screen(screenName);
}
export function screenWithProperties(event: string, properties: { [key: string]: any }): void {
if (!ExponentSegment.screenWithProperties) {
throw new UnavailabilityError('expo-analytics-segment', 'screenWithProperties');
}
ExponentSegment.screenWithProperties(event, properties);
}
export function flush(): void {
if (!ExponentSegment.flush) {
throw new UnavailabilityError('expo-analytics-segment', 'flush');
}
ExponentSegment.flush();
}
export async function getEnabledAsync(): Promise<boolean> {
if (!ExponentSegment.getEnabledAsync) {
throw new UnavailabilityError('expo-analytics-segment', 'getEnabledAsync');
}
const isEnabledNumber = await ExponentSegment.getEnabledAsync();
return !!isEnabledNumber;
}
export async function setEnabledAsync(enabled: boolean): Promise<void> {
if (!ExponentSegment.setEnabledAsync) {
throw new UnavailabilityError('expo-analytics-segment', 'setEnabledAsync');
}
await ExponentSegment.setEnabledAsync(enabled);
}