Skip to content

Commit 3c89230

Browse files
committed
1 parent abc7cdd commit 3c89230

File tree

1 file changed

+101
-1
lines changed
  • src/connections/sources/catalog/libraries/website/javascript

1 file changed

+101
-1
lines changed

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

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,107 @@ No, there is no change in behavior to Middlewares.
721721
#### 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?
722722
No, there is no impact to how events filter.
723723

724-
## Plugins
724+
## Plugin Architecture
725+
When developing 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.
726+
727+
### Plugin Categories
728+
Plugins are bound by Analytics 2.0 which handles operations such as observability, retries, and error handling. There are two different categories of plugins:
729+
1. **Critical**: 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.
730+
2. **Non-Critical**: 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 in case a partner destination fails to load, or in case users have ad blockers turned on that are targeting specific destinations.
731+
732+
> info ""
733+
> Non-critical plugins are only non-critical from a loading standpoint. A crash in a before plugin can still halt the event delivery pipeline.
734+
735+
Non-critical plugins run through a timeline that execute in order of insertion based on the entry type. Segment has these five entry types of non-critical plugins:
736+
737+
Type | Details
738+
---- | -------
739+
`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/src/plugins/validation/index.ts) to verify that every event has the correct shape.
740+
`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/src/plugins/page-enrichment/index.ts) to enrich every event with page information.
741+
`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.
742+
`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/src/plugins/segmentio/index.ts) which waits for destinations to succeed or fail so it can send it observability metrics.
743+
`utility` | Executes only with manual calls such as Logging. <br><br>These are plugins that change the Analytics 2.0 functionality. Utility plugins run outside of the event pipeline, and only execute once.
744+
745+
### Example Plugins
746+
Here's an example of a plugin that converts all track event names to lowercase before the event gets sent through the rest of the pipeline:
747+
748+
```js
749+
export const lowercase: Plugin = {
750+
name: 'Lowercase events',
751+
type: 'enrichment',
752+
version: '1.0.0',
753+
754+
isLoaded: () => true,
755+
load: () => Promise.resolve(),
756+
757+
track: (ctx) => {
758+
ctx.updateEvent('event', ctx.event.event.toLowerCase())
759+
return ctx
760+
}
761+
}
762+
763+
const identityStitching = () => {
764+
let user
765+
766+
const identity = {
767+
// Identifies your plugin in the Plugins stack.
768+
// You can see the full list by accessing: `window.analytics.queue.plugins`
769+
name: 'Identity Stitching',
770+
// Defines where in the event timeline a plugin should run
771+
type: 'enrichment',
772+
version: '0.1.0',
773+
774+
// use the `load` hook to bootstrap your plugin
775+
// The load hook will receive a context object as its first argument
776+
// followed by a reference to the analytics.js instance from the page
777+
load: async (_ctx, ajs) => {
778+
user = ajs.user()
779+
},
780+
781+
// Used to signal that a plugin has been property loaded
782+
isLoaded: () => user !== undefined,
783+
784+
// Applies the plugin code to every `identify` call in Analytics.js
785+
// You can override any of the existing types in the Segment Spec.
786+
async identify(ctx) {
787+
// Request some extra info to enrich your `identify` events from
788+
// an external API.
789+
const req = await fetch(
790+
`https://jsonplaceholder.typicode.com/users/${ctx.event.userId}`
791+
)
792+
const userReq = await req.json()
793+
794+
// ctx.updateEvent can be used to update deeply nested properties
795+
// in your events. It's a safe way to change events as it'll
796+
// create any missing objects and properties you may require.
797+
ctx.updateEvent('traits.custom', userReq)
798+
user.traits(userReq)
799+
800+
// Every plugin must return a `ctx` object, so that the event
801+
// timeline can continue processing.
802+
return ctx
803+
},
804+
}
805+
806+
return identity
807+
}
808+
809+
// Registers our new plugin into Analytics.js
810+
await window.analytics.register(identityStitching())
811+
```
812+
813+
You can view Segment's [existing plugins](https://github.com/segmentio/analytics-next/tree/master/src/plugins) to see more examples.
814+
815+
### Registering a plugin
816+
Registering plugins enable you to modify your analytics implementation to best fit your needs. You can register a plugin using this:
817+
818+
```js
819+
// A promise will resolve once the plugins has been successfully loaded into Analytics.js
820+
// You can register multiple plugins at once by using the variable args interface in Analytis.js
821+
await window.analytics.register(pluginA, pluginB, pluginN)
822+
```
823+
824+
## Video Player Plugins
725825

726826
Segment offers video player 'plugins' so you can quickly collect video events using Analytics.js. See the specific documentation below to learn more:
727827

0 commit comments

Comments
 (0)