Sending custom events
Even though the Analytics module natively supports Google Analytics ecommerce events, you may need to track customer activity not covered by default events. In such cases, you can trigger and intercept custom events using the
sendAnalyticsEvent
and useAnalyticsEvent
functions. In this guide, learn how to define and send custom events.Instructions
Step 1 - Declaring an interface for your custom event
To trigger a custom event, you must define an interface that describes the structure of your event object, including its properties and types. There are three approaches to doing this:
- Creating a new event interface
- Extending existing types from the Analytics Module
- Overriding Multiple Types
Creating a new event interface
To create a custom event interface, use the
sendAnalyticsEvent
function. This function requires the event to include two properties:Property name | Type | Description |
---|---|---|
name | string | The name of the event that appears in Analytics reports. The name doesn't need to follow any event name conventions related to natively supported events. |
params | any | Any type and value your custom event uses. |
Example
In the following example, we define a custom event called
WishlistEvent
to track when a user adds a product to their wishlist:
_13import { sendAnalyticsEvent } from '@faststore/sdk'_13_13interface WishlistEvent {_13 name: 'wishlist-event',_13 params: {_13 productId: string_13 productName: string_13 }_13 userId: string_13 isLoggedIn: boolean_13}_13_13sendAnalyticsEvent<WishlistEvent>({ name, params, userId, isLoggedIn })
This custom event interface captures when a user adds a product to their wishlist, logging details like
productId
, productName
, and whether the user is logged in (isLoggedIn
). This data can then be sent to your analytics tool to better understand customer behavior, such as which products are popular on wishlists and how engagement varies between logged-in and anonymous users.Extending existing types from the Analytics module
If your event is related to an existing one, you can extend relevant types from the Analytics module. You can do this by using the TypeScript generics with the
sendAnalyticsEvent
function.Example
The following example extends the
AddToCartEvent
interface to include the couponCode
property, which is useful for tracking if a customer applied a coupon code when adding an item to the cart:
_10import type { AddToCartEvent } from '@faststore/sdk'_10import { sendAnalyticsEvent } from '@faststore/sdk'_10_10interface AddToCartExtended extends AddToCartEvent {_10 couponCode: string_10}_10_10/* ... */_10_10sendAnalyticsEvent<AddToCartExtended>({ name, params, couponCode })
Overriding multiple types
If you have multiple custom events, such as adding/removing items from the cart or viewing products, you can define a unified type to handle them all. This approach simplifies firing multiple events from the same interface.
_18/* types.ts */_18import { sendAnalyticsEvent } from '@faststore/sdk'_18_18type AddToCartExtended = /* ... */_18type RemoveFromCartExtended = /* ... */_18type ViewItemExtended = /* ... */_18type SelectItemExtended = /* ... */_18_18type ExtendedEvents =_18| AddToCartExtended_18| RemoveFromCartExtended_18| ViewItemExtended_18| SelectItemExtended_18_18type SendExtendedAnalyticsEvent = (event: ExtendedEvents) => void_18_18export const sendExtendedAnalyticsEvent: SendExtendedAnalyticsEvent = (event) =>_18sendAnalyticsEvent<ExtendedEvents>(event)
_10/* MyComponent.tsx */_10import { sendExtendedAnalyticsEvent } from './types'_10_10/* ... */_10_10sendExtendedAnalyticsEvent({ /* Extended event object */})
The example above sets up a flexible way to fire multiple custom events. Instead of managing multiple event types separately, the purpose of the example is to handle them all through a single interface. You can use this setup to track diverse customer interactions, such as adding/removing products from the cart, viewing products, or saving items to the wishlist.
Step 2 - Intercepting custom events
After defining the event interface, you'll need to intercept these events using the
useAnalyticsEvent
hook. Below is an example of how to set up an event handler:
_12import { useAnalyticsEvent } from '@faststore/sdk'_12_12import type { ArbitraryEvent } from './types'_12_12export const AnalyticsHandler = () => {_12useAnalyticsEvent((event: ArbitraryEvent) => {_12})_12_12/* ... */_12_12return null_12}
Note that to target extended properties of events, you'll also need to configure the types of your
useAnalyticsEvent
callback function to expect an event of such type.
_14import { useAnalyticsEvent } from '@faststore/sdk'_14_14import type { ExtendedEvents } from './types'_14_14export const AnalyticsHandler = () => {_14_14useAnalyticsEvent((event: ExtendedEvents) => {_14 /* ... */_14})_14_14/* ... */_14_14return null_14}
By typing the callback function with the extended types, you can reference properties not natively offered by the analytics module.
Step 3 - Firing custom events
Now that you have declared your event interface and intercepted it with the
useAnalyticsEvent
hook, you can implement it in your components to fire the event.
_21import { useCallback } from 'react'_21import { sendAnalyticsEvent } from '@faststore/sdk'_21_21const MyComponent = () => {_21const arbitraryEvent = useCallback(() => {_21 /* ... */_21_21 const arbitraryEvent = {_21 type: 'arbitrary-event',_21 data: {_21 items: [_21 /* ... */_21 ],_21 },_21 }_21_21 sendAnalyticsEvent(arbitraryEvent)_21}, [])_21_21return <button onClick={arbitraryEvent}>Arbitrary button</button>_21}