Skip to content

Commit d97797f

Browse files
Add a filter function to plugins to filter out events so that they are not tracked (#1326)
1 parent fed8834 commit d97797f

File tree

15 files changed

+225
-65
lines changed

15 files changed

+225
-65
lines changed

api-docs/docs/browser-tracker/markdown/browser-tracker.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
| [enableAnonymousTracking(configuration, trackers)](./browser-tracker.enableanonymoustracking.md) | Enables anonymous tracking (ie. tracker initialized without <code>anonymousTracking</code>) |
2424
| [flushBuffer(configuration, trackers)](./browser-tracker.flushbuffer.md) | Send all events in the outQueue Only need to use this when sending events with a bufferSize of at least 2 |
2525
| [newSession(trackers)](./browser-tracker.newsession.md) | Expires current session and starts a new session. |
26-
| [newTracker(trackerId, endpoint)](./browser-tracker.newtracker.md) | Initialise a new tracker |
27-
| [newTracker(trackerId, endpoint, configuration)](./browser-tracker.newtracker_1.md) | Initialise a new tracker |
26+
| [newTracker(trackerId, endpoint, configuration)](./browser-tracker.newtracker.md) | Initialise a new tracker |
2827
| [preservePageViewId(trackers)](./browser-tracker.preservepageviewid.md) | Stop regenerating <code>pageViewId</code> (available from <code>web_page</code> context) |
2928
| [removeGlobalContexts(contexts, trackers)](./browser-tracker.removeglobalcontexts.md) | All provided contexts will no longer be sent with every event |
3029
| [setBufferSize(newBufferSize, trackers)](./browser-tracker.setbuffersize.md) | Set the buffer size Can be useful if you want to stop batching requests to ensure events start sending closer to event creation |

api-docs/docs/browser-tracker/markdown/browser-tracker.newtracker.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Initialise a new tracker
99
<b>Signature:</b>
1010

1111
```typescript
12-
declare function newTracker(trackerId: string, endpoint: string): BrowserTracker;
12+
declare function newTracker(trackerId: string, endpoint: string, configuration?: TrackerConfiguration): BrowserTracker | null | undefined;
1313
```
1414

1515
## Parameters
@@ -18,8 +18,9 @@ declare function newTracker(trackerId: string, endpoint: string): BrowserTracker
1818
| --- | --- | --- |
1919
| trackerId | string | The tracker id - also known as tracker namespace |
2020
| endpoint | string | Collector endpoint in the form collector.mysite.com |
21+
| configuration | TrackerConfiguration | The initialisation options of the tracker |
2122

2223
<b>Returns:</b>
2324

24-
BrowserTracker
25+
BrowserTracker \| null \| undefined
2526

api-docs/docs/browser-tracker/markdown/browser-tracker.newtracker_1.md

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@snowplow/node-tracker](./node-tracker.md) &gt; [CorePlugin](./node-tracker.coreplugin.md) &gt; [filter](./node-tracker.coreplugin.filter.md)
4+
5+
## CorePlugin.filter property
6+
7+
Called before the payload is sent to the callback to decide whether to send the payload or skip it
8+
9+
<b>Signature:</b>
10+
11+
```typescript
12+
filter?: (payload: Payload) => boolean;
13+
```

api-docs/docs/node-tracker/markdown/node-tracker.coreplugin.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ interface CorePlugin
2020
| [afterTrack?](./node-tracker.coreplugin.aftertrack.md) | (payload: Payload) =&gt; void | <i>(Optional)</i> Called just after the trackerCore callback fires |
2121
| [beforeTrack?](./node-tracker.coreplugin.beforetrack.md) | (payloadBuilder: PayloadBuilder) =&gt; void | <i>(Optional)</i> Called just before the trackerCore callback fires |
2222
| [contexts?](./node-tracker.coreplugin.contexts.md) | () =&gt; SelfDescribingJson\[\] | <i>(Optional)</i> Called when constructing the context for each event Useful for adding additional context to events |
23+
| [filter?](./node-tracker.coreplugin.filter.md) | (payload: Payload) =&gt; boolean | <i>(Optional)</i> Called before the payload is sent to the callback to decide whether to send the payload or skip it |
2324
| [logger?](./node-tracker.coreplugin.logger.md) | (logger: Logger) =&gt; void | <i>(Optional)</i> Passed a logger instance which can be used to send log information to the active logger |
2425

