Skip to content

Commit 6384758

Browse files
authored
Update index.md
1 parent 523268f commit 6384758

File tree

1 file changed

+23
-24
lines changed
  • src/connections/sources/catalog/libraries/website/javascript

1 file changed

+23
-24
lines changed

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -652,41 +652,40 @@ No, there is no change in behavior to Middlewares.
652652
No, there is no impact to how events filter.
653653

654654
## Plugins and source middleware
655-
656655
When you develop against Analytics 2.0, the plugins you write can augment functionality, enrich data, and control the flow and delivery of events. From modifying event payloads to changing analytics functionality, plugins and middleware help to speed up the process of getting things done.
657656

658657
Plugins and source middleware accomplish the same thing, but plugins are significantly more powerful (but more verbose to implement).
659658

660659
For basic use cases like adding event fields or dropping specific events, use [source middleware](#source-middleware). If you need more granular control of the lifecycle, or want to be able to abort the Segment initialization, you should use [plugins](#plugins-for-advanced-use-cases).
661660

662661
### Source Middleware
663-
664662
[Source middleware](/docs/connections/sources/catalog/libraries/website/javascript/middleware/) runs before any other plugins. You can use this to enrich or drop an event.
665663

666664
#### Example usage of `addSourceMiddleware`
667-
#### Enrichment
668-
```js
669-
analytics.addSourceMiddleware(({ payload, next }) => {
670-
const { event } = payload.obj.context
671-
if (event.type === 'track') {
672-
event.event.toLowerCase()
673-
}
674-
next(payload)
675-
});
676-
```
677-
678-
#### Validation
679-
```js
680-
analytics.addSourceMiddleware(({ payload, next }) => {
681-
const { event } = payload.obj.context
682-
if (!isValid(event)) {
683-
throw new Error("Event will be dropped")
684-
}
685-
next(payload)
686-
});
687-
```
665+
Here are some examples of using `addSourceMiddleware` for enrichment and validation.
666+
667+
* Enrichment
668+
```js
669+
analytics.addSourceMiddleware(({ payload, next }) => {
670+
const { event } = payload.obj.context
671+
if (event.type === 'track') {
672+
event.event.toLowerCase()
673+
}
674+
next(payload)
675+
});
676+
```
677+
678+
* Validation
679+
```js
680+
analytics.addSourceMiddleware(({ payload, next }) => {
681+
const { event } = payload.obj.context
682+
if (!isValid(event)) {
683+
throw new Error("Event will be dropped")
684+
}
685+
next(payload)
686+
});
687+
```
688688

689-
### Plugins (For advanced use-cases)
690689
### Plugin categories
691690
Plugins are bound by Analytics 2.0 which handles operations such as observability, retries, and error handling. There are two different categories of plugins:
692691
- **Critical Plugins**:

0 commit comments

Comments
 (0)