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
|`http_request`| Emitted when an HTTP request is made. |
483
+
|`register`| Emitted when a plugin is registered
483
484
|`call_after_close`| Emitted when an event is received after the flush with `{ close: true }`. |
484
485
485
486
These emitters allow you to hook into various stages of the event lifecycle and handle them accordingly.
486
487
487
488
488
489
## Plugin architecture
489
-
When you develop in [Analytics.js 2.0](/docs/connections/sources/catalog/libraries/website/javascript/), the plugins you write can improve 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.
490
+
The plugins you write can improve 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.
490
491
491
-
Though middlewares functionthe same as plugins, it's best to use plugins as they are easier to implement and are more testable.
492
492
493
493
### Plugin categories
494
494
495
495
| Type | Details
496
496
| ------------- | ------------- |
497
-
| `before` | Executes before event processing begins. These are plugins that run before any other plugins run. Thrown errors here can block the event pipeline. Source middleware added via `addSourceMiddleware` is treated as a `before` plugin. |
498
-
| `enrichment` | Executes as the first level of event processing. These plugins modify an event. Thrown errors here can block the event pipeline. |
497
+
|`before`| Executes before event processing begins. These are plugins that run before any other plugins run. Thrown errors here can block the event pipeline. Source middleware added via `addSourceMiddleware` is treated as a `before` plugin. No events will be sent to destinations until`.load()` method is resolved. |
498
+
|`enrichment`| Executes as the first level of event processing. These plugins modify an event. Thrown errors here can block the event pipeline. No events will be sent to destinations until`.load()` method is resolved. |
499
499
|`destination`| Executes as events begin to pass off to destinations. Segment.io is implemented as a destination plugin. Thrown errors here will _not_ block the event pipeline. |
500
500
|`after`| Executes after all event processing completes. You can use this to perform cleanup operations. |
501
501
|`utility`| Executes _only once_ during the bootstrap. Gives you access to the analytics instance via the plugin's `load()` method. This doesn't allow you to modify events. |
502
502
503
-
### Example plugins
503
+
### Example plugin
504
504
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:
// ctx.updateEvent can be used to update deeply nested properties
546
-
// in your events. It's a safe way to change events as it'll
547
-
// create any missing objects and properties you may require.
548
-
ctx.updateEvent('traits.custom', userReq)
549
-
user.traits(userReq)
550
-
551
-
// Every plugin must return a `ctx` object, so that the event
552
-
// timeline can continue processing.
553
-
return ctx
554
-
},
555
-
}
556
-
557
-
return identity
558
-
}
559
520
```
560
521
561
-
You can view Segment's [existing plugins](https://github.com/segmentio/analytics-next/tree/master/packages/browser/src/plugins){:target="_blank"} to see more examples.
562
-
563
522
### Register a plugin
564
523
Registering plugins enable you to modify your analytics implementation to best fit your needs. You can register a plugin using this:
0 commit comments