Personalization using Jinja

Useful examples for message personalization

Jinja allows you to personalize different types of messages (chat, emails, pop-ups, Telegram, Webhook) and chatbot messages using user properties and event attributes. You can also use Jinja syntax to set more complex conditions for your audience filters in Triggered messages and Message sequences.

Syntax

The most commonly used are Jinja expressions, which are called using {{ ... }} tags, and operators, which are called using {% ... %}  tags.

Learn more about Jinja syntax.

Available expressions in Dashly

For personalizing message texts and audience filters

{{ user[’$property name’] }}  — a system user property. For example, a username.

{{ user[’property name’] }}  — your own user properties that you created manually or automatically transferred to Dashly.

{{ event[’attribute name’] }}  — an attribute of the event that triggered the message.

get_last_events('event name')[0]  — a function that is used to get a chronological list of a specific event. When we add [0]  at the end, it means that we take the most recent event. Using [2] means that we select the third event from the end. Thus, the number in square brackets indicates how far back in the list of events we go.

Extra expressions for webhooks

{{ event.as_json }}  — an event with all its attributes that triggered the webhook

{{ user.as_json }} — all user data from the user card (all recorded properties and events)

Examples of Jinja usage

Personalization of messages based on attributes of the executed event

{% set lastEvent = get_last_events('Event name')[0] %}
{% set name = lastEvent.props['Attribute 1'] %}
{% set site = lastEvent.props['Attribute 2'] %}
Hello, {{ name }}! We got your request for the {{ site }} audit. We'll call you back shortly.

This Jinja code example does the following:

1. Get the latest event:

{% set lastEvent = get_last_events('Event name')[0] %}

Here the get_last_events function  is called with the argument 'Event Name'. This function receives a list of events, and [0] takes the last element from this list. This element is stored in the lastEvent variable. lastEvent is an object containing information about the last event.

2. Extract attributes from an event:

{% set name = lastEvent.props['Attribute 1'] %}
{% set site = lastEvent.props['Attribute 2'] %}

Here, the values of two attributes are extracted from the lastEvent object. These values are stored in the variables name and site . Attribute 1 and Attribute 2 get replaced with the actual values that are contained in your event.

3. Form the message text:

Hello, {{ name }}! We got your request for the {{ site }} audit. We'll call you back shortly.

This is a string that uses the substitution of the name and site variables. As a result, a personalized message text is formed: we greet the user by name and inform them that they have left a request for an audit of a specific site.

A similar article about personalization using event attributes

Message with conditions

{% if user['$name'] is defined %}
    Hello, {{ user['$name'] }}! Thank you for your request.
{% else %}
    Thank you for your request.
{% endif %}

This Jinja code example does the following:

1. Check for the presence of a username:

{% if user['$name'] is defined %}

This checks if the property '$name' is defined in the user object. This means that the code checks if the key '$name' exists and has a value.

2. Generate personalized text:

Hello, {{ user['$name'] }}! Thank you for your request.

If '$name' exists and has a value, a personalized greeting is displayed, where
{{ user['$name'] }} is replaced with the actual username. For example, if '$name' is "John", the message will be: "Hello, John! Thank you for your request."

3. Alternative text:

{% else %}
    Thank you for your request.
{% endif %}

If '$name' is undefined or has no value, a generic message is displayed: "Thank you for your request."


Other articles on Jinja:

Basic Jinja Syntax
Data Structure in Jinja
Filters in Jinja

Powered by