Trait - User Property
Trait user properties selects the last observed trait
value of an identify
event. It is the most basic and commonly used user property type. They can be used to render individual values, or to render arrays and objects.
Example Use Case - Plan
As an example, imagine that you’re running a SAAS business, where users can be subscribed to one of several plans. You can create a plan
trait.
{
"type": "identify",
"userId": "1234",
"traits": {
"plan": "premium",
"name": "John Doe",
"email": "[email protected]"
}
}
This property would then be available to render in a template.
You are subscribed to the {{ user.plan | default: "free" }} tier plan.
Example Use Case - Recommendations
Let’s say you’re running an online store, and you want to show recommendations to a user. You have a set of recommendations for each user that you’ve pre-calculated. You can create a recommendations
trait, and use it to render a list of products.
{
"type": "identify",
"userId": "1234",
"traits": {
"recommendations": [
{
"id": "123",
"name": "Laundry Detergent"
},
{
"id": "456",
"name": "Toothpaste"
}
],
"name": "John Doe"
}
}
{% for product in user.recommendations | default: empty %}
<mj-text><a>href="https://example.com/product/{{ product.id }}">{{ product.name }}</a></mj-text>
{% endfor %}
This is a demonstration of how Trait
user properties can be used to render arrays and objects in addition to strings.
Path Syntax
Paths use JSON path syntax as documented in the JSONPath specification, with one deviation.
If the path starts with a $
, it is treated as a JSONPath expression. Otherwise, it is automatically prefixed with $.
. So, for example, the path $.store.book[0].title
and store.book[0].title
are equivalent.
Prefixing the path with $
is optional, but it is useful when your path contains whitespace or other characters that are not valid in JSONPath expressions. For example, the path $['property name with spaces']
is valid, but ['property name with spaces']
is not.