UNPKG

4.01 kBPlain TextView Raw
1import { UnavailabilityError } from '@unimodules/core';
2import { Platform } from 'react-native';
3
4import ExponentSegment from './ExponentSegment';
5
6export type InitializeOptions = {
7 androidWriteKey?: string;
8 iosWriteKey?: string;
9};
10
11export type CommonOptions = { [key: string]: any } | null;
12
13export function initialize(options: InitializeOptions): void {
14 if (!ExponentSegment.initialize) {
15 throw new UnavailabilityError('expo-analytics-segment', 'initialize');
16 }
17 const platformWriteKey = Platform.select({
18 ios: options.iosWriteKey,
19 android: options.androidWriteKey,
20 });
21 if (platformWriteKey) {
22 ExponentSegment.initialize(platformWriteKey);
23 } else {
24 throw new Error('You must provide a platform-specific write key to initialize Segment.');
25 }
26}
27
28export function identify(userId: string): void {
29 if (!ExponentSegment.identify) {
30 throw new UnavailabilityError('expo-analytics-segment', 'identify');
31 }
32 ExponentSegment.identify(userId);
33}
34
35export function identifyWithTraits(
36 userId: string,
37 traits: { [key: string]: any },
38 options: CommonOptions = null
39): void {
40 if (!ExponentSegment.identifyWithTraits) {
41 throw new UnavailabilityError('expo-analytics-segment', 'identifyWithTraits');
42 }
43 ExponentSegment.identifyWithTraits(userId, traits, options);
44}
45
46export function group(groupId: string): void {
47 if (!ExponentSegment.group) {
48 throw new UnavailabilityError('expo-analytics-segment', 'group');
49 }
50 ExponentSegment.group(groupId);
51}
52
53export function groupWithTraits(
54 groupId: string,
55 traits: { [key: string]: any },
56 options: CommonOptions = null
57): void {
58 if (!ExponentSegment.groupWithTraits) {
59 throw new UnavailabilityError('expo-analytics-segment', 'groupWithTraits');
60 }
61 ExponentSegment.groupWithTraits(groupId, traits, options);
62}
63
64export async function alias(newId: string, options: CommonOptions = null): Promise<boolean> {
65 if (!ExponentSegment.alias) {
66 throw new UnavailabilityError('expo-analytics-segment', 'alias');
67 }
68 return await ExponentSegment.alias(newId, options);
69}
70
71export function reset(): void {
72 if (!ExponentSegment.reset) {
73 throw new UnavailabilityError('expo-analytics-segment', 'reset');
74 }
75 ExponentSegment.reset();
76}
77
78export function track(event: string): void {
79 if (!ExponentSegment.track) {
80 throw new UnavailabilityError('expo-analytics-segment', 'track');
81 }
82 ExponentSegment.track(event);
83}
84
85export function trackWithProperties(
86 event: string,
87 properties: { [key: string]: any },
88 options: CommonOptions = null
89): void {
90 if (!ExponentSegment.trackWithProperties) {
91 throw new UnavailabilityError('expo-analytics-segment', 'trackWithProperties');
92 }
93 ExponentSegment.trackWithProperties(event, properties, options);
94}
95
96export function screen(screenName: string): void {
97 if (!ExponentSegment.screen) {
98 throw new UnavailabilityError('expo-analytics-segment', 'screen');
99 }
100 ExponentSegment.screen(screenName);
101}
102
103export function screenWithProperties(
104 event: string,
105 properties: { [key: string]: any },
106 options: CommonOptions = null
107): void {
108 if (!ExponentSegment.screenWithProperties) {
109 throw new UnavailabilityError('expo-analytics-segment', 'screenWithProperties');
110 }
111 ExponentSegment.screenWithProperties(event, properties, options);
112}
113
114export function flush(): void {
115 if (!ExponentSegment.flush) {
116 throw new UnavailabilityError('expo-analytics-segment', 'flush');
117 }
118 ExponentSegment.flush();
119}
120
121export async function getEnabledAsync(): Promise<boolean> {
122 if (!ExponentSegment.getEnabledAsync) {
123 throw new UnavailabilityError('expo-analytics-segment', 'getEnabledAsync');
124 }
125 const isEnabledNumber = await ExponentSegment.getEnabledAsync();
126 return !!isEnabledNumber;
127}
128
129export async function setEnabledAsync(enabled: boolean): Promise<void> {
130 if (!ExponentSegment.setEnabledAsync) {
131 throw new UnavailabilityError('expo-analytics-segment', 'setEnabledAsync');
132 }
133 await ExponentSegment.setEnabledAsync(enabled);
134}