Support pre-init command queuing #112
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Enables calling
identify(),track(),page(),alias(), andupdateTraits()beforeinit(). Commands are queued and replayed wheninit()is called with granted consent.Usage
Details
Why this is needed
In frameworks like React, effects run bottom-to-top. When
init()is called in a layout component and tracking happens in a child component, the child's effect fires first:This is now handled gracefully — calls before
init()are queued and replayed.Architecture
Replaced
EventQueue(event-only) with genericQueue<QueueItem>that handles both:flowchart TD A[Method called] --> B{Initialized?} B -->|No| C[Queue command + runtime context] B -->|Yes| D{Consent granted?} D -->|Yes| E[Send immediately] D -->|Pending/Dismissed| F[Queue event payload] D -->|Denied| G[Discard] H[init called] --> I{Consent status?} I -->|Granted| J[Flush queue] I -->|Denied| K[Clear queue] I -->|Pending| L[Keep queued]Runtime context capture
For time-sensitive data (
track(),page()), browser state is captured at call time:This ensures queued events reflect the user's state when the method was called, not when the queue is flushed.
Key behaviors
track()identify()distinct_idfor subsequent itemspage()alias()updateTraits()reset()configure()getTrackingConsent()"pending"Queue overflow
Queue has
1000item capacity. When full, oldest items are dropped with a dev warning: