Analytics
Analytics is especially important for the Coveo. The AI Model learns how to give more accurate results based on it. Components shipped by us, already leverage Interactive Results and Interactive Instant Results on click but there is much more.
A lot of events are already supported by the Coveo Headless library, for example, if you send a query request, or select facet, change the sort order and so on. Basically, every interaction with a headless controller will be automatically tracked. However, there are some events that are not supported out of the box, like view product details page, add to cart, remove from cart, etc.
Currently supported events:
- all native headless events
- click on the interactive result (search result)
- click on the interactive instant result (quick search result)
- view product details page
Table of contents
Our module doesn't support each kind of events supported by Coveo yet.
Including coveoua
library
To handle custom events, there is a coveoua library that can be used.
Our composable useCoveoua
bases on the coveoua
library that has to be added to the head
. You can do that by modifying nuxt.config.js
file, and adding inside app.head
object:
// `useCoveoua` hook loads the `coveoua` library automatically. You do not need to include it manually.
Capturing customer events
To reach better personalization you need to attach anonymous identifier to authenticated customer:
// storefront-unified-nextjs/app/[locale]/layout.tsx
import { CoveoCustomer } from '@/sf-modules/coveo'; // ...
export default async function RootLayout({ children, params: { locale } }: RootLayoutProps) {
// ...
return (
<html lang={locale}>
<head>
<link href="res.cloudinary.com" rel="preconnect" />
</head>
<body className={classNames(fontHeadings.variable, fontBody.variable, 'font-body')}>
<NextIntlClientProvider
messages={pick(messages, 'Notifications.NotificationAlert', 'SapAsm', 'CartProductCard')}
>
<PublicEnvProvider>
<Providers initialCurrency={currencies} sdkOptions={sdkConfig}>
<CoveoCustomer />
Analytics mappers
To facilitate working with Coveoua and our ecosystem, we provide mappers transforming data types to required by analytics CoveouaProduct
type. Thanks to that, you could easily send events using data from different sources like:
Using existing mappers
Using mapGetProductDetailsToCoveouaProduct
mapper that has the following signature:
(source: GetProductDetailsData): CoveouaProduct
import { mapGetProductDetailsToCoveouaProduct, useCoveoua } from '@sf-modules/coveo';
//...
const coveoua = useCoveoua();
const productDetail = useProductDetails({ id, sku });
useEffect(() => {
coveoua.sendViewPDP(productDetail.data!)); // TS Error: Invalid type!
coveoua.sendViewPDP(mapGetProductDetailsToCoveouaProduct(productDetail.data!)); // It works
}, []);
//...
Using mapNormalizedResultToCoveouaProduct
mapper that has the following signature:
(result: NormalizedResult): CoveouaProduct
import { mapNormalizedResultToCoveouaProduct, useCoveoua } from '@sf-modules/coveo';
//...
const coveoua = useCoveoua();
const productCatalog = useCoveoSearchProducts();
// inside some callback
const someProduct = productsCatalog.products[0].result;
coveoua.sendViewPDP(someProduct!)); // TS Error: Invalid type!
coveoua.sendViewPDP(mapNormalizedResultToCoveouaProduct(someProduct!)); // It works
//...
Creating own analytics mapper
What if your data type doens't match any aforementioned mappers? In that case, it is recommended to create own mapper.
It is as simple as creating function, all it has to do is return CoveouaProduct
type.
import { CoveouaProduct } from "@sf-modules/coveo";
interface MyType {
a: number;
b: string;
// ...
}
export function mapMyTypeToCoveouaProduct(result: MyType): CoveouaProduct {
return {
// ...
};
}
Usage:
import { useCoveoua } from "@sf-modules/coveo";
import { mapMyTypeToCoveouaProduct } from "...";
//...
const coveoua = useCoveoua();
// In some function/composable/hook:
const myData: MyType = // ...
coveoua.sendViewPDP(myData!); // TS Error: Invalid type!
coveoua.sendViewPDP(mapMyTypeToCoveouaProduct(myData!)); // It works
//...
Events
Below, we specify every supported custom events with a brief description of how to implement it in your application.
Available events:
View PDP event
This event should be sent when a user views a product details page. It is important for the Coveo AI Model to learn more about the user's behavior. To track this event, you can use the sendViewPDP
method from the useCoveoua
composable.
// storefront-unified-nextjs/app/[locale]/(default)/product/[slug]/[id]/page.tsx
import { CoveoPdpCoeveoua } from '@sf-modules/coveo'; // ...
export default async function ProductDetailsPage({ params, searchParams }: ProductDetailsPageProps) {
return (
<CoveoClientProvider>
<CoveoPdpCoeveoua {...productData} /> <Portal>
// ...
//...
Cart events
This section assumes, you already implemented View PDP event.
There are 3 places where you can modify the cart in our application:
- PDP,
- PLP (Category/Search results),
- Cart view.
We need to trigger events in all of them.
PDP
// storefront-unified-nextjs/components/add-to-cart-button.tsx
import { useCoveouaCart } from '@sf-modules/coveo';
// ...
const { sendAddToCartEvent } = useCoveouaCart();
// ...
return (
<SfButton
onClick={() => addToCart.mutate({ quantity })} onClick={() => { addToCart.mutate({ quantity }); sendAddToCartEvent({ productId, quantity: quantity || 1, sku: sku as string | undefined }); }} // ...
// ...
PLP
It's covered by us changes made in previous step.
Cart view
// storefront-unified-nextjs/app/[locale]/(default)/cart/components/product-card.tsx
import { useCoveouaCart } from '@sf-modules/coveo';
// ...
const { id, value, sku } = props;
const { sendUpdateCartEvent, sendRemoveFromCartEvent } = useCoveouaCart();
return (
<CartProductCard
{...props}
onUpdate={(quantity) => {
updateCartQuantity.mutate({ quantity });
removeWantedQuantities(id);
if (product.sku) { sendUpdateCartEvent({ oldQuantity: product.quantity, productId: id, quantity, sku: product.sku }); } }}
onRemove={() => {
removeCartLineItem.mutate();
removeWantedQuantities(id);
if (product.sku) { sendRemoveFromCartEvent({ productId: id, quantity: product.quantity, sku: product.sku }); } }}
// ...
Purchase event
The sendPurchase
method should be called just after user placed an order.
import { useCoveoua } from '@sf-modules/coveo';
const coveoua = useCoveoua();
const onPlacedOrderSuccesfully = (order: SfOrder) => {
coveoua.sendPurchase(order!);
};