Skip to content

Commit 9e7d62d

Browse files
alanjcharlesAlan Charlesoscb
authored
fix: format integration settings correctly in the event integrations
Co-authored-by: Alan Charles <alancharles@Alans-MacBook-Pro-2.local> Co-authored-by: Oscar Bazaldua <511911+oscb@users.noreply.github.com>
1 parent efb2805 commit 9e7d62d

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

packages/core/src/__tests__/internal/fetchSettings.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('internal #getSettings', () => {
3030
});
3131

3232
it('fetches the settings succesfully ', async () => {
33-
const mockJSONResponse = { foo: 'bar' };
33+
const mockJSONResponse = { integrations: { foo: 'bar' } };
3434
const mockResponse = Promise.resolve({
3535
json: () => mockJSONResponse,
3636
});
@@ -43,10 +43,10 @@ describe('internal #getSettings', () => {
4343
'https://cdn-settings.segment.com/v1/projects/123-456/settings'
4444
);
4545

46-
expect(setSettingsSpy).toHaveBeenCalledWith(mockJSONResponse);
47-
expect(store.settings.get()).toEqual(mockJSONResponse);
46+
expect(setSettingsSpy).toHaveBeenCalledWith(mockJSONResponse.integrations);
47+
expect(store.settings.get()).toEqual(mockJSONResponse.integrations);
4848
expect(client.settings.get()).toEqual({
49-
...mockJSONResponse,
49+
...mockJSONResponse.integrations,
5050
});
5151
});
5252

packages/core/src/analytics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ export class SegmentClient {
239239
try {
240240
const res = await fetch(settingsEndpoint);
241241
const resJson = await res.json();
242+
const integrations = resJson.integrations;
242243
this.logger.info(`Received settings from Segment succesfully.`);
243-
this.store.settings.set(resJson);
244+
this.store.settings.set(integrations);
244245
} catch {
245246
this.logger.warn(
246247
`Could not receive settings from Segment. ${

packages/core/src/plugins/SegmentDestination.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ export class SegmentDestination extends DestinationPlugin {
2929
// Disable all destinations that have a device mode plugin
3030
const deviceModePlugins =
3131
plugins?.map((plugin) => (plugin as DestinationPlugin).key) ?? [];
32-
const cloudSettings: SegmentAPIIntegrations = {
33-
...pluginSettings,
34-
};
35-
for (const key of deviceModePlugins) {
36-
if (key in cloudSettings) {
37-
cloudSettings[key] = false;
32+
const disabledCloudIntegrations: SegmentAPIIntegrations = {};
33+
if (pluginSettings !== undefined) {
34+
for (const key of deviceModePlugins) {
35+
if (key in pluginSettings) {
36+
disabledCloudIntegrations[key] = false;
37+
}
3838
}
3939
}
4040

4141
// User/event defined integrations override the cloud/device mode merge
4242
const mergedEvent = {
4343
...event,
4444
integrations: {
45-
...cloudSettings,
45+
...disabledCloudIntegrations,
4646
...event?.integrations,
4747
},
4848
};

packages/core/src/timeline.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,21 @@ export class Timeline {
2626
this.plugins[type] = [plugin];
2727
}
2828
const settings = plugin.analytics?.settings.get();
29-
if (settings) {
29+
let hasInitialSettings = false;
30+
if (settings !== undefined) {
3031
plugin.update({ integrations: settings }, UpdateType.initial);
32+
hasInitialSettings = true;
3133
}
34+
35+
plugin.analytics?.settings.onChange((newSettings) => {
36+
if (newSettings !== undefined) {
37+
plugin.update(
38+
{ integrations: newSettings },
39+
hasInitialSettings ? UpdateType.refresh : UpdateType.initial
40+
);
41+
hasInitialSettings = true;
42+
}
43+
});
3244
}
3345

3446
remove(plugin: Plugin) {

packages/plugins/plugin-amplitudeSession/src/AmplitudeSessionPlugin.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,12 @@ export class AmplitudeSessionPlugin extends EventPlugin {
7878
private insertSession = (event: SegmentEvent) => {
7979
const returnEvent = event;
8080
const integrations = event.integrations;
81-
if (integrations) {
82-
returnEvent.integrations = {
83-
...integrations,
84-
[this.key]: {
85-
session_id: this.sessionId,
86-
},
87-
};
88-
}
81+
returnEvent.integrations = {
82+
...integrations,
83+
[this.key]: {
84+
session_id: this.sessionId,
85+
},
86+
};
8987
return returnEvent;
9088
};
9189

0 commit comments

Comments
 (0)