medusa-plugin-recently-view
v0.0.1
Published
Track and display recently viewed products for Medusa v2. Improves conversion with product view history.
Maintainers
Readme
Medusa Recently Viewed Products Plugin
Track and display recently viewed products in your Medusa v2 store. This plugin helps improve conversion by showing customers products they've already shown interest in.
Features
- Record product views – Track when customers view product pages (logged-in or anonymous)
- Get recent products – Retrieve recently viewed product IDs for display in carousels, checkout, etc.
- Session support – Works for both authenticated customers and anonymous visitors (via
session_id) - Configurable – 20 products max, 30-day TTL (configurable in code)
- Auto cleanup – Scheduled job removes records older than 30 days (daily at 3 AM)
Installation
npm install medusa-plugin-recently-view
# or
yarn add medusa-plugin-recently-viewSetup
- Register the plugin in
medusa-config.ts:
import { defineConfig } from "@medusajs/framework/utils"
export default defineConfig({
// ... your config
plugins: [
{
resolve: "medusa-plugin-recently-view",
options: {},
},
],
})- Run migrations:
npx medusa db:migrateAPI Reference
POST /store/product-views
Record a product view. Call this when a customer views a product page.
Request body:
{
"product_id": "prod_01ABC123",
"session_id": "optional-for-anonymous"
}product_id(required): The product ID that was viewedsession_id(optional): For anonymous users. Omit if customer is logged in (uses auth token)
Headers: Include Authorization: Bearer <token> for logged-in customers.
GET /store/product-views/recent
Get recently viewed product IDs.
Query params:
limit(optional): Max products to return (default: 20, max: 50)session_id(optional): For anonymous users. Omit if customer is logged in
Response:
{
"product_ids": ["prod_01ABC123", "prod_01DEF456", ...]
}Storefront Integration
Recording a view (product page)
// When customer lands on product page
await fetch(`${MEDUSA_URL}/store/product-views`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(token && { Authorization: `Bearer ${token}` }),
},
body: JSON.stringify({
product_id: product.id,
...(!token && { session_id: getOrCreateSessionId() }),
}),
})Displaying recently viewed
const res = await fetch(
`${MEDUSA_URL}/store/product-views/recent?limit=10` +
(!token ? `&session_id=${getOrCreateSessionId()}` : ""),
{
headers: token ? { Authorization: `Bearer ${token}` } : {},
}
)
const { product_ids } = await res.json()
// Fetch full product details via /store/products?ids=...Publishing to npm
Create an npm account at npmjs.com if you don't have one.
Login from the terminal:
npm loginBuild and publish:
cd medusa-plugin-recently-view npm run build npm publishFor a scoped package (e.g.
@iulia_cyber/medusa-plugin-recently-view):npm publish --access publicVersion updates for future releases:
npm version patch # 0.0.1 -> 0.0.2 npm version minor # 0.0.1 -> 0.1.0 npm publish
License
MIT
