useAnalyticsEvent
The
useAnalyticsEvent
hook intercepts both native and custom events triggered by the sendAnalyticsEvent
function. The hook automatically detects the events sent by sendAnalyticsEvent
and provides a response to them.Import
_10import type { AnalyticsEvent } from '@faststore/sdk'_10import { useAnalyticsEvent } from '@faststore/sdk'
Usage
The
useAnalyticsEvent
hook accepts a callback function that runs whenever the sendAnalyticsEvent
is triggered. The callback function receives the event that triggered its execution as an argument.Ideally, you should use the
useAnalyticsEvent
hook to transmit events to the analytics provider of your choice (e.g., Google Analytics).
_15import type { AnalyticsEvent } from '@faststore/sdk'_15import { useAnalyticsEvent } from '@faststore/sdk'_15_15export const AnalyticsHandler = () => {_15 useAnalyticsEvent((event: AnalyticsEvent) => {_15 /**_15 * This is where you can send events to your analytics provider_15 *_15 * Example:_15 * window.dataLayer.push({ event: event.name, ecommerce: event.params })_15 */_15 })_15_15 /* ... */_15}
Events from external libraries
External libraries can also send events via the Analytics module, which means you might come across unexpected events being intercepted by
useAnalyticsEvent
. This is usually not an issue, as most common analytics providers don't break if you send them meaningless data. However, if that's the case, filter events by name to ensure only the desired events are sent to the provider.