npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

vendure-plugin-recently-viewed

v1.0.2

Published

A Vendure plugin to track recently viewed products.

Readme

Recently Viewed Plugin

A Vendure plugin that tracks and manages recently viewed products for customers. This plugin provides GraphQL queries and mutations for both shop and admin APIs to manage customer product browsing history.

Features

  • Track Product Views: Automatically or manually record when customers view products
  • Shop API: Customers can view, create, and delete their recently viewed products
  • Admin API: Administrators can query recently viewed products for any customer
  • Automatic Updates: If a product variant is viewed again, its lastViewedAt timestamp is updated
  • Pagination Support: Full pagination and filtering support via RecentlyViewedListOptions

Installation

npm install vendure-plugin-recently-viewed

Configuration

Add the plugin to your Vendure config:

import { VendureConfig } from '@vendure/core';
import { RecentlyViewedPlugin } from 'vendure-plugin-recently-viewed';

export const config: VendureConfig = {
    // ... other config
    plugins: [
        RecentlyViewedPlugin.init({}),
        // ... other plugins
    ],
};

Database Migration

This plugin adds a new RecentlyViewed entity to your database. After adding the plugin to your config, you need to generate and run a database migration:

# Generate the migration
npm run migration:generate recently-viewed-plugin

# Run the migration
npm run migration:run

Make sure your Vendure config has migrations enabled:

export const config: VendureConfig = {
    // ... other config
    dbConnectionOptions: {
        // ... other options
        migrations: [
            /* your migrations */
        ],
        migrationsRun: false, // Set to true to run migrations automatically on startup
    },
};

Usage

Shop API

The plugin extends the Shop API with the following operations:

Query Recently Viewed Products

Retrieve the current customer's recently viewed products:

query {
    recentlyViewedProducts(options: { take: 10 }) {
        items {
            id
            productVariant {
                id
                name
                product {
                    id
                    name
                    slug
                }
            }
            lastViewedAt
            createdAt
        }
        totalItems
    }
}

Create Recently Viewed Record

Record that the current customer has viewed a product variant:

mutation {
    createRecentlyViewedProduct(productVariantId: "123") {
        id
        productVariant {
            id
            name
        }
        lastViewedAt
    }
}

If a record already exists for the customer and product variant, the lastViewedAt timestamp will be updated instead of creating a duplicate.

Delete Recently Viewed Product

Remove a specific product from the customer's recently viewed list:

# Delete by record ID
mutation {
    deleteRecentlyViewedProduct(id: "456") {
        result
        message
    }
}

# Or delete by product variant ID
mutation {
    deleteRecentlyViewedProduct(productVariantId: "123") {
        result
        message
    }
}

Delete All Recently Viewed Products

Clear the entire recently viewed history for the current customer:

mutation {
    deleteAllRecentlyViewedProducts {
        result
        message
    }
}

Admin API

The Admin API provides a query to view recently viewed products for any customer:

query {
    recentlyViewedProducts(customerId: "789", options: { take: 20, sort: { lastViewedAt: DESC } }) {
        items {
            id
            productVariant {
                id
                name
            }
            lastViewedAt
        }
        totalItems
    }
}

Frontend Integration Example

Here's how you might integrate this plugin in your storefront:

// Track product view when customer visits a product page
async function trackProductView(productVariantId: string) {
    const mutation = gql`
        mutation CreateRecentlyViewed($productVariantId: ID!) {
            createRecentlyViewedProduct(productVariantId: $productVariantId) {
                id
            }
        }
    `;

    await apolloClient.mutate({
        mutation,
        variables: { productVariantId },
    });
}

// Display recently viewed products
async function getRecentlyViewed() {
    const query = gql`
        query GetRecentlyViewed {
            recentlyViewedProducts(options: { take: 8 }) {
                items {
                    productVariant {
                        id
                        name
                        featuredAsset {
                            preview
                        }
                        product {
                            slug
                        }
                    }
                }
            }
        }
    `;

    const { data } = await apolloClient.query({ query });
    return data.recentlyViewedProducts.items;
}

Permissions

All Shop API operations require the customer to be authenticated and use the Permission.Owner decorator to ensure customers can only access their own data.

Database Schema

The plugin creates a RecentlyViewed entity with the following structure:

  • id: Unique identifier
  • customerId: Reference to the customer
  • productVariantId: Reference to the product variant
  • lastViewedAt: Timestamp of the most recent view
  • createdAt: Timestamp of initial creation
  • updatedAt: Timestamp of last update

API Reference

Shop API

  • Query: recentlyViewedProducts(options: RecentlyViewedListOptions): RecentlyViewedList!
  • Mutation: createRecentlyViewedProduct(productVariantId: ID!): RecentlyViewed!
  • Mutation: deleteRecentlyViewedProduct(id: ID, productVariantId: ID): DeletionResponse!
  • Mutation: deleteAllRecentlyViewedProducts: DeletionResponse!

Admin API

  • Query: recentlyViewedProducts(customerId: ID!, options: RecentlyViewedListOptions): RecentlyViewedList!

License

MIT