The Visitor Segments endpoint allows developers to retrieve all active segments that a specific visitor belongs to. This data can be used to deliver targeted content based on visitor behavior and characteristics.
Endpoint Details
- URL:
/wp-json/personalizewp/v1/visitor-segments - Method: POST
- Content Type: application/json
Request Parameters
The request body should be a JSON object containing the following parameter:
Parameter
Type
Required
Description
uid
string
Yes
The visitor's unique identifier
Response
The endpoint returns an array of segment records that the visitor is a member of. Each segment record contains the following fields:
Field
Type
Description
id
number
The segment's unique identifier
name
string
The segment's title
type
string
The segment's type
Example Usage
Basic Request
This example shows how to fetch all segments for the current visitor:
javascript ( async () => { const PWP = localStorage.getItem('pwptrackeduser') || ''; const UID = (PWP) ? JSON.parse(PWP).id : null;
if (UID) { const baseURL = '/wp-json/'; // This is available on frontend as "window.pwpSettings.root" const namespace = 'personalizewp/v1/'; const endpoint = 'visitor-segments';
const response = await fetch( ${baseURL}${namespace}${endpoint}, { method: "POST", cache: "no-cache", headers: { "Accept": "application/json, /;q=0.1", "Cache-Control": "no-cache, private", "Content-type": "application/json", "X-Requested-With": "XMLHttpRequest", }, body: JSON.stringify({ uid: UID }), });
if (response.ok){ const segments = await response.json(); console.log(segments); } } })();
Practical Application
Once you have retrieved the visitor's segments, you can use this information to:
- Display Targeted Content: Show different content based on the segments the visitor belongs to
- Customize Product Recommendations: Tailor product recommendations based on segment membership
- Adjust UI Elements: Change UI elements or messaging based on visitor segments
- Personalize Offers: Show special offers or promotions to specific segments
Example: Checking if a Visitor Belongs to a Specific Segment
This example shows how to check if a visitor belongs to a specific segment (e.g., "Frequent Shopper") and take action based on that information:
javascript ( async () => { const PWP = localStorage.getItem('pwptrackeduser') || ''; const UID = (PWP) ? JSON.parse(PWP).id : null;
if (UID) { const baseURL = '/wp-json/'; // Available as "window.pwpSettings.root" const namespace = 'personalizewp/v1/'; const endpoint = 'visitor-segments';
const response = await fetch( ${baseURL}${namespace}${endpoint}, { method: "POST", cache: "no-cache", headers: { "Accept": "application/json, /;q=0.1", "Cache-Control": "no-cache, private", "Content-type": "application/json", "X-Requested-With": "XMLHttpRequest", }, body: JSON.stringify({ uid: UID }), });
if (response.ok){ const segments = await response.json();
// Check if visitor belongs to "Frequent Shopper" segment const isFrequentShopper = segments.some(segment => segment.name === 'Frequent Shopper');
if (isFrequentShopper) { // Show special content or offers for frequent shoppers document.querySelector('.special-offers').style.display = 'block'; } } } })();
Common Use Cases
The Visitor Segments endpoint is particularly useful for:
- Creating personalized "Products For You" sections
- Showing different hero banners to different visitor segments
- Customizing navigation elements based on visitor interests
- Adjusting content priority based on visitor segments
- Implementing targeted calls-to-action
For a practical implementation example, see our guide on Creating a "Products For You" Block.
Notes
- Segments are dynamically calculated based on visitor behavior and characteristics
- A visitor can belong to multiple segments simultaneously
- Consider caching segment data for frequent requests to improve performance