Skip to content

Commit 58341c1

Browse files
committed
wip
1 parent 16b8380 commit 58341c1

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

packages/browser/architecture/ARCHITECTURE.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,40 @@ graph TD
4242
### Plugin Types Explanation
4343
[This information is also available in the Segment documentation](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#plugins-and-source-middleware)
4444

45-
1. **Before Plugins** (see [Example](#example-plugin-implementation))
45+
- **Source Middleware** (see [Example](#example-source-middleware-implementation))
46+
- **Source Middleware is just a light API wrapper around a "Before" type plugin Plugin**
47+
- Source Middleware is the legacy API (pre-analytics next). It's less verbose than the full plugin API, but a bit less powerful. It is functionally equivalent to a "Before" type plugin.
48+
49+
- **Before Plugins** (see [Example](#example-plugin-implementation))
4650
- Run before any other plugins
4751
- Critical priority - block event pipeline until `.load()` resolves
4852
- Use cases: Event validation, data transformation
4953
- Example: Event validation before passing to other plugins)
50-
51-
A. **Source Middleware** (see [Example](#example-source-middleware-implementation))
52-
- **Source Middleware is just a light API wrapper around a "Before" type plugin Plugin**
53-
- Source Middleware is the legacy API (pre-analytics next). It's less verbose than the full plugin API, but a bit less powerful. It is functionally equivalent to a "Before" type plugin.
5454

55-
2. **Enrichment Plugins**
55+
56+
- **Enrichment Plugins**
5657
- Functionally Identitical to "before" plugins, but run after them. Before plugins are typically used internally (e.g adding page info), but there's no hard and fast rule.
5758

58-
3. **Destination Plugins**
59+
- **Destination Plugins**
5960
- Run after enrichment
6061
- Cannot modify the event
6162
- Execute in parallel
6263
- Failures do not halt pipeline
6364
- Example: Segment.io, Google Analytics, Mixpanel
6465

65-
4. **After Plugins (uncommon)**
66+
- **After Plugins (uncommon)**
6667
- Run after all other plugins complete
6768
- Use cases: Metrics, logging
6869
- Example: segment.io plugin for observability metrics
6970

70-
5. **Utility Plugins**
71+
- **Utility Plugins**
7172
- Executes only once during the analytics.js bootstrap. Gives you access to the analytics instance using the plugin's load() method. This doesn't allow you to modify events.
7273
- Do not directly process events
7374
- Example: some plugin that registers a bunch of analytics event listeners (e.g. analytics.on('track', ...) and reports them to an external system)
75+
76+
- **Add Destination Middleware** (See [Example](#example-destination-middleware-implementation))
77+
- A special type of plugin that allows you to add a plugin that only affects a specific (device mode) destination plugin.
78+
7479

7580
### Example: Plugin Implementation
7681
```ts
@@ -109,6 +114,23 @@ analytics.addSourceMiddleware(({ payload, next }) => {
109114
})
110115
```
111116

117+
### Example: Destination Middleware Implementation
118+
> [!NOTE]
119+
> It is not currently possible to add a destination middleware JUST to the segment.io destination plugin.
120+
```ts
121+
analytics.addDestinationMiddleware('amplitude', ({ next, payload }) => {
122+
payload.obj.properties!.hello = 'from the other side'
123+
next(payload)
124+
})
125+
```
126+
or, to apply to all destinations
127+
```ts
128+
analytics.addDestinationMiddleware('*', (ctx) => {
129+
ctx.event.properties!.hello = 'from the other side'
130+
return ctx
131+
})
132+
```
133+
112134
### Event Flow Example
113135

114136
When `analytics.track()` is called:

0 commit comments

Comments
 (0)