Skip to main content
A track call in Analytics Web adds the event to a queue in memory and returns. The network request runs later, in the background. This page lists the size of the package, how the queue sends, how the configuration cache works, and what the React hooks do to limit re-renders.

Package size

The package is tree-shakeable. A bundler removes the exports that the app does not import.

Batching

The SDK collects events in a queue and sends the queue as one request. The queue flushes every flushInterval milliseconds, or when it holds batchSize events, whichever comes first. In a serverless function the process can end before the timer fires. Send each event with flush: true and wait for it.
On page unload the SDK sends the queue with the Beacon API. The browser completes that request after the page closes. Read Configuration options for every value.

Configuration cache

getConfig returns a value from memory without a network request. The order is: the value from the last fetch, then the value in localStorage, then defaultConfigurations. The SDK fetches new values in the background and refreshes them every configRefreshInterval milliseconds. Call preloadConfig at startup to fetch the keys that the first screen needs.
Read Remote configuration for the cache order.

React hooks

useConfig(key) re-renders the component only when that key changes. useAllConfigs() re-renders when any key changes. Use useConfig for one value.
useTrack() returns a stable function reference. It is safe in a useCallback dependency list, and it does not need memoization of its own.
Read React hooks.

Loading later

Create the client on first use when the first screen does not track anything.
In React, lazy() loads the provider in its own chunk. Tracking starts after that chunk loads.

Measuring the cost

Set debug: true to write batch send times, queue sizes, requests, and retry attempts to the console. A track call takes less than 1 millisecond, because it only adds to the queue.