|
| 1 | +## Analytics.js Plugin Architecture |
| 2 | + |
| 3 | +### Event Flow Diagram |
| 4 | +More details on plugin architecture can be found here: |
| 5 | +https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#plugins-and-source-middleware |
| 6 | + |
| 7 | + |
| 8 | +### You can use mermaid live editor to visualize the diagram: https://mermaid.live/ or use the vscode mermaid extension |
| 9 | +```mermaid |
| 10 | +graph TD |
| 11 | + subgraph Event Creation |
| 12 | + A[analytics.track/page/identify] --> B[Event Factory] |
| 13 | + B --> C[Event Queue] |
| 14 | + end |
| 15 | +
|
| 16 | + subgraph Plugin Pipeline |
| 17 | + C --> D[Before Plugins e.g add page context] |
| 18 | + D --> E[Enrichment Plugins] |
| 19 | + E --> F[Destination Plugins e.g Segment.io] |
| 20 | + F --> G[After Plugins] |
| 21 | + end |
| 22 | +
|
| 23 | + subgraph Plugin Types Details |
| 24 | + I[Before Plugins<br/>Priority: Critical<br/>Example: Event Validation] --- D |
| 25 | + J[Enrichment Plugins<br/>Priority: Critical<br/>Parallel Execution<br/>Can Modify Events<br/>Example: Add User Agent] --- E |
| 26 | + K[Destination Plugins<br/>Parallel Execution<br/>Cannot Modify Events<br/>Example: Google Analytics] --- F |
| 27 | + L[After Plugins<br/>Example: Metrics Collection] --- G |
| 28 | + end |
| 29 | +
|
| 30 | + subgraph Notes |
| 31 | + M[Plugin Priorities] |
| 32 | + N[Critical - Must load before<br/>event delivery starts] |
| 33 | + O[Non-Critical - Can load<br/>after event delivery starts] |
| 34 | + M --> N |
| 35 | + M --> O |
| 36 | + end |
| 37 | +``` |
| 38 | + |
| 39 | +### Plugin Types Explanation |
| 40 | +https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#add-destinations-from-npm |
| 41 | + |
| 42 | +**Source Middleware is just a light API wrapper around a "Before" type plugin Plugin** |
| 43 | + |
| 44 | +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. |
| 45 | + |
| 46 | +1. **Before Plugins** |
| 47 | + - Run before any other plugins |
| 48 | + - Critical priority - block event pipeline until loaded |
| 49 | + - Use cases: Event validation, data transformation |
| 50 | + - Example: Event validation before passing to other plugins) |
| 51 | + |
| 52 | +2. **Enrichment Plugins** |
| 53 | + - 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. |
| 54 | + |
| 55 | +3. **Destination Plugins** |
| 56 | + - Run after enrichment |
| 57 | + - Cannot modify the event |
| 58 | + - Execute in parallel |
| 59 | + - Failures do not halt pipeline |
| 60 | + - Example: Segment.io, Google Analytics, Mixpanel |
| 61 | + |
| 62 | +4. **After Plugins (uncommon)** |
| 63 | + - Run after all other plugins complete |
| 64 | + - Use cases: Metrics, logging |
| 65 | + - Example: segment.io plugin for observability metrics |
| 66 | + |
| 67 | +5. **Utility Plugins** |
| 68 | + - 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. |
| 69 | + - Do not directly process events |
| 70 | + - Example: some plugin that registers a bunch of analytics event listeners (e.g. analytics.on('track', ...) and reports them to an external system) |
| 71 | +### Event Flow Example |
| 72 | + |
| 73 | +When `analytics.track()` is called: |
| 74 | + |
| 75 | +1. Event is created via Event Factory |
| 76 | +2. Event enters the queue |
| 77 | +3. Before plugins validate/transform |
| 78 | +4. Enrichment plugins add data in parallel |
| 79 | +5. Destination plugins receive the event in parallel (including Segment.io plugin) |
| 80 | +6. Any after plugins handle post-processing (e.g. metrics collection) |
| 81 | + |
| 82 | +### Plugin Priorities |
| 83 | + |
| 84 | +- **Critical Plugins**: Must be loaded before event delivery starts |
| 85 | + - Example: Before plugins, Validation plugins |
| 86 | +- **Non-Critical Plugins**: Can load after event delivery begins |
| 87 | + - Example: Destination plugins |
| 88 | + |
| 89 | + |
0 commit comments