diff --git a/packages/expo-analytics-segment/android/src/main/java/expo/modules/analytics/segment/SegmentModule.java b/packages/expo-analytics-segment/android/src/main/java/expo/modules/analytics/segment/SegmentModule.java index 9943e7fd630db..ab72e4f5fc50a 100644 --- a/packages/expo-analytics-segment/android/src/main/java/expo/modules/analytics/segment/SegmentModule.java +++ b/packages/expo-analytics-segment/android/src/main/java/expo/modules/analytics/segment/SegmentModule.java @@ -112,7 +112,7 @@ public String getName() { } @ExpoMethod - public void initializeAndroid(final String writeKey, Promise promise) { + public void initialize(final String writeKey, Promise promise) { Analytics.Builder builder = new Analytics.Builder(mContext, writeKey); builder.tag(Integer.toString(sCurrentTag++)); builder.use(FirebaseIntegration.FACTORY); @@ -121,12 +121,6 @@ public void initializeAndroid(final String writeKey, Promise promise) { promise.resolve(null); } - @ExpoMethod - public void initializeIOS(final String writeKey, Promise promise) { - // NO-OP. Need this here because Segment has different keys for iOS and Android. - promise.reject("E_WRONG_PLATFORM", "Method initializeIOS should not be called on Android, please file an issue on GitHub."); - } - @ExpoMethod public void identify(final String userId, Promise promise) { if (mClient != null) { diff --git a/packages/expo-analytics-segment/build/Segment.js b/packages/expo-analytics-segment/build/Segment.js index ec0eff1a1984f..0896d96009c72 100644 --- a/packages/expo-analytics-segment/build/Segment.js +++ b/packages/expo-analytics-segment/build/Segment.js @@ -2,14 +2,18 @@ import { UnavailabilityError } from '@unimodules/core'; import { Platform } from 'react-native'; import ExponentSegment from './ExponentSegment'; export function initialize(options) { - if (Platform.OS === 'android') { - ExponentSegment.initializeAndroid(options.androidWriteKey); + if (!ExponentSegment.initialize) { + throw new UnavailabilityError('expo-analytics-segment', 'initialize'); } - else if (Platform.OS === 'ios') { - ExponentSegment.initializeIOS(options.iosWriteKey); + const platformWriteKey = Platform.select({ + ios: options.iosWriteKey, + android: options.androidWriteKey, + }); + if (platformWriteKey) { + ExponentSegment.initialize(platformWriteKey); } else { - throw new UnavailabilityError('expo-analytics-segment', 'initialize'); + throw new Error('You must provide a platform-specific write key to initialize Segment.'); } } export function identify(userId) { diff --git a/packages/expo-analytics-segment/build/Segment.js.map b/packages/expo-analytics-segment/build/Segment.js.map index 1c93f91464321..081c4b136b514 100644 --- a/packages/expo-analytics-segment/build/Segment.js.map +++ b/packages/expo-analytics-segment/build/Segment.js.map @@ -1 +1 @@ -{"version":3,"file":"Segment.js","sourceRoot":"","sources":["../src/Segment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAOhD,MAAM,UAAU,UAAU,CAAC,OAAuB;IAChD,IAAI,QAAQ,CAAC,EAAE,KAAK,SAAS,EAAE;QAC7B,eAAe,CAAC,iBAAiB,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC;KAC5D;SAAM,IAAI,QAAQ,CAAC,EAAE,KAAK,KAAK,EAAE;QAChC,eAAe,CAAC,aAAa,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;KACpD;SAAM;QACL,MAAM,IAAI,mBAAmB,CAAC,wBAAwB,EAAE,YAAY,CAAC,CAAC;KACvE;AACH,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,MAAc;IACrC,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE;QAC7B,MAAM,IAAI,mBAAmB,CAAC,wBAAwB,EAAE,UAAU,CAAC,CAAC;KACrE;IACD,eAAe,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AACnC,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,MAAc,EAAE,MAA8B;IAC/E,IAAI,CAAC,eAAe,CAAC,kBAAkB,EAAE;QACvC,MAAM,IAAI,mBAAmB,CAAC,wBAAwB,EAAE,oBAAoB,CAAC,CAAC;KAC/E;IACD,eAAe,CAAC,kBAAkB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AACrD,CAAC;AAED,MAAM,UAAU,KA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ailabilityError } from '@unimodules/core';\nimport { Platform } from 'react-native';\n\nimport ExponentSegment from './ExponentSegment';\n\nexport type SegmentOptions = {\n androidWriteKey?: string;\n iosWriteKey?: string;\n};\n\nexport function initialize(options: SegmentOptions): void {\n if (Platform.OS === 'android') {\n ExponentSegment.initializeAndroid(options.androidWriteKey);\n } else if (Platform.OS === 'ios') {\n ExponentSegment.initializeIOS(options.iosWriteKey);\n } else {\n throw new UnavailabilityError('expo-analytics-segment', 'initialize');\n }\n}\n\nexport function identify(userId: string): void {\n if (!ExponentSegment.identify) {\n throw new UnavailabilityError('expo-analytics-segment', 'identify');\n }\n ExponentSegment.identify(userId);\n}\n\nexport function identifyWithTraits(userId: string, traits: { [key: string]: any }): void {\n if (!ExponentSegment.identifyWithTraits) {\n throw new UnavailabilityError('expo-analytics-segment', 'identifyWithTraits');\n }\n ExponentSegment.identifyWithTraits(userId, traits);\n}\n\nexport function group(groupId: string): void {\n if (!ExponentSegment.group) {\n throw new UnavailabilityError('expo-analytics-segment', 'group');\n }\n ExponentSegment.group(groupId);\n}\n\nexport function groupWithTraits(groupId: string, traits: { [key: string]: any }): void {\n if (!ExponentSegment.groupWithTraits) {\n throw new UnavailabilityError('expo-analytics-segment', 'groupWithTraits');\n }\n ExponentSegment.groupWithTraits(groupId, traits);\n}\n\nexport async function alias(newId: string, options?: { [key: string]: any }): Promise {\n if (!ExponentSegment.alias) {\n throw new UnavailabilityError('expo-analytics-segment', 'alias');\n }\n return await ExponentSegment.alias(newId, options);\n}\n\nexport function reset(): void {\n if (!ExponentSegment.reset) {\n throw new UnavailabilityError('expo-analytics-segment', 'reset');\n }\n ExponentSegment.reset();\n}\n\nexport function track(event: string): void {\n if (!ExponentSegment.track) {\n throw new UnavailabilityError('expo-analytics-segment', 'track');\n }\n ExponentSegment.track(event);\n}\n\nexport function trackWithProperties(event: string, properties: { [key: string]: any }): void {\n if (!ExponentSegment.trackWithProperties) {\n throw new UnavailabilityError('expo-analytics-segment', 'trackWithProperties');\n }\n ExponentSegment.trackWithProperties(event, properties);\n}\n\nexport function screen(screenName: string): void {\n if (!ExponentSegment.screen) {\n throw new UnavailabilityError('expo-analytics-segment', 'screen');\n }\n ExponentSegment.screen(screenName);\n}\n\nexport function screenWithProperties(event: string, properties: { [key: string]: any }): void {\n if (!ExponentSegment.screenWithProperties) {\n throw new UnavailabilityError('expo-analytics-segment', 'screenWithProperties');\n }\n ExponentSegment.screenWithProperties(event, properties);\n}\n\nexport function flush(): void {\n if (!ExponentSegment.flush) {\n throw new UnavailabilityError('expo-analytics-segment', 'flush');\n }\n ExponentSegment.flush();\n}\n\nexport async function getEnabledAsync(): Promise {\n if (!ExponentSegment.getEnabledAsync) {\n throw new UnavailabilityError('expo-analytics-segment', 'getEnabledAsync');\n }\n const isEnabledNumber = await ExponentSegment.getEnabledAsync();\n return !!isEnabledNumber;\n}\n\nexport async function setEnabledAsync(enabled: boolean): Promise {\n if (!ExponentSegment.setEnabledAsync) {\n throw new UnavailabilityError('expo-analytics-segment', 'setEnabledAsync');\n }\n await ExponentSegment.setEnabledAsync(enabled);\n}\n"]} \ No newline at end of file +{"version":3,"file":"Segment.js","sourceRoot":"","sources":["../src/Segment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAOhD,MAAM,UAAU,UAAU,CAAC,OAAuB;IAChD,IAAI,CAAC,eAAe,CAAC,UAAU,EAAE;QAC/B,MAAM,IAAI,mBAAmB,CAAC,wBAAwB,EAAE,YAAY,CAAC,CAAC;KACvE;IACD,MAAM,gBAAgB,GAAG,QAAQ,CAAC,MAAM,CAAC;QACvC,GAAG,EAAE,OAAO,CAAC,WAAW;QACxB,OAAO,EAAE,OAAO,CAAC,eAAe;KACjC,CAAC,CAAC;IACH,IAAI,gBAAgB,EAAE;QACpB,eAAe,CAAC,UAA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,EAAE,OAAO,CAAC,CAAC;KAClE;IACD,eAAe,CAAC,KAAK,EAAE,CAAC;AAC1B,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe;IACnC,IAAI,CAAC,eAAe,CAAC,eAAe,EAAE;QACpC,MAAM,IAAI,mBAAmB,CAAC,wBAAwB,EAAE,iBAAiB,CAAC,CAAC;KAC5E;IACD,MAAM,eAAe,GAAG,MAAM,eAAe,CAAC,eAAe,EAAE,CAAC;IAChE,OAAO,CAAC,CAAC,eAAe,CAAC;AAC3B,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,OAAgB;IACpD,IAAI,CAAC,eAAe,CAAC,eAAe,EAAE;QACpC,MAAM,IAAI,mBAAmB,CAAC,wBAAwB,EAAE,iBAAiB,CAAC,CAAC;KAC5E;IACD,MAAM,eAAe,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;AACjD,CAAC","sourcesContent":["import { UnavailabilityError } from '@unimodules/core';\nimport { Platform } from 'react-native';\n\nimport ExponentSegment from './ExponentSegment';\n\nexport type SegmentOptions = {\n androidWriteKey?: string;\n iosWriteKey?: string;\n};\n\nexport function initialize(options: SegmentOptions): void {\n if (!ExponentSegment.initialize) {\n throw new UnavailabilityError('expo-analytics-segment', 'initialize');\n }\n const platformWriteKey = Platform.select({\n ios: options.iosWriteKey,\n android: options.androidWriteKey,\n });\n if (platformWriteKey) {\n ExponentSegment.initialize(platformWriteKey);\n } else {\n throw new Error('You must provide a platform-specific write key to initialize Segment.');\n }\n}\n\nexport function identify(userId: string): void {\n if (!ExponentSegment.identify) {\n throw new UnavailabilityError('expo-analytics-segment', 'identify');\n }\n ExponentSegment.identify(userId);\n}\n\nexport function identifyWithTraits(userId: string, traits: { [key: string]: any }): void {\n if (!ExponentSegment.identifyWithTraits) {\n throw new UnavailabilityError('expo-analytics-segment', 'identifyWithTraits');\n }\n ExponentSegment.identifyWithTraits(userId, traits);\n}\n\nexport function group(groupId: string): void {\n if (!ExponentSegment.group) {\n throw new UnavailabilityError('expo-analytics-segment', 'group');\n }\n ExponentSegment.group(groupId);\n}\n\nexport function groupWithTraits(groupId: string, traits: { [key: string]: any }): void {\n if (!ExponentSegment.groupWithTraits) {\n throw new UnavailabilityError('expo-analytics-segment', 'groupWithTraits');\n }\n ExponentSegment.groupWithTraits(groupId, traits);\n}\n\nexport async function alias(newId: string, options?: { [key: string]: any }): Promise {\n if (!ExponentSegment.alias) {\n throw new UnavailabilityError('expo-analytics-segment', 'alias');\n }\n return await ExponentSegment.alias(newId, options);\n}\n\nexport function reset(): void {\n if (!ExponentSegment.reset) {\n throw new UnavailabilityError('expo-analytics-segment', 'reset');\n }\n ExponentSegment.reset();\n}\n\nexport function track(event: string): void {\n if (!ExponentSegment.track) {\n throw new UnavailabilityError('expo-analytics-segment', 'track');\n }\n ExponentSegment.track(event);\n}\n\nexport function trackWithProperties(event: string, properties: { [key: string]: any }): void {\n if (!ExponentSegment.trackWithProperties) {\n throw new UnavailabilityError('expo-analytics-segment', 'trackWithProperties');\n }\n ExponentSegment.trackWithProperties(event, properties);\n}\n\nexport function screen(screenName: string): void {\n if (!ExponentSegment.screen) {\n throw new UnavailabilityError('expo-analytics-segment', 'screen');\n }\n ExponentSegment.screen(screenName);\n}\n\nexport function screenWithProperties(event: string, properties: { [key: string]: any }): void {\n if (!ExponentSegment.screenWithProperties) {\n throw new UnavailabilityError('expo-analytics-segment', 'screenWithProperties');\n }\n ExponentSegment.screenWithProperties(event, properties);\n}\n\nexport function flush(): void {\n if (!ExponentSegment.flush) {\n throw new UnavailabilityError('expo-analytics-segment', 'flush');\n }\n ExponentSegment.flush();\n}\n\nexport async function getEnabledAsync(): Promise {\n if (!ExponentSegment.getEnabledAsync) {\n throw new UnavailabilityError('expo-analytics-segment', 'getEnabledAsync');\n }\n const isEnabledNumber = await ExponentSegment.getEnabledAsync();\n return !!isEnabledNumber;\n}\n\nexport async function setEnabledAsync(enabled: boolean): Promise {\n if (!ExponentSegment.setEnabledAsync) {\n throw new UnavailabilityError('expo-analytics-segment', 'setEnabledAsync');\n }\n await ExponentSegment.setEnabledAsync(enabled);\n}\n"]} \ No newline at end of file diff --git a/packages/expo-analytics-segment/ios/EXSegment/EXSegment.m b/packages/expo-analytics-segment/ios/EXSegment/EXSegment.m index ac325c1e9fb5e..1f74d0edd3609 100644 --- a/packages/expo-analytics-segment/ios/EXSegment/EXSegment.m +++ b/packages/expo-analytics-segment/ios/EXSegment/EXSegment.m @@ -16,8 +16,8 @@ @implementation EXSegment UM_EXPORT_MODULE(ExponentSegment) -UM_EXPORT_METHOD_AS(initializeIOS, - initializeIOS:(NSString *)writeKey +UM_EXPORT_METHOD_AS(initialize, + initialize:(NSString *)writeKey resolver:(UMPromiseResolveBlock)resolve rejecter:(UMPromiseRejectBlock)reject) { @@ -30,15 +30,6 @@ @implementation EXSegment resolve(nil); } -UM_EXPORT_METHOD_AS(initializeAndroid, - initializeAndroid:(NSString *)writeKey - resolver:(UMPromiseResolveBlock)resolve - rejecter:(UMPromiseRejectBlock)reject) -{ - // NO-OP. Need this here because Segment has different keys for iOS and Android. - reject(@"E_WRONG_PLATFORM", @"Method initializeAndroid should not be called on iOS, please file an issue on GitHub.", nil); -} - UM_EXPORT_METHOD_AS(identify, identify:(NSString *)userId resolver:(UMPromiseResolveBlock)resolve diff --git a/packages/expo-analytics-segment/src/Segment.ts b/packages/expo-analytics-segment/src/Segment.ts index eed17d1375292..3ee2239cedab5 100644 --- a/packages/expo-analytics-segment/src/Segment.ts +++ b/packages/expo-analytics-segment/src/Segment.ts @@ -9,13 +9,18 @@ export type SegmentOptions = { }; export function initialize(options: SegmentOptions): void { - if (Platform.OS === 'android') { - ExponentSegment.initializeAndroid(options.androidWriteKey); - } else if (Platform.OS === 'ios') { - ExponentSegment.initializeIOS(options.iosWriteKey); - } else { + 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 { diff --git a/packages/expo-analytics-segment/src/__tests__/Segment-test.android.ts b/packages/expo-analytics-segment/src/__tests__/Segment-test.android.ts index 1863526e632d7..c17608e6cb279 100644 --- a/packages/expo-analytics-segment/src/__tests__/Segment-test.android.ts +++ b/packages/expo-analytics-segment/src/__tests__/Segment-test.android.ts @@ -6,9 +6,8 @@ const mockOptions = { iosWriteKey: 'ios-write-key', }; -it(`initializes`, () => { +it(`initializes once for android`, () => { Segment.initialize(mockOptions); - - expect(ExponentSegment.initializeAndroid).toHaveBeenCalledWith(mockOptions.androidWriteKey); - expect(ExponentSegment.initializeIOS).not.toHaveBeenCalled(); + expect(ExponentSegment.initialize).toHaveBeenCalledWith(mockOptions.androidWriteKey); + expect(ExponentSegment.initialize).toHaveBeenCalledTimes(1); }); diff --git a/packages/expo-analytics-segment/src/__tests__/Segment-test.ios.ts b/packages/expo-analytics-segment/src/__tests__/Segment-test.ios.ts index 93cc4ce1985b2..b6823c394cb7d 100644 --- a/packages/expo-analytics-segment/src/__tests__/Segment-test.ios.ts +++ b/packages/expo-analytics-segment/src/__tests__/Segment-test.ios.ts @@ -6,8 +6,18 @@ const mockOptions = { iosWriteKey: 'ios-write-key', }; -it(`initializes`, () => { +it(`initializes once for ios`, () => { Segment.initialize(mockOptions); - expect(ExponentSegment.initializeIOS).toHaveBeenCalledWith(mockOptions.iosWriteKey); - expect(ExponentSegment.initializeAndroid).not.toHaveBeenCalled(); + expect(ExponentSegment.initialize).toHaveBeenCalledWith(mockOptions.iosWriteKey); + expect(ExponentSegment.initialize).toHaveBeenCalledTimes(1); +}); + +it(`calling with an empty object results in error`, () => { + expect(() => { + Segment.initialize({}); + }).toThrowError( + new Error('You must provide a platform-specific write key to initialize Segment.') + ); + + expect(ExponentSegment.initialize).toHaveBeenCalledTimes(0); }); diff --git a/packages/jest-expo/src/preset/expoModules.js b/packages/jest-expo/src/preset/expoModules.js index 2ea80afd79828..9bf941c4bb740 100644 --- a/packages/jest-expo/src/preset/expoModules.js +++ b/packages/jest-expo/src/preset/expoModules.js @@ -584,15 +584,14 @@ module.exports = { { key: 3, argumentsCount: 1, name: 'screen' }, { key: 4, argumentsCount: 1, name: 'identify' }, { key: 5, argumentsCount: 2, name: 'identifyWithTraits' }, - { key: 6, argumentsCount: 1, name: 'initializeAndroid' }, - { key: 7, argumentsCount: 2, name: 'trackWithProperties' }, - { key: 8, argumentsCount: 1, name: 'initializeIOS' }, - { key: 9, argumentsCount: 2, name: 'groupWithTraits' }, + { key: 6, argumentsCount: 2, name: 'trackWithProperties' }, + { key: 7, argumentsCount: 2, name: 'groupWithTraits' }, + { key: 8, argumentsCount: 1, name: 'initialize' }, + { key: 9, argumentsCount: 0, name: 'getEnabledAsync' }, { key: 10, argumentsCount: 2, name: 'alias' }, { key: 11, argumentsCount: 1, name: 'group' }, { key: 12, argumentsCount: 1, name: 'track' }, - { key: 13, argumentsCount: 0, name: 'getEnabledAsync' }, - { key: 14, argumentsCount: 0, name: 'reset' }, + { key: 13, argumentsCount: 0, name: 'reset' }, ], ExponentSpeech: [ { key: 0, argumentsCount: 3, name: 'speak' },