Skip to main content

UserActivity

Record what your users and guests do, and turn it into personalized experiences.


The UserActivity module tracks activity events for the current user or guest: product views, page views, searches, cart and wishlist actions, purchases, and ratings.

These events feed OneEntry's recommendation engine, which powers personalization blocks such as personal recommendations, recently viewed, repeat purchase, and trending.

Quickstart​

Initialize the module from defineOneEntry:


const { } = defineOneEntry(
, {
}
);

Record a product rating (works for an authenticated user or a guest):

// Track that the visitor rated a product.
const ok = await UserActivity.trackUserActivity({
type: "product_rating",
productId: 12345,
meta: { rating: 5 },
});

console.log(ok); // true on success

trackUserActivity() takes a single body object and resolves to true on success. If the API refuses the event (for example, an unknown type), it resolves to an IError, or throws when isShell is false. Track events as they happen, but don't block rendering on the network call.

Users and guests​

Tracking doesn't require authentication. Events belong to the authenticated user (see AuthProvider) or, without a session, to the current guest. For guests, the SDK sends an x-guest-id header so events are attributed to the right anonymous visitor. In the browser the SDK generates and persists a guest id automatically; on the server, pass a per-visitor guestId that is a UUID v4 (for example, from crypto.randomUUID()), and reuse it for that visitor. The API replaces any other value with a new random id on every request, so the events would not add up to one visitor. See Guest mode for details.

What is tracked automatically​

The API records some events on its own when the request carries a user session or a guest id:

  • product_view when a product is fetched with getProductById()
  • page_view (or category_view for a catalog page) when a page is fetched with getPageByUrl() or getPageById(), for guests only. Page views of a signed-in user aren't attributed to the user.
  • product_add_to_cart, product_remove_from_cart, product_add_to_wishlist and product_remove_from_wishlist when the cart or wishlist changes through the Users cart and wishlist methods
  • product_purchase after a successful payment

Don't send these again with trackUserActivity(): manual events aren't deduplicated, so they're counted twice. Use the method for events the API can't see, such as search (product search methods don't record it), product_rating, or a product view rendered from cached data.

Event body​

The event type is one of a fixed set of values (some of them are recorded automatically): product_view, page_view, category_view, search, product_add_to_cart, product_remove_from_cart, product_add_to_wishlist, product_remove_from_wishlist, product_purchase, product_rating.

Send the field that matches the action: productId for product events, pageId for page and category views, and query for searches.

meta is a free-form object for anything else you want to attach to an event for your own analytics, such as the source block, an A/B test variant, or scroll depth.


Methods​

MethodDescription
trackUserActivity()Record a user or guest activity event