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/index.md
+23-24Lines changed: 23 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -652,41 +652,40 @@ No, there is no change in behavior to Middlewares.
652
652
No, there is no impact to how events filter.
653
653
654
654
## Plugins and source middleware
655
-
656
655
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.
657
656
658
657
Plugins and source middleware accomplish the same thing, but plugins are significantly more powerful (but more verbose to implement).
659
658
660
659
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).
661
660
662
661
### Source Middleware
663
-
664
662
[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.
665
663
666
664
#### 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
-
thrownewError("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
+
```
688
688
689
-
### Plugins (For advanced use-cases)
690
689
### Plugin categories
691
690
Plugins are bound by Analytics 2.0 which handles operations such as observability, retries, and error handling. There are two different categories of plugins:
0 commit comments