Skip to content

Commit 66f7683

Browse files
authored
Merge pull request #6406 from segmentio/update-destination-middleware-docs
Update source / destination middleware docs
2 parents 38c8272 + fbe3f17 commit 66f7683

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

src/connections/sources/catalog/libraries/website/javascript/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ Here are some examples of using `addSourceMiddleware` for enrichment and validat
677677
analytics.addSourceMiddleware(({ payload, next }) => {
678678
const { event } = payload.obj.context
679679
if (!isValid(event)) {
680-
throw new Error("Event will be dropped")
680+
return null // event is dropped
681681
}
682682
next(payload)
683683
});

src/connections/sources/catalog/libraries/website/javascript/middleware.md

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,72 @@ The first function (Source Middleware) allows you to manipulate the payload and
2020
2121
## Using Source Middlewares
2222

23-
The function signature for creating Source Middleware has three parameters:
23+
To add source middleware, use the following API:
2424

2525
```js
26-
function({payload, next, integrations}){};
26+
analytics.addSourceMiddleware(({ payload, next, integrations }) => .... )
2727
```
2828

2929
- `payload` represents the event payload sent by Analytics.js. To change the value of the `payload`, mutate the `payload.obj` object. (See the example below.)
3030
- `next` represents the next function to be called in the source middleware chain. If the middleware provided does not call this function, the event is dropped on the client and is not delivered to Segment or any destinations.
3131
- `integrations` is an array of objects representing all the integrations that the payload is sent to. If an integration in this array is set to a ‘falsey' value then the event is not be sent to the Integration.
3232

33+
### Examples
34+
#### Modifying an event
3335
```js
34-
var SMW1 = function({ payload, next, integrations }) {
35-
payload.obj.pageTitle = document.title;
36-
next(payload);
37-
};
36+
analytics.addSourceMiddleware(({ payload, next }) => {
37+
const { event } = payload.obj.context
38+
if (event.type === 'track') {
39+
event.event.toLowerCase()
40+
}
41+
next(payload)
42+
});
43+
```
44+
45+
#### Dropping an event
46+
```js
47+
analytics.addSourceMiddleware(({ payload, next }) => {
48+
const { event } = payload.obj.context
49+
if (!isValid(event)) {
50+
return null // event is dropped
51+
}
52+
next(payload)
53+
});
3854
```
3955

4056
## Using Destination Middlewares
4157

42-
The function signature for creating Destination Middleware also has three parameters:
58+
59+
To add source middleware, use the following API:
4360

4461
```js
45-
function({payload, next, integration}){}
62+
analytics.addDestinationMiddleware('integrationA', ({ payload, next, integrations }) => .... )
4663
```
4764

4865
- `payload` represents the event payload sent by Analytics.js. To change the value of the `payload`, mutate the `payload.obj` object. (See the example below.)
4966
- `next` represents the next function to be called in the destination middleware chain. If the middleware provided does not call this function, then the event is dropped completely for the given destination.
50-
- `integration` is a string value representing the integration that this middleware is applied to.
67+
- `integration` is a string value representing the integration that this middleware is applied to. To apply middleware to all destinations (excluding Segment.io), you can use the `*` value.
5168

69+
#### Example: Modifying an event
5270
```js
53-
var DMW1 = function({ payload, integration, next }) {
71+
analytics.addDestinationMiddleware('integrationA', ({ payload, next, integration }) => {
5472
delete payload.obj.pageTitle;
5573
next(payload);
5674
};
5775
```
5876
77+
#### Example: Dropping an event
78+
```js
79+
analytics.addDestinationMiddleware('integrationA', ({ payload, next, integration }) => {
80+
const { event } = payload.obj.context
81+
if (!isValid(event)) {
82+
return null // event is dropped
83+
}
84+
next(payload)
85+
});
86+
```
87+
88+
5989
> info ""
6090
> **Note**: Destination-middleware only act on [data sent to destinations in device-mode](/docs/connections/destinations#connection-modes). Since the destination middleware code exists in your app or project, it cannot transform the data sent from the Segment servers to the destination endpoint.
6191
@@ -64,8 +94,8 @@ var DMW1 = function({ payload, integration, next }) {
6494
The above defined Source & Destination Middleware can be added to the Analytics.js execution chain as:
6595
6696
```js
67-
analytics.addSourceMiddleware(SMW1);
68-
analytics.addDestinationMiddleware('integrationA', DMW1);
97+
analytics.addSourceMiddleware(() => ...);
98+
analytics.addDestinationMiddleware('integrationA', () => ...);
6999
```
70100
71101

0 commit comments

Comments
 (0)