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
+45-13Lines changed: 45 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -651,32 +651,64 @@ No, there is no change in behavior to Middlewares.
651
651
#### When using Segment features (Schema filtering, integrations object, Protocols) to filter events from going to destinations (device and cloud-mode), will batching impact the filtering of events?
652
652
No, there is no impact to how events filter.
653
653
654
-
## Plugin architecture
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 help to speed up the process of getting things done.
656
-
657
-
Though middlewares function the same as plugins, it's best to use plugins as they are easier to implement and are more testable.
654
+
## Plugins and source middleware
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.
656
+
657
+
Plugins and source middleware accomplish the same thing, but plugins are significantly more powerful (but more verbose to implement).
658
+
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).
660
+
661
+
### Source Middleware
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.
663
+
664
+
#### Example usage of `addSourceMiddleware`
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
+
```
658
688
659
689
### Plugin categories
660
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:
661
-
***Critical Plugins**: Analytics.js expects this plugin to be loaded before starting event delivery. Failure to load a critical plugin halts event delivery. Use this category sparingly, and only for plugins that are critical to your tracking.
691
+
***Critical Plugins**:
692
+
- Errors thrown in`load()` will completely stop segment from initializing.
|`before`| Executes before event processing begins. These are plugins that run before any other plugins run. <br><br>For example, validating events before passing them along to other plugins. A failure here could halt the event pipeline. <br><br> See the example of how Analytics.js uses the [Event Validation plugin](https://github.com/segmentio/analytics-next/blob/master/packages/browser/src/plugins/validation/index.ts){:target="_blank"} to verify that every event has the correct shape. |
696
+
|`before`| Executes before event processing begins. These are plugins that run before any other plugins run. <br><br>See the example of how Analytics.js uses the [EventValidationplugin](https://github.com/segmentio/analytics-next/blob/master/packages/browser/src/plugins/validation/index.ts){:target="_blank"} to verify that every event has the correct shape.<br><br> Source middleware added via `addSourceMiddleware` is treated as a before plugin. |
666
697
667
-
***Non-critical Plugins**: Analytics.js can start event delivery before this plugin finishes loading. This means your plugin can fail to load independently from all other plugins. For example, every Analytics.js destination is a non-critical plugin. This makes it possible for Analytics.js to continue working if a partner destination fails to load, or if users have ad blockers turned on that are targeting specific destinations.
|`enrichment`| Executes as the first level of event processing. These plugins modify an event. |
677
-
|`destination`| Executes as events begin to pass off to destinations. <br><br> This doesn't modify the event outside of the specific destination, and failure doesn't halt the execution. |
678
-
|`after`| Executes after all event processing completes. You can use this to perform cleanup operations. <br><br>An example of this is the [Segment.io Plugin](https://github.com/segmentio/analytics-next/blob/master/packages/browser/src/plugins/segmentio/index.ts){:target="_blank"} which waits for destinations to succeed or fail so it can send it observability metrics. |
679
-
|`utility`| Executes once during the bootstrap, to give you an outlet to make any modifications as to how Analytics.js works internally. This allows you to augment Analytics.js functionality. |
|`destination`|Executesaseventsbegintopassofftodestinations. <br><br>Thisdoesn't modify the event outside of the specific destination, and failure doesn'thalttheexecution. |
0 commit comments