Skip to content

Commit

Permalink
Merge pull request #34 from creately/ft-exclude-event-parameters-from…
Browse files Browse the repository at this point in the history
…-particular-event

Exclude event parameters from particular event
  • Loading branch information
samalw authored Apr 7, 2022
2 parents 6fe76f9 + c577055 commit 21bfaed
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ This is optional, default value is `allow: [ '*' ]`. If no value is specified, e

This is optional, default value is `deny: []`. If no value is specified, this doesn't affect anything but the decision to send the events will be based on the evaluation of other options provided. If both allow and deny options exist, evaluation of the deny options will determine if the events will be sent to the consumer of not.

* denyParameters
This is an optional parameter. You can use denyParameters to exclude sending particular event parameter for an event. Specify an array of objects. Each object should have eventId and parameters as an array.

```
{ name: 'consumer' } // Send all events to the consumer
{ name: 'consumer', allow: [] } // No events will be sent to the consumer
{ name: 'consumer', allow: [ '*' ] } // Send all events to the consumer
{ name: 'consumer', allow: [ 'user.login.' ] } // All events matching user.login.* will be sent to the consumer
{ name: 'consumer', deny: [ '*' ] } // No events will be sent to the consumer
{ name: 'consumer', allow: [ 'user.login.' ], deny: [ 'user.login.failed' ] } // Send all events which matches the namespace user.login.* but don't send user.login.failed event.
{ name: 'consumer', allow: [ '*' ], denyParameters: [ { eventId: 'user.login.failed', parameters: [ 'location' ] } ] } // Exclude sending location parameter with user.login.failed event to the consumer.
```

Configuration allows us to configure the consumers whatever the way we want, it up to you to decide.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@creately/siddi",
"version": "1.0.9",
"version": "1.0.10",
"description": "A convenient event tracker API which sends events to multiple consumers",
"main": "dist/siddi.js",
"scripts": {
Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/siddi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,21 @@ describe('Siddi', () => {
siddi.track('unitTest.track', { run: 'last' });
expect(mixpanelTrackSpy).toBeCalledWith('unitTest.track', { run: 'last' });
});
it('should not send event parameters with tracking data when that event parameters defined in denyParameters', () => {
mixpanelTestSpy.mockReturnValue(true);
siddi = new Siddi([{ name: 'mixpanel', allow: ['*'], denyParameters: [{eventId:'unitTest.track', parameters:['where', 'why']}] }]);
siddi.track('unitTest.track', { run: 'last', where: 'mac', why: 'denny' });
expect(mixpanelTestSpy).toBeCalled();
expect(mixpanelIdentifySpy).not.toBeCalled();
expect(mixpanelTrackSpy).toBeCalledWith('unitTest.track', { run: 'last' });
});
it('should only exclude event parameters which are defined in denyParameters', () => {
mixpanelTestSpy.mockReturnValue(true);
siddi = new Siddi([{ name: 'mixpanel', allow: ['*'], denyParameters: [{eventId:'unitTest.track', parameters:['param']}] }]);
siddi.track('unitTest.run', { run: 'last', param: 'value' });
expect(mixpanelTestSpy).toBeCalled();
expect(mixpanelIdentifySpy).not.toBeCalled();
expect(mixpanelTrackSpy).toBeCalledWith('unitTest.run', { run: 'last', param: 'value' });
});
});
});
19 changes: 17 additions & 2 deletions src/siddi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import { Consumers } from './consumers';
* 4. { name: 'mixpanel', allow: [ 'site.' ] } --> All site events will be sent
* 5. { name: 'mixpanel', allow: [ '*' ], deny: [ 'site.login.', 'app.user.' ] } --> Do not send site.login.* and app.user.* events
* 6. { name: 'mixpanel', deny: [ 'user.collab.' ] } --> Do not send user.collab.* events
* 7. { name: 'mixpanel', denyParameters: [ {eventId: 'user.login.failed', parameters: ['location'] } ] } --> Do not send location parameter with user.login.failed event
*/
export type EventConfiguration = { name: string; allow?: string[]; deny?: string[] };
export type EventConfiguration = { name: string; allow?: string[]; deny?: string[]; denyParameters?: any };

/**
* Siddi - An abstract event consumer
Expand Down Expand Up @@ -68,6 +69,8 @@ export class Siddi {
*/
public track(eventName: string, eventProperties: any): void {
this.consumerConfig.forEach(config => {
// Assign event properties to a new obeject
let filteredEventProperties = Object.assign({}, eventProperties);
// consumer name must exist, else ignore it
if (config.name && Consumers[config.name] && this.shouldTrack(config, eventName)) {
// If no consumer status tracking exist, check it first
Expand All @@ -85,6 +88,18 @@ export class Siddi {
}
}

// Exclude sending event parameters for particular event
// when those defined in denyParameters config
if(eventProperties && config.denyParameters) {
config.denyParameters.some(function(element: any, index: number) {
if(element.eventId === eventName) {
config.denyParameters[index].parameters.forEach(function(property: string) {
delete filteredEventProperties[property];
});
}
});
}

// Track the event only if consumer is in enabled status
// We do not send tracking data to consumers knowingly that they would fail
if (this.consumerStatus[config.name].enabled) {
Expand All @@ -94,7 +109,7 @@ export class Siddi {
this.consumerStatus[config.name].identified = true;
}
new Promise(resolve => {
Consumers[config.name].track(eventName, eventProperties);
Consumers[config.name].track(eventName, filteredEventProperties);
resolve();
});
}
Expand Down

0 comments on commit 21bfaed

Please sign in to comment.