Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix: merge context to preserve plugin injected properties (segmentio#487
Browse files Browse the repository at this point in the history
)
  • Loading branch information
oscb authored Apr 4, 2022
1 parent 686f73f commit 9586a7a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,4 @@ SPEC CHECKSUMS:

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

0 comments on commit 9586a7a

Please sign in to comment.