Context Attributes
Attributes are used to pass meta-data about the user and/or the request. They
can be used later in the Web Console to create segments or audiences. They can
be set using the attribute()
or attributes()
methods, before or after the
context is ready.
- Javascript
- Python3
- React
- Swift
- Vue2
- Vue3
- Java
- Go
- .NET
- PHP
- Ruby
- Flutter/Dart
context.attribute("user_agent", navigator.userAgent);
context.attributes({
customer_time:
user.created > new Date().getTime() - 24 * 60 * 60 * 1000
? "new_customer"
: "returning_customer",
user_type: user.isInternal ? "internal" : "normal",
url: location.toString(),
user_agent: navigator.userAgent,
referrer: document.referrer,
screenName,
pageName,
country: headers["HTTP_CF_IPCOUNTRY"],
language: headers["Accept-Language"],
channel: query_params.utm_medium,
});
context.set_attribute("user_agent", req.get_header("User-Agent"))
context.set_attributes({
"customer_age": "new_customer"
})
context.attributes({
customer_time:
user.created > new Date().getTime() - 24 * 60 * 60 * 1000
? "new_customer"
: "returning_customer",
user_type: user.isInternal ? "internal" : "normal",
url: location.toString(),
user_agent: navigator.userAgent,
referrer: document.referrer,
screenName,
pageName,
country: headers["HTTP_CF_IPCOUNTRY"],
language: headers["Accept-Language"],
channel: query_params.utm_medium,
});
The attribute()
and attributes()
methods can be called both inside, and outside
of your useEffect.
context.setAttribute(name: "device", value: UIDevice.current.model)
context.setAttributes(["customer_age": "new_customer", "screen": "product"])
Attributes can be set in script.
this.$absmartly.attribute("user_agent", navigator.userAgent);
this.$absmartly.attributes({
customer_age: "new_customer",
});
Or directly in templates with the :attributes
property of the Treatment
component.
<treatment
name="exp_test_experiment"
:attributes="{ customer_age: 'returning' }"
>
<template #default="{ config, treatment, ready }">
<template v-if="ready">
<my-button v-if="treatment == 0"></my-button>
<my-button v-else-if="treatment == 1" :color="config.color"></my-button>
<my-other-button
v-else-if="treatment == 2"
:color="config.color"
></my-other-button>
</template>
<template v-else><my-spinner></my-spinner></template>
</template>
</treatment>
Attributes can be set in script.
this.$absmartly.attribute("user_agent", navigator.userAgent);
this.$absmartly.attributes({
customer_age: "new_customer",
});
Or directly in templates with the :attributes
property of the Treatment
component.
<treatment
name="exp_test_experiment"
:attributes="{ customer_age: 'returning' }"
>
<template #default="{ config, treatment, ready }">
<template v-if="ready">
<my-button v-if="treatment == 0"></my-button>
<my-button v-else-if="treatment == 1" :color="config.color"></my-button>
<my-other-button
v-else-if="treatment == 2"
:color="config.color"
></my-other-button>
</template>
<template v-else><my-spinner></my-spinner></template>
</template>
</treatment>
context.setAttribute('user_agent', req.getHeader("User-Agent"));
context.setAttributes(Map.of(
"customer_age", "new_customer"
));
context.SetAttribute("user_agent", req.GetHeader("User-Agent"));
context.SetAttributes(map[string]string{
"customer_age": "new_customer",
}
context.SetAttribute('user_agent', Request.Headers["User-Agent"]);
context.SetAttributes(new Dictionary<string, object>() {
{ "customer_age", "new_customer" }
});
$context->setAttribute('session_id', \session_id());
$context->setAttributes(
[
'customer_age' => 'new_customer'
]
);
context.set_attribute('session_id', session_id)
context.set_attributes(
'customer_age' => 'new_customer'
)
context.setAttribute("attribute", 1);
context.setAttributes(
{
"attribute": 1,
},
);
You should create audiences with the same attributes as those in the Web Console.
By default it will not enforce these conditions, but if for some reason the
treatment()
method is called for a user that doesn’t meet the correct criteria, the
Web Console will warn you about it.