-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add recently viewed products widget on pixel code sample (#40)
Co-authored-by: Anand Gorantala <[email protected]>
- Loading branch information
1 parent
f1b7fae
commit fc5d4b3
Showing
13 changed files
with
194 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
examples/pixel/src/components/ItemBasedRecommendationsWidget.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useEffect, useState } from 'react'; | ||
import useAnalytics from '../hooks/useAnalytics'; | ||
import useItemBasedRecommendationsApi from '../hooks/useItemBasedRecommendationsApi'; | ||
import { ProductsCarouselWidget } from './ProductsCarouselWidget'; | ||
import { CONFIG } from '../constants'; | ||
|
||
export function ItemBasedRecommendationsWidget({ title = 'Similar products', widgetId, pids }) { | ||
const { trackEvent } = useAnalytics(); | ||
const [options, setOptions] = useState({}); | ||
const { data } = useItemBasedRecommendationsApi(widgetId, CONFIG, options); | ||
|
||
useEffect(() => { | ||
if (pids && pids.length) { | ||
setOptions({ | ||
item_ids: pids.join(','), | ||
filter: `-pid:(${pids.map((pid) => `"${pid}"`).join(' OR ')})`, | ||
rows: 4, | ||
start: 0, | ||
}); | ||
} | ||
}, [pids]); | ||
|
||
useEffect(() => { | ||
if (!data?.metadata) { | ||
return; | ||
} | ||
|
||
trackEvent({ | ||
event: 'event_widgetView', | ||
widgetId: data.metadata.widget.id, | ||
widgetType: data.metadata.widget.type, | ||
widgetRequestId: data.metadata.widget.rid, | ||
}); | ||
}, [data]); | ||
|
||
|
||
if (!data?.response) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<div className="flex gap-2 my-2 items-center"> | ||
<div className="font-semibold text-xl opacity-80">{title}</div> | ||
</div> | ||
{data && (<ProductsCarouselWidget data={data} />)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useEffect, useState } from 'react'; | ||
import useAnalytics from '../hooks/useAnalytics'; | ||
import usePersonalizedWidgetApi from '../hooks/usePersonalizedWidgetApi'; | ||
import { ProductsCarouselWidget } from './ProductsCarouselWidget'; | ||
import { CONFIG } from '../constants'; | ||
|
||
export function PersonalizedWidget({ widgetId, title = 'Recently viewed products' }) { | ||
const { trackEvent } = useAnalytics(); | ||
const [options] = useState({ rows: 4, start: 0 }); | ||
const { data } = usePersonalizedWidgetApi(widgetId, CONFIG, options); | ||
|
||
useEffect(() => { | ||
if (!data?.metadata) { | ||
return; | ||
} | ||
|
||
trackEvent({ | ||
event: 'event_widgetView', | ||
widgetId: data.metadata.widget.id, | ||
widgetType: data.metadata.widget.type, | ||
widgetRequestId: data.metadata.widget.rid, | ||
}); | ||
}, [data]); | ||
|
||
|
||
if (!data?.response) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<div className="flex gap-2 my-2 items-center"> | ||
<div className="font-semibold text-xl opacity-80">{title}</div> | ||
</div> | ||
{data && (<ProductsCarouselWidget data={data} />)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.