|
12 | 12 |
|
13 | 13 |
|
14 | 14 | ### Analytics.js Initialization Flow |
15 | | -The following diagram illustrates how Analytics.js initializes and loads device mode destinations: |
| 15 | +The following diagram illustrates how Analytics.js bootstraps and initializes: |
16 | 16 |
|
17 | 17 | ```mermaid |
18 | | -sequenceDiagram |
19 | | - participant Page as Web Page |
20 | | - participant AJS as analytics.load(...) |
21 | | - participant CDN as Segment CDN |
22 | | - participant Plugin as Plugin Registration |
23 | | -
|
24 | | - Note over Page,Plugin: Initialization Phase |
25 | | - Page<<->>AJS: Web Page fetches analytics.js from CDN |
26 | | - Page<<->>AJS: analytics.load(...) invoked. |
27 | | - AJS<<->>CDN: Fetch CDN settings from cdn.segment.io |
28 | | - |
29 | | - Note over AJS: Block events until all non-destination plugins and middleware are loaded |
30 | | - |
31 | | - |
32 | | - Note over AJS,Plugin: Begin parallel destination loading from CDN settings |
33 | | - par Parallel Destination Loading |
34 | | - AJS->>Plugin: Load destination 1<br/>(async) |
35 | | - AJS->>Plugin: Load destination 2<br/>(async) |
| 18 | +graph TD |
| 19 | + subgraph Load Phase |
| 20 | + Start([Web Page Load]) --> LoadAJS[Fetch analytics.js<br/>from CDN] |
| 21 | + LoadAJS --> InitAJS[analytics.load<br/>with writeKey] |
| 22 | + InitAJS --> FetchSettings[Fetch settings<br/>from cdn.segment.io] |
36 | 23 | end |
37 | 24 |
|
38 | | - Note over AJS,Plugin: Each destination loads its own scripts into the head and calls its own load() method |
39 | | - |
40 | | - Note over AJS: If critical plugins exist,<br/>wait for their registration |
| 25 | + subgraph Plugin Registration Phase |
| 26 | + FetchSettings --> RegisterCore[Register Core Plugins<br/>and Middleware] |
| 27 | + RegisterCore --> ParseDest[Parse Device Mode<br/>Destinations] |
| 28 | + end |
| 29 | +
|
| 30 | + subgraph Destination Loading Phase |
| 31 | + ParseDest --> LoadDests{Load Device Mode<br/>Destinations} |
| 32 | + LoadDests -->|Async| Dest1[Load Destination 1<br/>+ Scripts] |
| 33 | + LoadDests -->|Async| Dest2[Load Destination 2<br/>+ Scripts] |
| 34 | + LoadDests -->|Async| DestN[Load Destination N<br/>+ Scripts] |
| 35 | + end |
41 | 36 |
|
42 | | - par Event Delivery (per destination) |
43 | | - AJS->>Plugin: Flush buffered events to segment.io |
44 | | - AJS->>Plugin: Flush buffered events to destination 1 |
45 | | - AJS->>Plugin: Flush buffered events to destination 2 |
| 37 | + subgraph Event Processing Phase |
| 38 | + Dest1 --> Ready1[Destination 1<br/>Ready] |
| 39 | + Dest2 --> Ready2[Destination 2<br/>Ready] |
| 40 | + DestN --> ReadyN[Destination N<br/>Ready] |
| 41 | + |
| 42 | + Ready1 --> Flush1[Flush Events to<br/>Destination 1] |
| 43 | + Ready2 --> Flush2[Flush Events to<br/>Destination 2] |
| 44 | + ReadyN --> FlushN[Flush Events to<br/>Destination N] |
46 | 45 | end |
47 | 46 |
|
| 47 | + RegisterCore -.->|Events Blocked<br/>Until Complete| ParseDest |
| 48 | + LoadDests -.->|Critical Plugins<br/>Block Events| EventReady[Event Pipeline Ready] |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + class Dest1,Dest2,DestN,Flush1,Flush2,FlushN async |
48 | 53 | ``` |
49 | 54 |
|
| 55 | + |
50 | 56 | Key Points: |
51 | | -- Analytics.js is loaded with a writeKey that identifies your Segment source |
52 | | -- CDN settings are fetched containing destination configurations |
53 | | -- Event queue is blocked until all non-destination plugins and middleware are registered |
54 | | -- Each destination may load its own third-party scripts |
55 | | -- Critical plugins (if any) must complete registration before event delivery |
56 | | -- Buffered events are flushed to destinations in parallel once they're ready |
| 57 | +1. **Load Phase** |
| 58 | + - Analytics.js is fetched from CDN |
| 59 | + - Initialized with writeKey that identifies your Segment source |
| 60 | + - CDN settings are fetched containing destination configurations |
| 61 | + |
| 62 | +2. **Plugin Registration Phase** |
| 63 | + - Core plugins and middleware are registered synchronously |
| 64 | + - Event queue is blocked until this phase completes |
| 65 | + - Device mode destinations are identified from CDN settings |
| 66 | + |
| 67 | +3. **Destination Loading Phase** |
| 68 | + - Device mode destinations are loaded in parallel |
| 69 | + - Each destination loads its own third-party scripts |
| 70 | + - Critical plugins (if any) must complete registration |
| 71 | + |
| 72 | +4. **Event Processing Phase** |
| 73 | + - Destinations become ready independently |
| 74 | + - Buffered events are flushed to each destination in parallel |
| 75 | + - Non-critical destinations can receive events while others are still loading |
57 | 76 |
|
58 | 77 |
|
59 | 78 | ### Event Flow |
@@ -85,8 +104,16 @@ graph TD |
85 | 104 | Priorities --> Critical |
86 | 105 | Priorities --> NonCritical |
87 | 106 | end |
| 107 | +
|
| 108 | + %% Add these style definitions at the end of the diagram |
| 109 | + classDef default fill:#ffffff,stroke:#333,stroke-width:1px |
| 110 | + classDef subgraph fill:#f5f5f5,stroke:#666,stroke-width:2px |
| 111 | + |
| 112 | + class UserAction,EventCreate,Queue,BeforePlugins,EnrichPlugins,DestPlugins,AfterPlugins default |
| 113 | + class Event Creation,Plugin Pipeline,Plugin Types Details,Notes subgraph |
88 | 114 | ``` |
89 | 115 |
|
| 116 | + |
90 | 117 | ### Plugin Types and Middleware |
91 | 118 | [This information is also available in the Segment documentation](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#plugins-and-source-middleware) |
92 | 119 |
|
|
0 commit comments