PageSenseUserContext represents a visitor participating in FullStack A/B Testing. It encapsulates the visitor identifier along with optional custom attributes that are used during experiment evaluation, audience targeting, variation assignment, and goal tracking. A PageSenseUserContext instance should be created for every unique visitor and reused across all SDK APIs during the visitor's request lifecycle. This ensures consistent experiment evaluation and goal attribution. The same PageSenseUserContext object is passed to:
activate_experiment()
get_variation_name()
track_goal()
By using a single user context across all SDK APIs for each visitor, the SDK can consistently evaluate experiments, retrieve variation assignments, and associate goal conversions with the correct visitor.
Constructor
PageSenseUserContext(
user_id: str,
user_attributes: dict[str, str] = None
)
Parameters
Parameter | Type | Required | Description |
user_id | str | Yes | Unique identifier of the visitor. The SDK uses this identifier for deterministic visitor bucketing, ensuring sticky variation assignment across multiple requests. |
user_attributes | dict[str, str] | No | Collection of custom visitor attributes used for audience targeting and visitor segmentation. |
Return Value
Type | Description |
PageSenseUserContext | Represents the visitor and associated attributes used by the SDK. |
Usage Example
from pagesense import PageSenseUserContext
# Create visitor attributes
user_attributes = {
"Browser": "Chrome",
"Device": "Desktop",
"OS": "Windows 11",
"Country": "India"
}
# Create the user context
user_context = PageSenseUserContext(
user_id="USER_001",
user_attributes=user_attributes
)
User Attributes
User attributes provide additional information about the visitor and are primarily used for audience targeting. For example, experiments can target visitors based on attributes such as:
Browser
Device
Operating System
Country
Membership Tier
Subscription Plan
Customer Type
Application Version
Audience Targeting
During experiment evaluation, the SDK compares the visitor attributes available in the PageSenseUserContext against the audience targeting rules configured for the experiment. Only visitors satisfying the configured targeting conditions are considered eligible to participate in the experiment. For example, an experiment may target:
Best Practices
Use a stable and immutable visitor identifier. As the variation assignment is based on the MurmurHash algorithm, changing the visitor identifier may result in the visitor being assigned to a different variation.
Reuse the same PageSenseUserContext instance across all SDK API calls for visitor.
Provide all available targeting attributes before calling Activate Experiment or Track Goal API functions.
Keep attribute names consistent for every visitor being tracked.
Avoid changing the visitor identifier between requests for the same user.