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
Copy file name to clipboardExpand all lines: packages/browser/architecture/ARCHITECTURE.md
+70-18Lines changed: 70 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,39 +3,88 @@
3
3
> [!IMPORTANT]
4
4
> 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.
5
5
6
-
### Event Flow Diagram
7
-
More details on plugin architecture can be found here:
### 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/).
11
8
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
0 commit comments