Documentation
Feedback
Guides
Storefront Development

Storefront Development
FastStore
FastStore SDK
Analytics
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


_10
import type { AnalyticsEvent } from '@faststore/sdk'
_10
import { 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).

_15
import type { AnalyticsEvent } from '@faststore/sdk'
_15
import { useAnalyticsEvent } from '@faststore/sdk'
_15
_15
export 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.
Contributors
1
Photo of the contributor
+ 1 contributors
Was this helpful?
Yes
No
Suggest Edits (GitHub)
Contributors
1
Photo of the contributor
+ 1 contributors
On this page