User attributes
This tutorial covers the steps necessary to save data from a tracker into user attributes and then load that data when new instances of the same tracker or different tracker with the same metrics.
Steps
- Define a user schema
- Create a tracker with a metric that will get stored
- Create a script that will map the metric to a user attribute
- Configure a trigger that connects the tracker to the script
- Configure the tracker (same or different) to automatically load the metric when a new dispatch is created
Details
Define a user schema
Under Admin > Schema create a schema for entity graph:user
that will hold user attributes. The following is an example schema that matches the examples.
It is a best practice to structure the schema so that it can be extended. In this example there is a tracking top level attribute for data that will change based on dispatch entries with two attributes below it.
{
"title": "Graph",
"type": "object",
"properties": {
"tracking": {
"type": "object",
"title": "Tracking",
"properties": {
"weight": {
"title": "Weight",
"type": "number",
"$template": "{numeral:0,0} kg"
},
"height": {
"title": "Height",
"type": "number",
"$template": "{numeral:0,0} cm"
}
}
}
}
}
Create a tracker
Create a tracker that has the appropriate metrics. In this example we have metrics for height and weight. These are configured as conversion metrics but the type of metric does not matter.
{
"title": "Body metrics",
"type": "object",
"properties": {
"weight": {
"type": "number",
"title": "Weight",
"$capture": {
"as": "convert",
"unit": "mass"
},
"$display": {
"as": "convert"
}
},
"height": {
"type": "string",
"title": "Height",
"$capture": {
"as": "convert",
"unit": "length"
},
"$display": {
"as": "convert"
}
}
}
}
Script to map the metric
There are a few attribute functions that will work but the simplest is to use DataMapContext; the context here is the dispatch creation that triggers the script.
core.DataMapContext(
{from="$.weight", to="$.tracking.weight"},
{from="$.height", to="$.tracking.height"}
)
Configure the trigger
In Admin > Triggers create a new trigger with:
- Trigger kind = tracker
- Trigger life cycle = create
- Trigger handle = the tracker handle value
- Behavior kind = script
- Behavior handle = the script handle value
Configure the tracker
To load the data into the tracker when a new dispatch is created it should be configured with "fetchData" enabled and then a "$ref" with the path into the user schema. The paths use JSON path notation where "$." indicates the root of the JSON object.
The tracker can be the same tracker or a different tracker.
{
"title": "Body metrics",
"type": "object",
"$options": {
"fetchData": "yes"
},
"properties": {
"weight": {
"type": "number",
"title": "Weight",
"$capture": {
"as": "convert",
"unit": "mass"
},
"$ref": "$.tracking.weight",
"$display": {
"as": "convert"
}
},
"height": {
"type": "string",
"title": "Height",
"$capture": {
"as": "convert",
"unit": "mass"
},
"$display": {
"as": "convert"
},
"$ref": "$.intake.height"
}
}
}