By default, events are batched and sent asynchronously. To send an event immediately within the same JavaScript execution cycle, set the sync property on the event object.
Warning: Use this sparingly. Each sync event creates a new outbound connection, which can negatively impact user experience if overused. Additionally, sendBeacon() and fetch() (with keepalive) have a maximum payload size of 64Kb; if the serialized JSON event exceeds this, it will be dropped.
Supported sync values
| Name | Value | Description |
|---|
| Batched | undefined, false, 0 | Default behavior. Events are batched and sent asynchronously. |
| Synchronous | true, 1 | Attempts to send using the first available transport: httpXHROverride, XMLHttpRequest (sync), fetch() [keepalive], or sendBeacon(). |
| SendBeacon | 2 | Attempts to send with a preference for the sendBeacon() API. Fallbacks: fetch() [keepalive], XMLHttpRequest (sync), httpXHROverride. |
| SyncFetch | 3 | Attempts to send with a preference for fetch() with the keepalive flag. Fallbacks: sendBeacon(), XMLHttpRequest (sync), httpXHROverride. |
// Example of setting a sync property (conceptual usage)
event.sync = 2; // Using SendBeacon preference