Skip to content

Commit bc7c421

Browse files
committed
update plugins section
1 parent 68f7a1f commit bc7c421

File tree

1 file changed

+47
-11
lines changed
  • src/connections/sources/catalog/libraries/website/javascript

1 file changed

+47
-11
lines changed

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

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -651,32 +651,68 @@ No, there is no change in behavior to Middlewares.
651651
#### 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?
652652
No, there is no impact to how events filter.
653653

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.
654+
## Plugins and Source Middleware
656655

657-
Though middlewares function the same as plugins, it's best to use plugins as they are easier to implement and are more testable.
656+
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.
658657

658+
### What is the difference between Source Middleware and Plugins, and which should I use?
659+
660+
Plugins and Source Middleware accomplish the same thing, but Plugins are significantly more powerful (but more verbose to implement).
661+
662+
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 segment initialization, you should reach for [Plugins](#plugins-for-advanced-use-cases).
663+
664+
### Source Middleware
665+
666+
[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.
667+
668+
#### Example usage of `addSourceMiddleware`
669+
d
670+
#### Enrichment
671+
```js
672+
analytics.addSourceMiddleware(({ payload, next }) => {
673+
const { event } = payload.obj.context
674+
if (event.type === 'track') {
675+
event.event.toLowerCase()
676+
}
677+
next(payload)
678+
});
679+
```
680+
681+
#### Validation
682+
```js
683+
analytics.addSourceMiddleware(({ payload, next }) => {
684+
const { event } = payload.obj.context
685+
if (!isValid(event)) {
686+
throw new Error("Event will be dropped")
687+
}
688+
next(payload)
689+
});
690+
```
691+
692+
### Plugins (For advanced use-cases)
659693
### Plugin categories
660694
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.
695+
- **Critical Plugins**:
696+
- Errors thrown in `load()` will completely stop segment from initializing.
662697

663698
| Type | Details |
664699
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
665-
| `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. |
700+
| `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 [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.<br><br> Source middleware added via `addSourceMiddleware` is treated as a before plugin. |
666701

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.
702+
* **Non-critical Plugins**:
703+
- This plugin can throw an error on `load()`, and all other segment plugins, including destinations, will load as usual, without blocking the delivery pipeline.
668704

669705
> info ""
670706
> Non-critical plugins are only non-critical from a loading standpoint. For example, if the `before` plugin crashes, this can still halt the event delivery pipeline.
671707
672708
Non-critical plugins run through a timeline that executes in order of insertion based on the entry type. Segment has these four entry types of non-critical plugins:
673709

674710
| Type | Details |
675-
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
676-
| `enrichment` | Executes as the first level of event processing. These plugins modify an event. <br><br> See the example of how Analytics.js uses the [Page Enrichment plugin](https://github.com/segmentio/analytics-next/blob/master/packages/browser/src/plugins/page-enrichment/index.ts){:target="_blank"} to enrich every event with page information. |
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. |
711+
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
712+
| `enrichment` | Executes as the first level of event processing. These plugins modify an event. |
713+
| `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. |
714+
| `after` | Executes after all event processing completes. You can use this to perform cleanup operations. |
715+
| `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. |
680716

681717
### Example plugins
682718
Here's an example of a plugin that converts all track event names to lowercase before the event goes through the rest of the pipeline:

0 commit comments

Comments
 (0)