useTrack() returns the track method of the client as a function with a stable reference. Use it
in event handlers and effects.
Signature
The function calls
client.track(eventName, properties, options). Read
Track an event.
Stable reference
The hook wrapstrack in useCallback with the client as its only dependency. The reference
changes only when the provider gives a new client. Because of this:
- Pass
trackto child components without a wrapper. A memoized child does not re-render because of it. - List
trackin the dependency array of an effect. The effect runs one time on mount. - Do not call
trackin the render body. Each render sends one more event. Call it in a handler or an effect.
Send before navigation
The client batches events. If the page unloads before the next flush, the queue is lost. Pass{ flush: true } and wait for the promise before you navigate.
Send on mount
Template events
useTrack sends custom events only. For trackLogin, trackPurchase, and the other template
methods, read the client with useGrainAnalytics. Read Template events
and useGrainAnalytics.