Skip to content

Page Mapping

Page mapping defines how URLs on your site map to Segmentify page types. When configured, the library automatically detects the current page and sends the appropriate tracking event.

View as Markdown llms.txt

When you provide a pageMap in your config, the library:

  1. Matches the current URL against your configured patterns (or calls externalObject)
  2. Determines the page type
  3. Runs preExecuteFunction to gather page-specific data
  4. Sends the tracking event
  5. Runs postExecuteFunction after tracking
import { Initializer } from '@segmentify/integration';
import type { PageMap } from '@segmentify/integration';
const pageMap: PageMap = {
'Home Page': {
type: 'Home Page',
pattern: ['/'],
},
'Product Page': {
type: 'Product Page',
pattern: ['/product/*', '/p/*'],
},
'Category Page': {
type: 'Category Page',
pattern: ['/category/*'],
},
};
new Initializer({
// ...required fields
pageMap,
});
Page KeyConstantDescription
Home PagePAGE_TYPES.HOME_PAGESite homepage
Product PagePAGE_TYPES.PRODUCT_PAGEProduct detail pages
Category PagePAGE_TYPES.CATEGORY_PAGECategory listing pages
Brand PagePAGE_TYPES.BRAND_PAGEBrand-specific pages
Search PagePAGE_TYPES.SEARCH_PAGESearch results
Empty Search PagePAGE_TYPES.EMPTY_SEARCH_PAGENo-results search
Basket PagePAGE_TYPES.BASKET_PAGEShopping cart
Checkout Success PagePAGE_TYPES.CHECKOUT_SUCCESS_PAGEOrder confirmation
RegisterPAGE_TYPES.REGISTER_PAGEUser registration
LoginPAGE_TYPES.LOGIN_PAGEUser login
LogoutPAGE_TYPES.LOGOUT_PAGEUser logout
404 PagePAGE_TYPES.FOUR_O_FOUR_PAGENot found
Blog PagePAGE_TYPES.BLOG_PAGEBlog content
Profile PagePAGE_TYPES.PROFILE_PAGEUser profile
Unknown PagePAGE_TYPES.UNKNOWN_PAGEUnmatched pages

Match pages using glob-style URL patterns. Multiple patterns per page — matches if any pattern hits:

'Product Page': {
type: 'Product Page',
pattern: ['/product/*', '/p/*', '/item/*'],
}

For SPAs where URL patterns aren’t enough, determine the page type programmatically:

'Product Page': {
type: 'Product Page',
externalObject: () => {
const currentRoute = getAppRoute();
return currentRoute.name === 'product' ? 'Product Page' : null;
},
}

If externalObject returns null, the matcher falls through to the next page config.

preExecuteFunction runs before the tracking event — returns page-specific data:

'Product Page': {
type: 'Product Page',
pattern: ['/product/*'],
preExecuteFunction: async () => {
const product = await getProductFromPage();
return { productId: product.id, title: product.name, price: product.price };
},
}

The return type is inferred from the page type:

Page TypeExpected Return Type
Product PageProductViewPayload
Checkout Success PagePurchasePayload
Basket PageBasketViewPayload
RegisterRegisterPayload
LoginOmit<RegisterPayload, 'isLogin'>
LogoutOmit<RegisterPayload, 'isLogin' | 'email'>

postExecuteFunction runs after the event completes:

'Product Page': {
type: 'Product Page',
pattern: ['/product/*'],
postExecuteFunction: () => {
console.log('Product page tracked');
},
}
OptionTypeDescription
categoryPageKeyPage category override
brandstring | () => string | Promise<string>Brand name or getter
subCategorystring | () => string | Promise<string>Subcategory or getter
paramsobject | () => object | Promise<object>Additional parameters
emptySearchPagebooleanFlag for empty search results
const pageMap: PageMap = {
'Home Page': {
type: 'Home Page',
pattern: ['/'],
},
'Product Page': {
type: 'Product Page',
pattern: ['/product/*'],
preExecuteFunction: () => ({
productId:
document
.querySelector('[data-product-id]')
?.getAttribute('data-product-id') || '',
title: document.querySelector('h1')?.textContent || '',
price: parseFloat(document.querySelector('.price')?.textContent || '0'),
}),
},
'Basket Page': {
type: 'Basket Page',
pattern: ['/cart', '/basket'],
preExecuteFunction: () => ({
totalPrice: getCartTotal(),
productList: getCartItems(),
}),
},
'Checkout Success Page': {
type: 'Checkout Success Page',
pattern: ['/order/success*'],
preExecuteFunction: () => ({
totalPrice: getOrderTotal(),
orderNo: getOrderNumber(),
productList: getOrderItems(),
}),
},
'Search Page': {
type: 'Search Page',
pattern: ['/search*'],
},
};
  • SPA Mode — How page detection works with client-side routing