Skip to content

Commit ea72d84

Browse files
committed
Middleware edits
1 parent b114a97 commit ea72d84

File tree

1 file changed

+18
-19
lines changed
  • src/connections/sources/catalog/libraries/website/javascript

1 file changed

+18
-19
lines changed

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,33 @@ strat: ajs
44
---
55

66

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.
88

9-
Analytics.js can be extended using two functions:
9+
Analytics.js can be extended using 2 functions:
1010

1111
```js
1212
addSourceMiddleware(middleware)
1313
addDestinationMiddleware(targetIntegration, middleware1, middleware2, ...)
1414
```
1515

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.
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.
1717

1818
> 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.
2020
21-
## Using Source Middlewares
21+
## Using source middlewares
2222

2323
To add source middleware, use the following API:
2424

2525
```js
2626
analytics.addSourceMiddleware(({ payload, next, integrations }) => .... )
2727
```
2828

29-
- `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.
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.
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.
3232

33-
### Examples
34-
#### Modifying an event
33+
#### Example: Modifying an event
3534
```js
3635
analytics.addSourceMiddleware(({ payload, next }) => {
3736
const { event } = payload.obj.context
@@ -42,7 +41,7 @@ analytics.addSourceMiddleware(({ payload, next }) => {
4241
});
4342
```
4443

45-
#### Dropping an event
44+
#### Example: Dropping an event
4645
```js
4746
analytics.addSourceMiddleware(({ payload, next }) => {
4847
const { event } = payload.obj.context
@@ -53,7 +52,7 @@ analytics.addSourceMiddleware(({ payload, next }) => {
5352
});
5453
```
5554

56-
## Using Destination Middlewares
55+
## Using destination middlewares
5756

5857

5958
To add destination middleware, use the following API:
@@ -62,7 +61,7 @@ To add destination middleware, use the following API:
6261
analytics.addDestinationMiddleware('integrationA', ({ payload, next, integration }) => .... )
6362
```
6463

65-
- `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.
6665
- `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.
6766
- `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.
6867

@@ -87,29 +86,29 @@ analytics.addDestinationMiddleware('integrationA', ({ payload, next, integration
8786
8887
8988
> info ""
90-
> **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+
> 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.
9190
9291
## Adding middlewares to Analytics.js
9392
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:
9594
9695
```js
9796
analytics.addSourceMiddleware(() => ...);
9897
analytics.addDestinationMiddleware('integrationA', () => ...);
9998
```
10099
101100
102-
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.
103102
104103
Both `.addSourceMiddleware(fn)` and `.addDestinationMiddleware('integration', fn, ...)` can be called before [`.load()`](/docs/connections/sources/catalog/libraries/website/javascript/#load-options).
105104
106-
## Braze Middleware
105+
## Braze middleware
107106
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 Segment by 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.
109108
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.
110109
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.
112111
113112
![BrazeMiddleware](images/sources_ajs_brazemiddleware.png)
114113
115-
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

Comments
 (0)