Metadata
Tag and query Sessions with custom data
Overview
As your number of sessions grows, attaching metadata helps to organize your sessions based on your application’s specific needs.
Our List Sessions endpoint supports filtering sessions by status, which is helpful but not highly configurable. Session metadata allows you to attach arbitrary JSON data to sessions, which then gives you additional flexibility when querying sessions.
Creating a Session with Metadata
Metadata is attached to a session when hitting our Create Session endpoint. This metadata can be any JSON-serializable object.
This metadata is attached to the stored session object and can be queried against using the List Sessions endpoint.
Below is an example for attaching an order status to a created session. This will attach the object {"order": {"status": "shipped"}}
to the created session.
The size of the stored JSON object is limited to 512 characters. We measure
this by converting your object into a JSON string (think JSON.stringify
) and
measuring the length of the resulting string.
Querying Sessions by Session Metadata
Querying using session metadata is done via the q
query parameter on the List Sessions endpoint.
Let’s use the example object {"order": {"status": "shipped"}}
from the previous section. To query for all sessions with an order of status "shipped"
, you can use the following query:
This query contains the following components:
-
user_metadata
is known as the “base” of the query. Currently we only support theuser_metadata
base, although we’re working to support more querying bases in the future. Stay tuned! -
['order']['path']
is known as the “path” of the query. The path is used to drill into nested fields in the stored metadata object. In this case, we’re looking for an object with keys in the shape{ "order": { "status" }}
. -
'shipped'
is known as the “value” of the query. This is separated from the base and the path of the query with a:
character. The “value” field of the query is used to check strict equality of the value specified by the “path” of the query. In our case, we’re looking for an object with the exact shape{ "order": { "status": "shipped" }}
.
Note that we need to URL encode the query string to ensure that it’s properly parsed by the API.
-
%5B
is the URL encoded version of[
-
%5D
is the URL encoded version of]
-
%3A
is the URL encoded version of:
In JavaScript, you can use encodeURIComponent("user_metadata['order']['status']:'shipped'")
to encode the query string.
Below is an example of how to use this query.
This will return a list of all sessions with attached metadata in the form {"order": {"status": "shipped"}}
. If the query doesn’t match any sessions, the API will respond with an empty list []
.
Currently we only support querying by fields (no arrays) and checking value equality of string (no numbers or booleans). A quick workaround for this is to convert numbers and booleans into strings and query normally.
Was this page helpful?