Documentation
Feedback
Guides
Storefront Development

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

To create a custom event interface, use the sendAnalyticsEvent function. This function requires the event to include two properties:
Property nameTypeDescription
namestringThe 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.
paramsanyAny 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:

_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 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:

_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, 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 */
_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 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:

_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
}

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.

_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 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.

_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