Sending custom events
Even though the Analytics module supports Google Analytics ecommerce events by default, a store may need to monitor customer activity not covered by the Analytics module. In these cases, it is still possible to use the
sendAnalyticsEvent
and useAnalyticsEvent
functions to fire and intercept custom events. In this guide, learn how to define and send custom events.Instructions
Step 1 - Declaring an interface for your custom event
To fire a custom event, you first need to declare an interface that describes the structure of your event object, including all its properties and types. To do that, you can:
- Create a new event interface
- Extend existing types from the Analytics module
- Override multiple types
Creating a new event interface
You can use the
sendAnalyticsEvent
function to create a custom event interface. This function demands that the event contains two properties:Property name | Type | Description |
---|---|---|
name | string | The name of the event presented 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 used by your custom event. |
Take the following example to track when a user adds a product to their wishlist by defining the
WishlistEvent
:
_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 allows you to capture when a user adds a product to their wishlist. It logs details like the
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 extended existing types from the Analytics module by using the generics available on the
sendAnalyticsEvent
function.Take the following example where the
AddToCartEvent
interface is extended to also accept the couponCode
property, useful to track 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, like adding to the cart, removing from the cart, or viewing products, you can create a unified type to manage them all and easily fire extended events:
_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) =>_18 sendAnalyticsEvent<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 various custom events. Instead of managing multiple event types separately, the purpose of the example is to handle them all through a single interface, making. 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 creating or extending an event interface, you'll need to intercept these events using the
useAnalyticsEvent
hook. You can do that in the following:
_12import { useAnalyticsEvent } from '@faststore/sdk'_12_12import type { ArbitraryEvent } from './types'_12_12export const AnalyticsHandler = () => {_12 useAnalyticsEvent((event: ArbitraryEvent) => {_12 })_12_12 /* ... */_12_12 return null_12}
Also, notice 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_14 useAnalyticsEvent((event: ExtendedEvents) => {_14 /* ... */_14 })_14_14 /* ... */_14_14 return null_14}
By typing the callback function with the extended types, you are able to reference properties that are not natively offered by the analytics module.
Step 3 - Firing custom events
Now that you have declared your event interface and intercepted them with the
useAnalyticsEvent
hook, you can implement it in your components to fire the event when desired.
_21import { useCallback } from 'react'_21import { sendAnalyticsEvent } from '@faststore/sdk'_21_21const MyComponent = () => {_21 const 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_21 return <button onClick={arbitraryEvent}>Arbitrary button</button>_21}