Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: merge context to preserve plugin injected properties #487

Merged
merged 1 commit into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
44 changes: 44 additions & 0 deletions packages/core/src/__tests__/internal/checkInstalledVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
);
});
});
7 changes: 4 additions & 3 deletions packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Expand Down