Middlewares
Monetate
🧭 Overview
The monetate.global.ts middleware handles page tracking and data events for Monetate and Algolia. It ensures that page views, product views, and other interactions are pushed to the Monetate tracking system based on route metadata and path patterns.
📄 Code Summary
This middleware runs only on the client side and is responsible for:
- Detecting page types like home, category, product, etc.
- Sending view and event data to Monetate and Algolia.
- Handling safe routes like
/trolley,/account/home, and/search. - Automatically identifying taxonomy/category pages.
🧩 Key Constants
SAFE_ROUTES
Defined paths to detect common route types:
const SAFE_ROUTES = {
trolley: "/trolley",
orderConfirmation: "/order-confirmation",
myAccount: "/account/home",
categories: "/categories",
search: "/search",
};
PAGE_TYPE_MAPPING
Static route mapping to page types understood by Monetate:
const PAGE_TYPE_MAPPING = {
"/": "main",
"/login": "login",
"/checkout": "checkout",
"/register": "register",
"/contact": "contact",
// ...and more
} as const;
🔧 Behavior Summary
| Condition | Event / Action | Page Type |
|---|---|---|
layout === product | Sends product view events | product |
to.path in PAGE_TYPE_MAPPING | Maps route to known Monetate page type | mapped type |
to.path === '/' | Marks homepage view | main |
to.path includes /categories | Sets as taxonomy category view | taxonomy |
Matches /x/x/c123 pattern | Sends PLP view event | taxonomy |
Includes /trolley, /search, etc. | Sends respective tracking events | contextual |
📝 Notes
- Works only on
process.client(browser-side only). - Calls methods from
monetateService,useAlgoliaEvent, and utility helpers. - Uses
window.monetateQfor queuing and sending tracking events.
✅ Example Usage
// Inside your layout/page:
definePageMeta({
middleware: ["monetate"],
});