Skip to main content
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 wraps track in useCallback with the client as its only dependency. The reference changes only when the provider gives a new client. Because of this:
  1. Pass track to child components without a wrapper. A memoized child does not re-render because of it.
  2. List track in the dependency array of an effect. The effect runs one time on mount.
  3. Do not call track in 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.