To create field functions, head over to https://app.datatrics.com/project/fieldfunctions.
When creating a new field function, make sure to give it a descriptive name without no-break spaces.
In the code editor, you can create functions with vanilla Javascript.
The best way to explain how you need to use it, is with an example case. The case below is with two visitors, one is a b2c visitor, the other one is a b2b visitor.
Two different visitors:
Consumer
customertype: b2c
Business
customertype: b2b
Both see the following two products recommended to them:
Product 1:
name: Headset
price: 100
b2bdiscountperc: 20
Product 2:
name: Microphone
price: 50
b2bdiscountperc: 50
The recommendation is shown by a Datatrics touchpoint, it has the following HTML code:
Touchpoint:
<ul>
{{#each items}} itemcount='2'
<li>{{name}}<br>
€ {{#fieldfunction discountfunction itemname=name price=price discount=b2bdiscountperc}}</li>
{{/each}}
</ul>
The code above contains the helper #fieldfunction, here it is without the other code:
{{#fieldfunction discountfunction itemname=name price=price discount=b2bdiscountperc}}
Field function:
The field function itself has the following JavaScript code (same code as the screenshot above):
if (data.profile.customertype == 'b2b') {
output(data.price * ((100 - data.discount) / 100));
} else {
output(data.price)
}
Based on the profile field “customertype”, both visitors get a different output:
Output for consumer:
Headset
€100Microphone
€50
Output for business:
Headset
€80Microphone
€25
// End of the example
How to use in your template:
{{#fieldfunction discountfunction fieldfunctiondataname=brand}}
Content item that has a field called “brand” with the value “Nike”:
name: Cool Shoe
price: 150
brand: Nike
How to use it in field functions:
data.fieldfunctiondataname
(it contains Nike for the example above)
You can use all content item fields to send along with the #fieldfunction helper to use in your field function, example:
{{#fieldfunction discountfunction itemname=name price=price specialprice=special_price itembrand=brand}}
You can also use all profile fields in the JavaScript of your field function, example:
if (data.profile.name == 'Christiaan Proper') {
output(data.price * 0.5);
} else {
output(data.price)
}