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

Commit

Permalink
fix: format integration settings correctly in the event integrations
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Charles <[email protected]>
Co-authored-by: Oscar Bazaldua <[email protected]>
  • Loading branch information
3 people authored Feb 23, 2022
1 parent efb2805 commit 9e7d62d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/__tests__/internal/fetchSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('internal #getSettings', () => {
});

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

expect(setSettingsSpy).toHaveBeenCalledWith(mockJSONResponse);
expect(store.settings.get()).toEqual(mockJSONResponse);
expect(setSettingsSpy).toHaveBeenCalledWith(mockJSONResponse.integrations);
expect(store.settings.get()).toEqual(mockJSONResponse.integrations);
expect(client.settings.get()).toEqual({
...mockJSONResponse,
...mockJSONResponse.integrations,
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ export class SegmentClient {
try {
const res = await fetch(settingsEndpoint);
const resJson = await res.json();
const integrations = resJson.integrations;
this.logger.info(`Received settings from Segment succesfully.`);
this.store.settings.set(resJson);
this.store.settings.set(integrations);
} catch {
this.logger.warn(
`Could not receive settings from Segment. ${
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/plugins/SegmentDestination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ export class SegmentDestination extends DestinationPlugin {
// Disable all destinations that have a device mode plugin
const deviceModePlugins =
plugins?.map((plugin) => (plugin as DestinationPlugin).key) ?? [];
const cloudSettings: SegmentAPIIntegrations = {
...pluginSettings,
};
for (const key of deviceModePlugins) {
if (key in cloudSettings) {
cloudSettings[key] = false;
const disabledCloudIntegrations: SegmentAPIIntegrations = {};
if (pluginSettings !== undefined) {
for (const key of deviceModePlugins) {
if (key in pluginSettings) {
disabledCloudIntegrations[key] = false;
}
}
}

// User/event defined integrations override the cloud/device mode merge
const mergedEvent = {
...event,
integrations: {
...cloudSettings,
...disabledCloudIntegrations,
...event?.integrations,
},
};
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@ export class Timeline {
this.plugins[type] = [plugin];
}
const settings = plugin.analytics?.settings.get();
if (settings) {
let hasInitialSettings = false;
if (settings !== undefined) {
plugin.update({ integrations: settings }, UpdateType.initial);
hasInitialSettings = true;
}

plugin.analytics?.settings.onChange((newSettings) => {
if (newSettings !== undefined) {
plugin.update(
{ integrations: newSettings },
hasInitialSettings ? UpdateType.refresh : UpdateType.initial
);
hasInitialSettings = true;
}
});
}

remove(plugin: Plugin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ export class AmplitudeSessionPlugin extends EventPlugin {
private insertSession = (event: SegmentEvent) => {
const returnEvent = event;
const integrations = event.integrations;
if (integrations) {
returnEvent.integrations = {
...integrations,
[this.key]: {
session_id: this.sessionId,
},
};
}
returnEvent.integrations = {
...integrations,
[this.key]: {
session_id: this.sessionId,
},
};
return returnEvent;
};

Expand Down

0 comments on commit 9e7d62d

Please sign in to comment.