api-docs/docs/node-tracker/node-tracker.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export interface CorePlugin {
179179
afterTrack?: (payload: Payload) => void;
180180
beforeTrack?: (payloadBuilder: PayloadBuilder) => void;
181181
contexts?: () => SelfDescribingJson[];
182+
filter?: (payload: Payload) => boolean;
182183
// Warning: (ae-forgotten-export) The symbol "Logger" needs to be exported by the entry point index.module.d.ts
183184
logger?: (logger: Logger) => void;
184185
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker-core",
5+
"comment": "Add a filter function to plugins to filter out events so that they are not tracked (#1326)",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker-core"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker",
5+
"comment": "Add a filter function to plugins to filter out events so that they are not tracked (#1326)",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/javascript-tracker",
5+
"comment": "Add a filter function to plugins to filter out events so that they are not tracked (#1326)",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/javascript-tracker"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/node-tracker",
5+
"comment": "Add a filter function to plugins to filter out events so that they are not tracked (#1326)",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/node-tracker"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/tracker-core",
5+
"comment": "Add a filter function to plugins to filter out events so that they are not tracked (#1326)",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/tracker-core"
10+
}

libraries/tracker-core/src/core.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export interface TrackerCore {
138138
* @param pb - Payload
139139
* @param context - Custom contexts relating to the event
140140
* @param timestamp - Timestamp of the event
141-
* @returns Payload after the callback is applied
141+
* @returns Payload after the callback is applied or undefined if the event is skipped
142142
*/
143143
track: (
144144
/** A PayloadBuilder created by one of the `buildX` functions */
@@ -147,7 +147,7 @@ export interface TrackerCore {
147147
context?: Array<SelfDescribingJson> | null,
148148
/** Timestamp override */
149149
timestamp?: Timestamp | null
150-
) => Payload;
150+
) => Payload | undefined;
151151

152152
/**
153153
* Set a persistent key-value pair to be added to every payload
@@ -376,13 +376,13 @@ export function trackerCore(configuration: CoreConfiguration = {}): TrackerCore
376376
* @param pb - Payload
377377
* @param context - Custom contexts relating to the event
378378
* @param timestamp - Timestamp of the event
379-
* @returns Payload after the callback is applied
379+
* @returns Payload after the callback is applied or undefined if the event is skipped
380380
*/
381381
function track(
382382
pb: PayloadBuilder,
383383
context?: Array<SelfDescribingJson> | null,
384384
timestamp?: Timestamp | null
385-
): Payload {
385+
): Payload | undefined {
386386
pb.withJsonProcessor(payloadJsonProcessor(encodeBase64));
387387
pb.add('eid', uuid());
388388
pb.addDict(payloadPairs);
@@ -404,6 +404,19 @@ export function trackerCore(configuration: CoreConfiguration = {}): TrackerCore
404404
}
405405
});
406406

407+
// Call the filter on plugins to determine if the event should be tracked
408+
const skip = corePlugins.find((plugin) => {
409+
try {
410+
return plugin.filter && plugin.filter(pb.build()) === false;
411+
} catch (ex) {
412+
LOG.error('Plugin filter', ex);
413+
return false;
414+
}
415+
});
416+
if (skip) {
417+
return undefined;
418+
}
419+
407420
if (typeof callback === 'function') {
408421
callback(pb);
409422
}

libraries/tracker-core/src/plugins.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface CorePlugin {
4444
*/
4545
activateCorePlugin?: (core: TrackerCore) => void;
4646
/**
47-
* Called just before the trackerCore callback fires
47+
* Called before the `filter` method is called and before the trackerCore callback fires (if the filter passes)
4848
* @param payloadBuilder - The payloadBuilder which will be sent to the callback, can be modified
4949
*/
5050
beforeTrack?: (payloadBuilder: PayloadBuilder) => void;
@@ -53,6 +53,12 @@ export interface CorePlugin {
5353
* @param payload - The final built payload
5454
*/
5555
afterTrack?: (payload: Payload) => void;
56+
/**
57+
* Called before the payload is sent to the callback to decide whether to send the payload or skip it
58+
* @param payload - The final event payload, can't be modified.
59+
* @returns True if the payload should be sent, false if it should be skipped
60+
*/
61+
filter?: (payload: Payload) => boolean;
5662
/**
5763
* Called when constructing the context for each event
5864
* Useful for adding additional context to events

0 commit comments

Comments
 (0)