Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(headless-ssr-commerce): log product view events #4833

Merged
merged 8 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading