Skip to content

Commit

Permalink
docs(headless-ssr-commerce): log product view events (#4833)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmarceau authored Jan 15, 2025
1 parent bcac152 commit 3153a4b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as externalCartAPI from '@/actions/external-cart-api';
import ContextDropdown from '@/components/context-dropdown';
import ProductViewer from '@/components/product-viewer';
import {
RecommendationProvider,
StandaloneProvider,
Expand Down Expand Up @@ -64,7 +65,9 @@ export default async function ProductDescriptionPage({

const resolvedSearchParams = await searchParams;
const price = Number(resolvedSearchParams.price) ?? NaN;
const name = resolvedSearchParams.name ?? params.productId;
const name = Array.isArray(resolvedSearchParams.name)
? params.productId
: (resolvedSearchParams.name ?? params.productId);

return (
<StandaloneProvider
Expand All @@ -74,6 +77,7 @@ export default async function ProductDescriptionPage({
<h2>Product description page</h2>
<ContextDropdown />
<StandaloneSearchBox />
<ProductViewer productId={params.productId} name={name} price={price} />
<p>
{name} ({params.productId}) - ${price}
</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use client';

import {useProductView} from '@/lib/commerce-engine';
import {useEffect} from 'react';

interface Product {
productId: string;
name: string;
price: number;
}

export default function ProductViewer({productId, name, price}: Product) {
const {methods} = useProductView();
let productViewEventEmitted = false;

useEffect(() => {
if (methods && !productViewEventEmitted) {
methods?.view({productId, name, price});
productViewEventEmitted = true;
}
}, []);

return null;
}

0 comments on commit 3153a4b

Please sign in to comment.