diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 0303d7e0b..22e46eec6 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -286,10 +286,10 @@ PODS: - React - RNGestureHandler (2.3.0): - React-Core - - segment-analytics-react-native (2.1.8): + - segment-analytics-react-native (2.1.9): - React-Core - sovran-react-native - - segment-analytics-react-native-plugin-idfa (0.2.0-beta): + - segment-analytics-react-native-plugin-idfa (0.2.1): - React-Core - sovran-react-native (0.2.6): - React-Core @@ -459,11 +459,11 @@ SPEC CHECKSUMS: RNCAsyncStorage: b49b4e38a1548d03b74b30e558a1d18465b94be7 RNCMaskedView: 0e1bc4bfa8365eba5fbbb71e07fbdc0555249489 RNGestureHandler: 77d59828d40838c9fabb76a12d2d0a80c006906f - segment-analytics-react-native: 1b6238e92a7d9e85148b7113ef3b6c529d1cfc29 - segment-analytics-react-native-plugin-idfa: 2dc6e38506a5b034db4a4cf16db48643b2f356a2 + segment-analytics-react-native: 4b7a033f75bd5f0d50ca700c36048bdd2a997a76 + segment-analytics-react-native-plugin-idfa: 80e5d610f537156833eabea12a1804523355de95 sovran-react-native: ef02f663b489ac5e63ea7b80cd8426bf82992263 Yoga: 90dcd029e45d8a7c1ff059e8b3c6612ff409061a PODFILE CHECKSUM: 0c7eb82d495ca56953c50916b7b49e7512632eb6 -COCOAPODS: 1.11.2 +COCOAPODS: 1.11.3 diff --git a/packages/core/package.json b/packages/core/package.json index 4cdf8c44d..ffd9383dc 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -47,6 +47,7 @@ "dependencies": { "@react-native-async-storage/async-storage": "^1.15.17", "@segment/sovran-react-native": "^0.2.6", + "deepmerge": "^4.2.2", "js-base64": "^3.7.2", "nanoid": "^3.1.25" }, diff --git a/packages/core/src/__tests__/internal/checkInstalledVersion.test.ts b/packages/core/src/__tests__/internal/checkInstalledVersion.test.ts index 5e3b2af95..b749fca0c 100644 --- a/packages/core/src/__tests__/internal/checkInstalledVersion.test.ts +++ b/packages/core/src/__tests__/internal/checkInstalledVersion.test.ts @@ -3,6 +3,7 @@ import { getMockLogger } from '../__helpers__/mockLogger'; import * as context from '../../context'; import { MockSegmentStore } from '../__helpers__/mockSegmentStore'; import { Context, EventType } from '../../types'; +import deepmerge from 'deepmerge'; jest .spyOn(Date.prototype, 'toISOString') @@ -161,4 +162,47 @@ describe('internal #checkInstalledVersion', () => { type: EventType.TrackEvent, }); }); + + it('merges context and preserves context injected by plugins during configure', async () => { + const injectedContextByPlugins = { + device: { + adTrackingEnabled: true, + advertisingId: 'mock-advertising-id', + }, + }; + + const store = new MockSegmentStore({ + context: { + ...currentContext, + ...injectedContextByPlugins, + }, + }); + + client = new SegmentClient({ + ...clientArgs, + store, + }); + + const newContext = { + ...currentContext, // Just adding the full object to prevent TS complaining about missing properties + app: { + version: '1.5', + build: '2', + name: 'Test', + namespace: 'Test', + }, + device: { + manufacturer: 'Apple', + model: 'iPhone', + name: 'iPhone', + type: 'iOS', + }, + }; + jest.spyOn(context, 'getContext').mockResolvedValueOnce(newContext); + await client.init(); + + expect(store.context.get()).toEqual( + deepmerge(newContext, injectedContextByPlugins) + ); + }); }); diff --git a/packages/core/src/analytics.ts b/packages/core/src/analytics.ts index 0d8ed6da2..85978198c 100644 --- a/packages/core/src/analytics.ts +++ b/packages/core/src/analytics.ts @@ -1,7 +1,7 @@ -import { AppState, AppStateStatus } from 'react-native'; import type { Unsubscribe } from '@segment/sovran-react-native'; +import deepmerge from 'deepmerge'; +import { AppState, AppStateStatus } from 'react-native'; import { getContext } from './context'; - import { applyRawEventData, createAliasEvent, @@ -566,7 +566,8 @@ export class SegmentClient { const previousContext = this.store.context.get(); - this.store.context.set(context); + // Only overwrite the previous context values to preserve any values that are added by enrichment plugins like IDFA + this.store.context.set(deepmerge(previousContext ?? {}, context)); if (!this.config.trackAppLifecycleEvents) { return;