Sticky MPKnowledgedrop

Liquid templating examples for email and other messages

  • 13 November 2019
  • 5 replies
  • 346 views
Liquid templating examples for email and other messages
Userlevel 5
Badge +4

Every week, we will release tips to help you get the most out of Mixpanel. Want to see more? Click here to see other #mpknowledgedrop articles.

 

If you read our Liquid Templating article and want to get better at it, then keep reading our MPKnowledgeDrop! In this post we will share Liquid Templating examples on email and other message types!

What is personalization and why should I do it?

Personalization is all about sending messages that are tailored to each unique user.

Liquid is a templating language developed by Shopify, which you can use to personalize text in your Mixpanel messages. Personalization can range from the simple variable insertion to complex iterations and substitutions.
 

Variable insertion

The most common personalization is to pull in fields like a person’s name, their city, etc. This works similarly across message types. For example, if you want to increase how many people open your emails, you might use a variable to address them by name.

Hi {{ user[‘$first_name’] }}, welcome to Mixpanel!

 

Now, what if you didn’t always track the variable $first_name for every one of your users. You can specify a default value to be used if you don’t have the person’s name.

Hi {{ user[‘$first_name’] | default:’there’ }}, welcome to Mixpanel!

 

For a user named Robert, this would read “Hi Robert, welcome to Mixpanel!”. However, if a user didn’t have a first name stored in their Mixpanel people property, it would read “Hi there, welcome to Mixpanel!”

Sometimes, you may have tracked names without capitalizing them. This is an easy fix too. You can just use the special capitalize keyword

Hi {{ user[‘$first_name’] | capitalize | default:’there’ }}, welcome to Mixpanel!

If the user’s first name is stored as “robert”, this will read “Hi Robert, welcome to Mixpanel”

Variable insertion can be used in many different parts of various messages. You can use variables in an email subject, body, reply-to address, from name, and bcc address. For example, in the message below, we’ve used the CSM’s name and email when reaching out to a new customer.

683-1573680533203.png

 

Conditionals

Conditionals are the second most common type of personalization. You may want to send the same email/SMS/push notification with differences for different groups. The most common use-case for this is language localization, where you want to send different content to different users.

In the example below, if a user named Robert who has his language set to French will see their message as “Bonjour Robert!”

{% if user['Language'] == 'spanish'  %} Hola {}! {% elsif user['Language'] == 'french' %} Bonjour {}! {% else %} Hello {}! {% endif %}

684-1573680573942.png

 

However, you may also want to divide people based by lifetime value or plan type.

In the example below, we are using conditionals to tell people what discount programs they are eligible for. In addition to text, we are also capturing a discount multiplier that we can use to figure out what their price is. The liquid syntax {% assign var_name = value %} creates a variable that can be used in subsequent parts of the text to help modify it.

{% if user[‘company_launch_year’] >= 2016 %} You may be eligible for our startup program discount!  {% assign discount_multiplier = 0.75 %} {% elsif user[‘company_type’] contains ‘non-profit’ or user[‘company_type’] contains ‘charity’ %} You may be eligible for our non-profit discounts! {% assign discount_multiplier = 0.70 %} {% else %} {% assign discount_multiplier = 1 %} {% endif %} 

 

Iteration

Liquid supports iteration through lists. This is really helpful in cases where you may have objects as user properties and need to do things like render shopping carts or set up recommendation lists.

<div>{% assign total = 0 %}{% assign foo = user["shopping_cart"] %}{% for item in foo %}{% assign temp_price = {}}} %}{% assign total = total | plus: {} %}{% endfor %}<table style="width:100%" border="1"><tbody><tr><th>Image</th> <th>Item</th><th>Price</th><th>Quantity</th><th>Total</th></tr><tr><td><img src="{}" height="200"></td><td>{}</td><td>${}</td><td>x{}</td><td>${}</td></tr><tr><td bgcolor="gray"></td><td bgcolor="gray"></td><td bgcolor="gray"></td><td bgcolor="gray"></td><td>${}</td></tr></tbody></table><div><br></div>

 

You can also set up liquid in push and SMS messages. For example, in a push message you can use the body or the custom data field to add liquid templating.

 

Here is an example of an iOS push -

{ "aps":{ "alert":{ "body":"{% if user['language'] == 'French' %} Bonjour {% elsif user['language'] == 'Spanish' %} Hola {% elsif user['language'] == 'German' %} Hallo \n Brother {% else %} Hello {% endif %}" } } }
686-1573680878334.png

 

 

These are just some common Liquid examples to get you started. Go do something cool with Liquid in Mixpanel and share it with us!

 


5 replies

i try to search about example of abandoned email reminder that i can add automatically products names and images to reminder email sent automatically to user, How can i do this ? 

Userlevel 4
Badge +3

Hi @anasemadmix, can you share more details or an example of what you are trying to set-up? 

Hey @cherise, I’m facing an error while using {{$first_name}} in subject of an email.

using {{$first_name}}

 

Userlevel 5
Badge +4

 @anasarshad it looks like you are using Mixpanel’s Dynamic syntax instead of the Liquid Templating in your subject line (read more on the differences here). I would recommend using Liquid Templating syntax to target your user properties in your email subject line because Mixpanel’s Dynamic feature will be deprecated on December 2020

When using Liquid in emails, you need to check the "Advance templating (liquid)" box at the bottom of the email builder.  Once you have liquid turned on, you can target your user properties.

 

Example of a liquid syntax,

{{user['$first_name']}}

 

You can read more on how to use Liquid templating language in our help documentation here

Let me know if this helps!

Thanks @stephanie !

Reply