Documentation
Feedback
Guides
Storefront Development

Storefront Development
FastStore
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:

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 nameTypeDescription
namestringThe name of the event presented in Analytics reports. The name doesn't need to follow any event name conventions related to natively supported events.
paramsanyAny 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:

_13
import { sendAnalyticsEvent } from '@faststore/sdk'
_13
_13
interface WishlistEvent {
_13
name: 'wishlist-event',
_13
params: {
_13
productId: string
_13
productName: string
_13
}
_13
userId: string
_13
isLoggedIn: boolean
_13
}
_13
_13
sendAnalyticsEvent<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.:

_10
import type { AddToCartEvent } from '@faststore/sdk'
_10
import { sendAnalyticsEvent } from '@faststore/sdk'
_10
_10
interface AddToCartExtended extends AddToCartEvent {
_10
couponCode: string
_10
}
_10
_10
/* ... */
_10
_10
sendAnalyticsEvent<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 */
_18
import { sendAnalyticsEvent } from '@faststore/sdk'
_18
_18
type AddToCartExtended = /* ... */
_18
type RemoveFromCartExtended = /* ... */
_18
type ViewItemExtended = /* ... */
_18
type SelectItemExtended = /* ... */
_18
_18
type ExtendedEvents =
_18
| AddToCartExtended
_18
| RemoveFromCartExtended
_18
| ViewItemExtended
_18
| SelectItemExtended
_18
_18
type SendExtendedAnalyticsEvent = (event: ExtendedEvents) => void
_18
_18
export const sendExtendedAnalyticsEvent: SendExtendedAnalyticsEvent = (event) =>
_18
sendAnalyticsEvent<ExtendedEvents>(event)


_10
/* MyComponent.tsx */
_10
import { sendExtendedAnalyticsEvent } from './types'
_10
_10
/* ... */
_10
_10
sendExtendedAnalyticsEvent({ /* 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:

_12
import { useAnalyticsEvent } from '@faststore/sdk'
_12
_12
import type { ArbitraryEvent } from './types'
_12
_12
export 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.

_14
import { useAnalyticsEvent } from '@faststore/sdk'
_14
_14
import type { ExtendedEvents } from './types'
_14
_14
export 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.

_21
import { useCallback } from 'react'
_21
import { sendAnalyticsEvent } from '@faststore/sdk'
_21
_21
const 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
}

Contributors
1
Photo of the contributor
Was this helpful?
Yes
No
Suggest Edits (GitHub)
Contributors
1
Photo of the contributor
On this page