# Segmentify Integration
> Documentation for the Segmentify Integration library.
import { Card, CardGrid, LinkCard } from '@astrojs/starlight/components';
## Core Features
Every event, payload, and config option is fully typed. Catch integration
errors at build time, not in production.
Page views, product views, basket ops, purchases, wishlists, and
gamification — all ready to go out of the box.
Define URL patterns or use external object matching. The library detects
page types and fires events automatically.
Built-in History API interception re-detects pages on every route change.
Works with React, Vue, Angular, and Next.js.
---
## Explore the Documentation
---
# Introduction
> This guide will help you set up the Segmentify integration library in your project.
## Prerequisites
- Node.js >= 20
- A Segmentify account with an API key
## Quick Start
### 1. Install the package
```bash
npm install @segmentify/integration
# or
pnpm add @segmentify/integration
# or
yarn add @segmentify/integration
```
### 2. Initialize the library
```typescript
import { Initializer, type SegmentifyConfig } from '@segmentify/integration';
const AppConfig = {
domain: 'segmentify.com',
apiKey: '',
language: '',
currency: '',
segmentifyApiUrl: '',
segmentifyPushUrl: '',
doNotPersonalize: {
cb: () => false,
},
debug: 'DEBUG',
spa: false,
ga: {
enabled: true,
ga4: true,
},
searchify: true,
} as const satisfies SegmentifyConfig;
new Initializer({
...AppConfig,
pageMap: {
'Home Page': {
type: 'Home Page',
category: 'Home Page',
pattern: ['/'],
},
},
});
```
### 3. Start tracking events
```typescript
import { SegmentifyEventManager } from '@segmentify/integration';
// Track a page view
SegmentifyEventManager().PageView({
name: 'PAGE_VIEW',
});
// Track a product view
SegmentifyEventManager().ProductView({
productId: 'PROD-123',
title: 'Amazing Product',
price: 99.99,
category: 'electronics',
});
```
## As a Script Tag
For non-bundled environments, include the UMD build directly:
```html
```
## Build Outputs
The package ships with multiple formats:
| File | Format | Use Case |
| ------------------ | ----------------------- | --------------------------------------- |
| `dist/index.es.js` | ES Module | Modern bundlers (Vite, webpack, Rollup) |
| `dist/index.js` | CommonJS | Node.js / legacy bundlers |
| `dist/index.d.ts` | TypeScript Declarations | Type checking and IDE support |
## What's Next?
- [Configuration](/02-configuration) — Full configuration reference
- [Events](/05-events) — Complete guide to event tracking
---
# Configuration
> Simple configuration example.
## Basic Configuration
```typescript
import { Initializer } from '@segmentify/integration';
new Initializer({
domain: 'your-domain.com',
apiKey: 'your-api-key',
language: 'EN',
currency: 'USD',
spa: false,
segmentifyApiUrl: '//your-api.segmentify.com',
});
```
## Configuration Reference
| Property | Type | Required | Description |
| ------------------- | --------------------------------------------------- | -------- | ---------------------------------------------------------------------- |
| `domain` | `string` | Yes | Your website domain |
| `apiKey` | `string` | Yes | Your Segmentify API key |
| `language` | `string` | Yes | Language code (e.g., `'EN'`, `'TR'`) |
| `currency` | `string` | Yes | Currency code (e.g., `'USD'`, `'EUR'`) |
| `spa` | `boolean` | Yes | Enable [SPA mode](/04-spa-mode) for client-side routing |
| `segmentifyApiUrl` | `string` | Yes | Segmentify API endpoint URL |
| `segmentifyPushUrl` | `string` | No | Custom push endpoint URL |
| `debug` | `boolean \| 'DEBUG' \| 'INFO' \| 'WARN' \| 'ERROR'` | No | Enable debug logging — see [Logger](/11-logger) |
| `searchify` | `boolean` | No | Enable Searchify integration |
| `ga` | `object` | No | Google Analytics config (see below) |
| `pageMap` | `PageMap` | No | Page type mapping — see [Page Mapping](/03-page-mapping) |
| `doNotPersonalize` | `object` | No | Opt-out callback (see below) |
| `allPageLogic` | `object` | No | Global pre/post execute — see [Page Mapping](/03-page-mapping) |
| `user` | `User` | No | Pre-set user info — see [User Identification](/06-user-identification) |
## Google Analytics
```typescript
new Initializer({
// ...required fields
ga: {
enabled: true,
ga4: true, // true for GA4, false for Universal Analytics
},
});
```
## Do Not Personalize
Provide a callback that returns `true` to opt out of personalization:
```typescript
new Initializer({
// ...required fields
doNotPersonalize: {
cb: () => !hasConsent(), // e.g., check GDPR preferences
},
});
```
## Initialization Behavior
- Calling `new Initializer()` twice logs a warning and skips the second call.
- The Segmentify SDK script is loaded asynchronously after DOM ready.
- Any events queued from the previous page are drained automatically.
## See Also
- [Page Mapping](/03-page-mapping)
- [SPA Mode](/04-spa-mode)
- [Logger](/11-logger)
---
# 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.
## How It Works
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
## Basic Page Map
```typescript
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 Types
| Page Key | Constant | Description |
| ----------------------- | ---------------------------------- | ---------------------- |
| `Home Page` | `PAGE_TYPES.HOME_PAGE` | Site homepage |
| `Product Page` | `PAGE_TYPES.PRODUCT_PAGE` | Product detail pages |
| `Category Page` | `PAGE_TYPES.CATEGORY_PAGE` | Category listing pages |
| `Brand Page` | `PAGE_TYPES.BRAND_PAGE` | Brand-specific pages |
| `Search Page` | `PAGE_TYPES.SEARCH_PAGE` | Search results |
| `Empty Search Page` | `PAGE_TYPES.EMPTY_SEARCH_PAGE` | No-results search |
| `Basket Page` | `PAGE_TYPES.BASKET_PAGE` | Shopping cart |
| `Checkout Success Page` | `PAGE_TYPES.CHECKOUT_SUCCESS_PAGE` | Order confirmation |
| `Register` | `PAGE_TYPES.REGISTER_PAGE` | User registration |
| `Login` | `PAGE_TYPES.LOGIN_PAGE` | User login |
| `Logout` | `PAGE_TYPES.LOGOUT_PAGE` | User logout |
| `404 Page` | `PAGE_TYPES.FOUR_O_FOUR_PAGE` | Not found |
| `Blog Page` | `PAGE_TYPES.BLOG_PAGE` | Blog content |
| `Profile Page` | `PAGE_TYPES.PROFILE_PAGE` | User profile |
| `Unknown Page` | `PAGE_TYPES.UNKNOWN_PAGE` | Unmatched pages |
## Pattern Matching
Match pages using glob-style URL patterns. Multiple patterns per page — matches if **any** pattern hits:
```typescript
'Product Page': {
type: 'Product Page',
pattern: ['/product/*', '/p/*', '/item/*'],
}
```
## External Object Matching
For SPAs where URL patterns aren't enough, determine the page type programmatically:
```typescript
'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.
## Pre/Post Execute Functions
`preExecuteFunction` runs **before** the tracking event — returns page-specific data:
```typescript
'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 Type | Expected Return Type |
| ----------------------- | --------------------------------------------- |
| `Product Page` | `ProductViewPayload` |
| `Checkout Success Page` | `PurchasePayload` |
| `Basket Page` | `BasketViewPayload` |
| `Register` | `RegisterPayload` |
| `Login` | `Omit` |
| `Logout` | `Omit` |
`postExecuteFunction` runs **after** the event completes:
```typescript
'Product Page': {
type: 'Product Page',
pattern: ['/product/*'],
postExecuteFunction: () => {
console.log('Product page tracked');
},
}
```
## Additional Options
| Option | Type | Description |
| ----------------- | ------------------------------------------- | ----------------------------- |
| `category` | `PageKey` | Page category override |
| `brand` | `string \| () => string \| Promise` | Brand name or getter |
| `subCategory` | `string \| () => string \| Promise` | Subcategory or getter |
| `params` | `object \| () => object \| Promise