User Context

The user context serves as a unique identifier for individual users and plays a critical role in ensuring consistent feature rollouts across sessions and devices. Typically represented as a dictionary, the user context includes an _id key that uniquely identifies the user.

In addition to the user ID, the context can incorporate various attributes to support advanced targeting and segmentation strategies. These may include:

  • custom-variables: User-specific data points for personalized experiences.
  • user-agent: Information about the user's device, browser, or operating system.
  • ip-address: Location-based data to enable geo-targeting.

By leveraging these attributes, organizations can deliver precisely targeted features, maintain personalization consistency, and conduct granular experimentation for improved user engagement and performance analysis.

📘

Important Note

The user context attributes differ from the attributes set using the setAttribute API.

  • User Context Attributes: Primarily used for targeting purposes during feature rollouts and experimentation. These attributes help determine which users are eligible for specific features or variations.
  • setAttribute API Attributes: Specifically designed for post-segmentation analysis, allowing you to segment and analyze experiment results based on defined user characteristics.

🚧

Current Limitation

VWO does not support using user context attributes directly as post-segmentation filters in the reporting section of VWO applications. For post-segmentation, it is recommended to rely on attributes set via the setAttribute API.

Usage

# Define the user context object to identify and provide user-specific details
user_context = {
    'id': 'unique_user_id',  # Unique identifier for the user (required)
    'custom_variables': {    # Custom attributes for the user (optional)
        'age': 25,           # Example: User's age
        'location': 'US'     # Example: User's location
    },
    'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',  # User's browser and OS info (optional)
    'ip_address': '1.1.1.1'  # User's IP address (optional)
}

# The same user context can be used across different APIs. For example -

# Returns a flag object which can be used to get flag's status or variable(s)
flag = vwo_client.get_flag('feature_key', user_context)

# Track a metric conversion for the specified event-name
vwo_client.track_event('event_name', user_context, {'userType': 'paid'})

# Send a user attribute to VWO
vwo_client.set_attribute('attribute_key', 'attribute_value', user_context)

User Context keys

ParameterTypeDescription
id
Required
stringUnique user ID for the current user. 'id' is the only required property in userContext. This ID should be a unique identifier for the current user which you need to pass to VWO. Read more here.
user_agent
Optional
stringThe userAgent object for the current user can be used for targeting & segmentation.
ip_address
Optional
stringIP Address of the current user can be used for targeting & segmentation.
custom_variables
Optional
dictAny additional details of the current user that you want to push to VWO can be added here as key-value pairs and can be used for targeting & pre-segmentation.

📘

Note

You need to pass Gateway Service configuration while initializing the SDK for targeting (pre-segmentation using user-agent or IP-address-related segments).