From 955c78ec05c587e943344db8f3811da57a609990 Mon Sep 17 00:00:00 2001 From: Alan Charles Date: Tue, 11 Jan 2022 10:36:37 -0700 Subject: [PATCH 1/3] fix: add fix for deviceId on android builds --- example/src/App.tsx | 3 ++- .../AnalyticsReactNativeModule.kt | 4 ++-- packages/core/src/analytics.ts | 3 ++- packages/core/src/context.ts | 10 ++++++++-- packages/core/src/types.ts | 1 + 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 7c839598..ac711fdc 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -28,8 +28,9 @@ import { IdfaPlugin } from '@segment/analytics-react-native-plugin-idfa'; import { AmplitudeSessionPlugin } from '@segment/analytics-react-native-plugin-amplitude-session'; const segmentClient = createClient({ - writeKey: 'WRITE KEY', + writeKey: 'Write Key', trackAppLifecycleEvents: true, + collectDeviceId: true, }); const LoggerPlugin = new Logger(); diff --git a/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativeModule.kt b/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativeModule.kt index 6c4c0ce7..acf89481 100644 --- a/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativeModule.kt +++ b/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativeModule.kt @@ -76,7 +76,7 @@ class AnalyticsReactNativeModule(reactContext: ReactApplicationContext) : ReactC } @ReactMethod - fun getContextInfo(config: ReadableMap, promise: Promise) { + fun getContextInfo(collectDeviceId: Boolean, promise: Promise) { val appName: String = reactApplicationContext.applicationInfo.loadLabel(reactApplicationContext.packageManager).toString() val appVersion: String = pInfo.versionName val buildNumber = getBuildNumber() @@ -98,7 +98,7 @@ class AnalyticsReactNativeModule(reactContext: ReactApplicationContext) : ReactC contextInfo.putString("appVersion", appVersion) contextInfo.putString("buildNumber", buildNumber) contextInfo.putString("bundleId", bundleId) - contextInfo.putString("deviceId", getUniqueId(config.hasKey("collectDeviceId") && config.getBoolean("collectDeviceId"))) + contextInfo.putString("deviceId", getUniqueId(collectDeviceId)) contextInfo.putString("deviceName", Build.DEVICE) contextInfo.putString("deviceType", "android") contextInfo.putString("manufacturer", Build.MANUFACTURER) diff --git a/packages/core/src/analytics.ts b/packages/core/src/analytics.ts index 31c623ec..b615b6f2 100644 --- a/packages/core/src/analytics.ts +++ b/packages/core/src/analytics.ts @@ -557,7 +557,8 @@ export class SegmentClient { * Application Opened - the previously detected version is same as the current version */ private async checkInstalledVersion() { - const context = await getContext(undefined); + const context = await getContext(undefined, this.config); + const previousContext = this.store.context.get(); this.store.context.set(context); diff --git a/packages/core/src/context.ts b/packages/core/src/context.ts index dc03f26a..cea46dba 100644 --- a/packages/core/src/context.ts +++ b/packages/core/src/context.ts @@ -8,8 +8,12 @@ import type { UserTraits, } from './types'; +interface GetContextConfig { + collectDeviceId?: boolean; +} export const getContext = async ( - userTraits: UserTraits = {} + userTraits: UserTraits = {}, + { collectDeviceId = false }: GetContextConfig = {} ): Promise => { const { AnalyticsReactNative } = NativeModules; @@ -31,7 +35,9 @@ export const getContext = async ( deviceId, deviceType, screenDensity, - }: NativeContextInfo = await AnalyticsReactNative.getContextInfo({}); + }: NativeContextInfo = await AnalyticsReactNative.getContextInfo( + collectDeviceId + ); const device: ContextDevice = { id: deviceId, diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 649b3031..ed7ae456 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -123,6 +123,7 @@ export type Config = { maxEventsToRetry?: number; defaultSettings?: SegmentAPISettings; autoAddSegmentDestination?: boolean; + collectDeviceId?: boolean; }; export type ClientMethods = { From a612f808f9b40215ffb28fd0e8d5f530167ce4df Mon Sep 17 00:00:00 2001 From: Alan Charles Date: Tue, 11 Jan 2022 11:33:35 -0700 Subject: [PATCH 2/3] fix: refactor to pass config object into getContext --- example/src/App.tsx | 2 +- .../AnalyticsReactNativeModule.kt | 4 ++-- packages/core/src/context.ts | 6 ++---- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index ac711fdc..9e368439 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -28,7 +28,7 @@ import { IdfaPlugin } from '@segment/analytics-react-native-plugin-idfa'; import { AmplitudeSessionPlugin } from '@segment/analytics-react-native-plugin-amplitude-session'; const segmentClient = createClient({ - writeKey: 'Write Key', + writeKey: 'QKoI2cHIPlixGDB358Y3T86tVqpaBZK3', trackAppLifecycleEvents: true, collectDeviceId: true, }); diff --git a/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativeModule.kt b/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativeModule.kt index acf89481..6c4c0ce7 100644 --- a/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativeModule.kt +++ b/packages/core/android/src/main/java/com/segmentanalyticsreactnative/AnalyticsReactNativeModule.kt @@ -76,7 +76,7 @@ class AnalyticsReactNativeModule(reactContext: ReactApplicationContext) : ReactC } @ReactMethod - fun getContextInfo(collectDeviceId: Boolean, promise: Promise) { + fun getContextInfo(config: ReadableMap, promise: Promise) { val appName: String = reactApplicationContext.applicationInfo.loadLabel(reactApplicationContext.packageManager).toString() val appVersion: String = pInfo.versionName val buildNumber = getBuildNumber() @@ -98,7 +98,7 @@ class AnalyticsReactNativeModule(reactContext: ReactApplicationContext) : ReactC contextInfo.putString("appVersion", appVersion) contextInfo.putString("buildNumber", buildNumber) contextInfo.putString("bundleId", bundleId) - contextInfo.putString("deviceId", getUniqueId(collectDeviceId)) + contextInfo.putString("deviceId", getUniqueId(config.hasKey("collectDeviceId") && config.getBoolean("collectDeviceId"))) contextInfo.putString("deviceName", Build.DEVICE) contextInfo.putString("deviceType", "android") contextInfo.putString("manufacturer", Build.MANUFACTURER) diff --git a/packages/core/src/context.ts b/packages/core/src/context.ts index cea46dba..ca9fe1c6 100644 --- a/packages/core/src/context.ts +++ b/packages/core/src/context.ts @@ -13,7 +13,7 @@ interface GetContextConfig { } export const getContext = async ( userTraits: UserTraits = {}, - { collectDeviceId = false }: GetContextConfig = {} + config: GetContextConfig = {} ): Promise => { const { AnalyticsReactNative } = NativeModules; @@ -35,9 +35,7 @@ export const getContext = async ( deviceId, deviceType, screenDensity, - }: NativeContextInfo = await AnalyticsReactNative.getContextInfo( - collectDeviceId - ); + }: NativeContextInfo = await AnalyticsReactNative.getContextInfo(config); const device: ContextDevice = { id: deviceId, From 266db35620fe4d8f6faa2c5c1180ca4d57a6cf09 Mon Sep 17 00:00:00 2001 From: Alan Charles Date: Tue, 11 Jan 2022 11:34:05 -0700 Subject: [PATCH 3/3] fix: remove write key in example --- example/src/App.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 9e368439..db2ac2ca 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -28,7 +28,7 @@ import { IdfaPlugin } from '@segment/analytics-react-native-plugin-idfa'; import { AmplitudeSessionPlugin } from '@segment/analytics-react-native-plugin-amplitude-session'; const segmentClient = createClient({ - writeKey: 'QKoI2cHIPlixGDB358Y3T86tVqpaBZK3', + writeKey: 'WRITE KEY', trackAppLifecycleEvents: true, collectDeviceId: true, });