Skip to content

Commit 3153a4b

Browse files
authored
docs(headless-ssr-commerce): log product view events (#4833)
https://coveord.atlassian.net/browse/KIT-3826 This PR adds a component to log product view events on PDPs. Relevant CSR docs: https://docs.coveo.com/en/o8ce0239#displaying-a-product-on-a-product-detail-page
1 parent bcac152 commit 3153a4b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as externalCartAPI from '@/actions/external-cart-api';
22
import ContextDropdown from '@/components/context-dropdown';
3+
import ProductViewer from '@/components/product-viewer';
34
import {
45
RecommendationProvider,
56
StandaloneProvider,
@@ -64,7 +65,9 @@ export default async function ProductDescriptionPage({
6465

6566
const resolvedSearchParams = await searchParams;
6667
const price = Number(resolvedSearchParams.price) ?? NaN;
67-
const name = resolvedSearchParams.name ?? params.productId;
68+
const name = Array.isArray(resolvedSearchParams.name)
69+
? params.productId
70+
: (resolvedSearchParams.name ?? params.productId);
6871

6972
return (
7073
<StandaloneProvider
@@ -74,6 +77,7 @@ export default async function ProductDescriptionPage({
7477
<h2>Product description page</h2>
7578
<ContextDropdown />
7679
<StandaloneSearchBox />
80+
<ProductViewer productId={params.productId} name={name} price={price} />
7781
<p>
7882
{name} ({params.productId}) - ${price}
7983
</p>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use client';
2+
3+
import {useProductView} from '@/lib/commerce-engine';
4+
import {useEffect} from 'react';
5+
6+
interface Product {
7+
productId: string;
8+
name: string;
9+
price: number;
10+
}
11+
12+
export default function ProductViewer({productId, name, price}: Product) {
13+
const {methods} = useProductView();
14+
let productViewEventEmitted = false;
15+
16+
useEffect(() => {
17+
if (methods && !productViewEventEmitted) {
18+
methods?.view({productId, name, price});
19+
productViewEventEmitted = true;
20+
}
21+
}, []);
22+
23+
return null;
24+
}

0 commit comments

Comments
 (0)