UNPKG

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