Skip to content

Commit 7843ceb

Browse files
committed
wip
1 parent 3af83bf commit 7843ceb

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

packages/browser/architecture/ARCHITECTURE.md

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,88 @@
33
> [!IMPORTANT]
44
> This doc may get out-of-date. Please prefer to use and link to Segment documentation for the most up-to-date information. It would be advisable to move this doc to https://segment.com/docs/connections/sources/catalog/libraries/website/javascript, so there is a single source of truth.
55
6-
### Event Flow Diagram
7-
More details on plugin architecture can be found here:
8-
https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#plugins-and-source-middleware
96

107
### You can use the [vscode mermaid extension](https://marketplace.visualstudio.com/items?itemName=bierner.markdown-mermaid) to preview the following diagram code block inside of vscode, or copy and paste into the [mermaid live editor](https://mermaid.live/).
118

9+
## Table of Contents
10+
- [Initialization Flow](#initialization-flow)
11+
- [Plugin Types and Middleware](#plugin-types-and-middleware)
12+
13+
14+
### Analytics.js Initialization Flow
15+
The following diagram illustrates how Analytics.js initializes and loads device mode destinations:
16+
17+
```mermaid
18+
sequenceDiagram
19+
participant Page as Web Page
20+
participant AJS as Analytics.js
21+
participant CDN as Segment CDN
22+
participant Dest as Device Mode Destinations
23+
24+
Note over Page,Dest: Initialization Phase
25+
Page->>AJS: Load analytics.js<br/>with writeKey
26+
AJS->>CDN: Fetch settings
27+
CDN-->>AJS: Return CDN settings
28+
29+
Note over AJS: Block events unil all non-destination plugins and middleware are loaded
30+
31+
Note over AJS: Parse device mode<br/>destinations
32+
33+
Note over AJS,Dest: Begin parallel destination loading
34+
par Parallel Destination Loading
35+
AJS->>Dest: Load destination 1<br/>(async)
36+
AJS->>Dest: Load destination 2<br/>(async)
37+
end
38+
39+
Note over AJS,Dest: Each destination loads its own scripts
40+
41+
Note over AJS: If critical plugins exist,<br/>wait for their registration
42+
43+
par Event Delivery (per destination)
44+
AJS->>Dest: Flush buffered events to segment.io
45+
AJS->>Dest: Flush buffered events to dest 1
46+
AJS->>Dest: Flush buffered events to dest 2
47+
end
48+
49+
```
50+
51+
Key Points:
52+
- Analytics.js is loaded with a writeKey that identifies your Segment source
53+
- CDN settings are fetched containing destination configurations
54+
- Event queue is blocked until all non-destination plugins and middleware are registered
55+
- Each destination may load its own third-party scripts
56+
- Critical plugins (if any) must complete registration before event delivery
57+
- Buffered events are flushed to destinations in parallel once they're ready
58+
59+
60+
### Event Flow
1261
```mermaid
1362
graph TD
1463
subgraph Event Creation
15-
A[analytics.track/page/identify] --> B[Event Factory]
16-
B --> C[Event Queue]
64+
UserAction[analytics.track/page/identify] --> EventCreate[Event Factory]
65+
EventCreate --> Queue[Event Queue]
1766
end
1867
1968
subgraph Plugin Pipeline
20-
C --> D[Before Plugins e.g add page context]
21-
D --> E[Enrichment Plugins]
22-
E --> F[Destination Plugins e.g Segment.io]
23-
F --> G[After Plugins]
69+
Queue --> BeforePlugins[Before Plugins e.g add page context]
70+
BeforePlugins --> EnrichPlugins[Enrichment Plugins]
71+
EnrichPlugins --> DestPlugins[Destination Plugins e.g Segment.io]
72+
DestPlugins --> AfterPlugins[After Plugins]
2473
end
2574
2675
subgraph Plugin Types Details
27-
I[Before Plugins<br/>Priority: Critical<br/>Example: Event Validation] --- D
28-
J[Enrichment Plugins<br/>Priority: Critical<br/>Parallel Execution<br/>Can Modify Events<br/>Example: Add User Agent] --- E
29-
K[Destination Plugins<br/>Parallel Execution<br/>Cannot Modify Events<br/>Example: Google Analytics] --- F
30-
L[After Plugins<br/>Example: Metrics Collection] --- G
76+
BeforeDetails[Before Plugins<br/>Priority: Critical<br/>Example: Event Validation] --- BeforePlugins
77+
EnrichDetails[Enrichment Plugins<br/>Priority: Critical<br/>Parallel Execution<br/>Can Modify Events<br/>Example: Add User Agent] --- EnrichPlugins
78+
DestDetails[Destination Plugins<br/>Parallel Execution<br/>Cannot Modify Events<br/>Example: Google Analytics] --- DestPlugins
79+
AfterDetails[After Plugins<br/>Example: Metrics Collection] --- AfterPlugins
3180
end
3281
3382
subgraph Notes
34-
M[Plugin Priorities]
35-
N[Critical - Must load before<br/>event delivery starts]
36-
O[Non-Critical - Can load<br/>after event delivery starts]
37-
M --> N
38-
M --> O
83+
Priorities[Plugin Priorities]
84+
Critical[Critical - Must load before<br/>event delivery starts]
85+
NonCritical[Non-Critical - Can load<br/>after event delivery starts]
86+
Priorities --> Critical
87+
Priorities --> NonCritical
3988
end
4089
```
4190

@@ -132,6 +181,7 @@ analytics.addDestinationMiddleware('*', (ctx) => {
132181
})
133182
```
134183

184+
135185
### Event Flow Example
136186

137187
When `analytics.track()` is called:
@@ -149,3 +199,5 @@ When `analytics.track()` is called:
149199
- Example: Before plugins, Validation plugins
150200
- **Non-Critical Plugins**: Can load after event delivery begins
151201
- Example: Destination plugins
202+
203+

0 commit comments

Comments
 (0)