Skip to content

Commit 450f8f7

Browse files
authored
feat: Inline context in custom events. (#118)
1 parent 8b28a37 commit 450f8f7

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

src/EventProcessor.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const ContextFilter = require('./ContextFilter');
44
const errors = require('./errors');
55
const messages = require('./messages');
66
const utils = require('./utils');
7-
const { getContextKeys } = require('./context');
87

98
function EventProcessor(
109
platform,
@@ -47,27 +46,17 @@ function EventProcessor(
4746
// Transform an event from its internal format to the format we use when sending a payload.
4847
function makeOutputEvent(e) {
4948
const ret = utils.extend({}, e);
50-
if (e.kind === 'identify') {
51-
// identify events always have an inline context
52-
ret.context = contextFilter.filter(e.context);
53-
} else if (e.kind === 'feature') {
54-
// feature events always have an inline context
55-
ret.context = contextFilter.filter(e.context, true);
56-
} else {
57-
ret.contextKeys = getContextKeysFromEvent(e);
58-
delete ret['context'];
59-
}
49+
50+
// This method is used for identify, feature, and custom events, which always have an inline context.
51+
ret.context = contextFilter.filter(e.context);
52+
6053
if (e.kind === 'feature') {
6154
delete ret['trackEvents'];
6255
delete ret['debugEventsUntilDate'];
6356
}
6457
return ret;
6558
}
6659

67-
function getContextKeysFromEvent(event) {
68-
return getContextKeys(event.context, logger);
69-
}
70-
7160
function addToOutbox(event) {
7261
if (queue.length < eventCapacity) {
7362
queue.push(event);

src/__tests__/EventProcessor-test.js

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ describe.each([
5959
}
6060
}
6161

62-
function checkUserInline(e, source, inlineUser) {
63-
if (inlineUser) {
64-
expect(e.context).toEqual(inlineUser);
65-
expect(e.contextKeys).toBeUndefined();
66-
} else {
67-
expect(e.contextKeys).toEqual({ user: source.context.key || source.context.user.key });
68-
expect(e.context).toBeUndefined();
69-
}
70-
}
71-
7262
function checkFeatureEvent(e, source, debug, inlineUser) {
7363
expect(e.kind).toEqual(debug ? 'debug' : 'feature');
7464
expect(e.creationDate).toEqual(source.creationDate);
@@ -87,7 +77,7 @@ describe.each([
8777
expect(e.key).toEqual(source.key);
8878
expect(e.data).toEqual(source.data);
8979
expect(e.metricValue).toEqual(source.metricValue);
90-
checkUserInline(e, source);
80+
expect(e.context).toEqual(source.context);
9181
}
9282

9383
function checkSummaryEvent(e) {
@@ -219,6 +209,31 @@ describe.each([
219209
});
220210
});
221211

212+
it('filters context in feature event', async () => {
213+
const config = { ...defaultConfig, allAttributesPrivate: true };
214+
await withProcessorAndSender(config, async (ep, mockEventSender) => {
215+
const e = {
216+
kind: 'feature',
217+
creationDate: 1000,
218+
context: eventContext,
219+
key: 'flagkey',
220+
version: 11,
221+
variation: 1,
222+
value: 'value',
223+
default: 'default',
224+
trackEvents: true,
225+
};
226+
ep.enqueue(e);
227+
await ep.flush();
228+
229+
expect(mockEventSender.calls.length()).toEqual(1);
230+
const output = (await mockEventSender.calls.take()).events;
231+
expect(output.length).toEqual(2);
232+
checkFeatureEvent(output[0], e, false, filteredContext);
233+
checkSummaryEvent(output[1]);
234+
});
235+
});
236+
222237
it('can both track and debug an event', async () => {
223238
await withProcessorAndSender(defaultConfig, async (ep, mockEventSender) => {
224239
const futureTime = new Date().getTime() + 1000000;
@@ -380,6 +395,27 @@ describe.each([
380395
});
381396
});
382397

398+
it('filters context in custom event', async () => {
399+
const config = { ...defaultConfig, allAttributesPrivate: true };
400+
await withProcessorAndSender(config, async (ep, mockEventSender) => {
401+
const e = {
402+
kind: 'custom',
403+
creationDate: 1000,
404+
context: eventContext,
405+
key: 'eventkey',
406+
data: { thing: 'stuff' },
407+
metricValue: 1.5,
408+
};
409+
ep.enqueue(e);
410+
await ep.flush();
411+
412+
expect(mockEventSender.calls.length()).toEqual(1);
413+
const output = (await mockEventSender.calls.take()).events;
414+
expect(output.length).toEqual(1);
415+
checkCustomEvent(output[0], { ...e, context: filteredContext });
416+
});
417+
});
418+
383419
it('enforces event capacity', async () => {
384420
const config = { ...defaultConfig, eventCapacity: 1, logger: stubPlatform.logger() };
385421
const e0 = { kind: 'custom', creationDate: 1000, context: eventContext, key: 'key0' };

0 commit comments

Comments
 (0)