Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion packages/core/src/__tests__/__helpers__/mockSegmentStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SEGMENT_DESTINATION_KEY } from '../../plugins/SegmentDestination';
import type { DeepLinkData, Storage } from '../../storage';
import type {
Context,
Expand All @@ -21,7 +22,9 @@ const INITIAL_VALUES: Data = {
isReady: true,
events: [],
context: undefined,
settings: {},
settings: {
[SEGMENT_DESTINATION_KEY]: {},
},
userInfo: {
anonymousId: 'anonymousId',
userId: undefined,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/__tests__/internal/fetchSettings.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { getMockLogger } from '../__helpers__/mockLogger';
import { SegmentClient } from '../../analytics';
import { MockSegmentStore } from '../__helpers__/mockSegmentStore';
import { SEGMENT_DESTINATION_KEY } from '../../plugins/SegmentDestination';

describe('internal #getSettings', () => {
const defaultIntegrationSettings = {
integrations: {},
integrations: {
// This one is injected by the mock
[SEGMENT_DESTINATION_KEY]: {},
},
};
const store = new MockSegmentStore();

Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ export class DestinationPlugin extends EventPlugin {

timeline = new Timeline();

private hasSettings() {
return this.analytics?.settings.get()?.[this.key] !== undefined;
}

private isEnabled(event: SegmentEvent): boolean {
let customerDisabled = false;
if (event.integrations?.[this.key] === false) {
customerDisabled = true;
}

return this.hasSettings() && !customerDisabled;
}

/**
Adds a new plugin to the currently loaded set.

Expand Down Expand Up @@ -141,6 +154,10 @@ export class DestinationPlugin extends EventPlugin {
}

execute(event: SegmentEvent): SegmentEvent | undefined {
if (!this.isEnabled(event)) {
return undefined;
}

// Apply before and enrichment plugins
const beforeResult = this.timeline.applyPlugins({
type: PluginType.before,
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/plugins/__tests__/SegmentDestination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('SegmentDestination', () => {
firebase: {
someConfig: 'someValue',
},
[SEGMENT_DESTINATION_KEY]: {},
},
}),
});
Expand Down Expand Up @@ -140,6 +141,7 @@ describe('SegmentDestination', () => {
firebase: {
someConfig: 'someValue',
},
[SEGMENT_DESTINATION_KEY]: {},
},
}),
});
Expand Down Expand Up @@ -209,4 +211,33 @@ describe('SegmentDestination', () => {
})),
});
});

it('lets plugins/events disable destinations individually', () => {
const plugin = new SegmentDestination();
// @ts-ignore
plugin.analytics = new SegmentClient({
...clientArgs,
store: new MockSegmentStore({
settings: {
[SEGMENT_DESTINATION_KEY]: {},
},
}),
});

const event: TrackEventType = {
anonymousId: '3534a492-e975-4efa-a18b-3c70c562fec2',
event: 'Awesome event',
type: EventType.TrackEvent,
properties: {},
timestamp: '2000-01-01T00:00:00.000Z',
messageId: '1d1744bf-5beb-41ac-ad7a-943eac33babc',
context: { app: { name: 'TestApp' } },
integrations: {
[SEGMENT_DESTINATION_KEY]: false,
},
};

const result = plugin.execute(event);
expect(result).toEqual(undefined);
});
});