You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/connections/sources/catalog/libraries/website/javascript/middleware.md
+18-19Lines changed: 18 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -4,34 +4,33 @@ strat: ajs
4
4
---
5
5
6
6
7
-
Middlewares allow developers to extend Analytics.js with custom code which runs on every event. This code has full access to the DOM and Browser API, and helps customers enrich and transform event payloads. Source Middlewares and Destination Middlewares are available on the Analytics.js snippet version `4.12.0` and later.
7
+
Middlewares allow developers to extend Analytics.js with custom code that runs on every event. This code has full access to the DOM and Browser API, and helps customers enrich and transform event payloads. Source and destination middlewares are available on the Analytics.js snippet version `4.12.0` and later.
The first function (Source Middleware) allows you to manipulate the payload and filter events on a per-source basis, while the second function (Destination Middleware) allows this on a per destination basis. Middlewares run in the browser.
16
+
The first function (source middleware) allows you to manipulate the payload and filter events on a per-source basis, while the second function (destination middleware) allows this on a per destination basis. Middlewares run in the browser.
17
17
18
18
> info ""
19
-
> **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.
19
+
> 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.
-`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.)
29
+
-`payload` represents the event payload sent by Analytics.js. To change the value of the `payload`, mutate the `payload.obj` object, as in the example below.
30
30
-`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.
31
-
-`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.
31
+
-`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 falsy value, then the event is not sent to the integration.
32
32
33
-
### Examples
34
-
#### Modifying an event
33
+
#### Example: Modifying an event
35
34
```js
36
35
analytics.addSourceMiddleware(({ payload, next }) => {
37
36
const { event } =payload.obj.context
@@ -42,7 +41,7 @@ analytics.addSourceMiddleware(({ payload, next }) => {
42
41
});
43
42
```
44
43
45
-
#### Dropping an event
44
+
#### Example: Dropping an event
46
45
```js
47
46
analytics.addSourceMiddleware(({ payload, next }) => {
48
47
const { event } =payload.obj.context
@@ -53,7 +52,7 @@ analytics.addSourceMiddleware(({ payload, next }) => {
53
52
});
54
53
```
55
54
56
-
## Using Destination Middlewares
55
+
## Using destination middlewares
57
56
58
57
59
58
To add destination middleware, use the following API:
@@ -62,7 +61,7 @@ To add destination middleware, use the following API:
-`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.)
64
+
-`payload` represents the event payload sent by Analytics.js. To change the value of the `payload`, mutate the `payload.obj` object, as in the example below.
66
65
-`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.
67
66
-`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.
> **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.
89
+
> Destinationmiddleware 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.
91
90
92
91
## Adding middlewares to Analytics.js
93
92
94
-
The above defined Source & Destination Middleware can be added to the Analytics.js execution chain as:
93
+
The above defined source and destination middleware can be added to the Analytics.js execution chain as:
You can call the `.addSourceMiddleware(fn)` multiple times, and the order of operations reflects the order in which you register your Source Middleware.
101
+
You can call the `.addSourceMiddleware(fn)` multiple times, and the order of operations reflects the order in which you register your source middleware.
103
102
104
103
Both `.addSourceMiddleware(fn)` and `.addDestinationMiddleware('integration', fn, ...)` can be called before [`.load()`](/docs/connections/sources/catalog/libraries/website/javascript/#load-options).
105
104
106
-
## Braze Middleware
105
+
## Braze middleware
107
106
108
-
If you use the Braze (Appboy) destination in either [cloud or device mode](/docs/connections/destinations/#connection-modes) you can save Braze costs by "debouncing" duplicate `identify()` calls from Segmentby adding our [open-source Middleware tool](https://github.com/segmentio/segment-braze-mobile-middleware) to your implementation.
107
+
If you use the Braze (Appboy) destination in either [cloud or device mode](/docs/connections/destinations/#connection-modes) you can reduce Braze costs by debouncing duplicate Identify calls from Segment. You can achieve this by adding Segment's [open-source middleware tool](https://github.com/segmentio/segment-braze-mobile-middleware){:target="_blank"} to your implementation.
109
108
This optional middleware is disabled by default. When enabled, it ensures that only events where at least one changed trait value are sent to Braze, and events with duplicate traits are not sent.
110
109
111
-
To enable this Middleware for a JavaScript or Project source, go to `Analytics.js` in your source settings.
110
+
To enable this middleware for a JavaScript or Project source, go to **Analytics.js** in your source settings.
More information about this tool and how it works [is available in the project's README](https://github.com/segmentio/segment-braze-mobile-middleware/blob/master/README.md#how-does-this-work).
114
+
More information about this tool and how it works is available in the project's [README](https://github.com/segmentio/segment-braze-mobile-middleware/blob/master/README.md#how-does-this-work){:target="_blank"}.
0 commit comments