Understand the logic that powers Klaviyo
You probably have several different pieces of technology in your tech stack that are each designed to serve a very specific purpose. While the front-end UI and output of each program may be different, all of the software in your stack (including Klaviyo) is built using a few key programming logic concepts. Understanding these logic concepts can help you grasp how these programs actually function. Many of the powerful features in Klaviyo are just point-and-click versions of these basic logic concepts.
By understanding these concepts, you can build more powerful segments, implement and troubleshoot advanced personalization in email templates, and gain the foundational knowledge needed to understand APIs and programming languages.
After completing this lesson, you should be able to:
- Define booleans, if statements, and for loops.
- Use comparison operators to evaluate statements.
- Create complex conditional logic with AND and OR statements.
Identify key logic concepts
Let’s go over some basic programming concepts that are used in your email templates, segment builder, and all other programming languages.
Booleans
A boolean is a binary value that can be evaluated as either true or false; there are no other options.
Booleans are one of the different data types in Klaviyo, in addition to text, numbers, dates, and lists. Every property has a specific data type.
Consider a profile property: “Has purchased,” representing whether or not a profile has any order history. If the customer has placed an order in the past, this property would be set to True. If the customer has never placed an order, this property would have a value of False.
In Klaviyo, any of the following values represent a True boolean:
True, “1”, 1, “true”, “t”, “yes”, “y.”
Any of the following values represent a False boolean:
False, “0”, 0, “false”,”f”, “no”,”n”, None.
Comparison operators
Comparison operators are used to compare 2 values and return a result that is either True or False.
If you ask yourself, “Is 10 greater than 2?,” it’s clear that the answer is yes.
In this case, let’s replace “greater than” with its comparison operator: “>.” 10>2 returns True.
An important comparison operator is “==,” which checks if 2 values are equal.
"Klaviyo" == "Klaviyo" would return True, since the text on the left is equal to the text on the right.
Another important operator is != which means “not equal.”
Consider a profile with these custom properties:
What do you think person.language != "English" would return?
On the left side of the equals sign, person.language accesses the "language" profile property, which is "Spanish". Spanish is not equal to English, so "Spanish" != "English" is True.
The most commonly used comparison operators are:
== : equal
!= : not equal
> : greater than
>= : greater than or equal to
< : less than
<= : less than or equal to
These operators can compare all data types.
AND and OR
Understand the difference
The words AND and OR have very specific meanings in the context of mathematics and programming.
For an AND statement to be true, the conditions it joins must both be true.
“Person is in the United States” AND “Person is greater than 50 years old” would only be evaluated as True for a person over 50 who is in the United States.
Meanwhile, only one condition needs to be true for an OR condition to be evaluated as True. “Person is in the United States” OR “Person is greater than 50 years old” would return True for people over 50 in all countries, and it would return True for all people in the United States, regardless of their age.
These can also be combined. To target all Oceanian customers over the age of 50, you can use “(Person is in Australia OR person is in New Zealand) AND person is greater than 50 years old.” This would only return True for people over 50 in Australia or New Zealand.
Negative conditions
Be careful when using negative conditions. Generally, we recommend keeping negative conditions as separate AND conditions rather than OR conditions to ensure all conditions are met.
Consider this example. Let’s say you want to create a segment that identifies people who are not on list A or list B. You may attempt to write the logic like this::
Person is not in List A
OR
Person is not in List B
Using the OR operator actually makes this segment inclusive. If someone is in List A and is NOT in List B, they will still enter the segment because they satisfy one of the two conditions, “Person is not in List B”. To build your intended segment correctly, you would need to use the AND operator:
Person is not in List A
AND
Person is not in List B
This logic would only show users who are not in List A and also not in List B, making it exclusive (narrower).
If/then/else statements
If statements first evaluate whether a condition is true. If the condition is true, then the program will execute a specific action. Else, the program will execute a different action if the statement is false.
An example of this logic is deciding whether to send a customer a VIP link. If the customer meets the criteria to be a VIP, then they get the link. Otherwise, they do not get the link.
You can also add in multiple conditions. For instance, you can use an if statement to send a Gold VIP package if a customer has 200 or more loyalty points and a Silver VIP package if they have between 100 and 200 loyalty points. Otherwise, they do not get a VIP package.
In most programming languages, the words, if, else if (or elif), and else are used.
For example, in an email template, the above example would be written as:
{% if person|lookup:'loyalty_points' >= 200 %}Congrats! Here is your Gold VIP Package. {% elif person|lookup:'loyalty_points' > 100 and person|lookup:'loyalty_points' < 200 %}Congrats! Here is your Silver VIP Package. {% else %}Here is your Standard Package! {% endif %}
For loops
For loops execute a piece of code for each item in a specified group. They are called loops because they loop around and do the same thing over and over again.
For instance, if you have a for loop with a list of 5 items, the code will be executed 5 times, one for each item.
Let’s say you have a customer who purchased 3 items. The item details are all saved in a variable called “items.” You can use a for loop to loop over all 3 items and display each item’s name and price.
In an email template, this for loop would be written as:
{% for item in event.items %} Your item name is {{ item.Name }}. Your item costs {{ item.Price }}. {% endfor %}
One way of thinking about this for loop is to say “for each item, display name and price.”
These logic concepts come into play in many parts of Klaviyo. One of the most powerful examples is the segment builder, which combines segment conditions using AND and OR logic.
In this example, profiles will only enter the segment if they have ever received an email AND either opened or clicked an email in the last 30 days.
The grouping of the conditions in the segment builder can help you determine which conditions need to be true in order for a profile to enter the segment.
As a next step, learn how to apply these concepts to design email templates using Django.
Apply logic to your email templates
There are a few native features of the Klaviyo email template editor that rely on fundamental programming concepts. Let’s learn more about how these work.
Show/hide blocks
Show/hide blocks combine booleans, comparison operators, and and/or.
You can set show/hide logic for each block and section in an email template.
To edit show/hide logic, select the block or section in your template and click Display options. Then, review and edit any show/hide logic from the Show/Hide Logic section.
Show/hide logic is a boolean - if it evaluates as True, then the block will appear.
Use comparison operators to build out your show/hide logic. You can also combine pieces of logic using "and" and "or." In this example, the block will only show to customers who are in either Canada or the United States and whose favorite color is not green.
Dynamic table
Dynamic tables use for loops to iterate over sets of information. To create a dynamic table, specify a Row collection and a Row alias.
- Row collection: A set of data from the triggering event
- Row alias: a simple word replacing the row collection when customizing the template
Every row in the table represents each item in the row collection.
A common use case for a dynamic table is displaying data about every product in a customer’s checkout or order. Klaviyo’s pre-built abandoned checkout and order confirmation flows include dynamic tables pre-populated with relevant information.
This example shows an email template from within an abandoned checkout flow that was triggered by a Started Checkout event.
The variable event.extra.line_items contains data about all the items in the customer’s checkout. The table iterates over everything in this variable and refers to each item as "item".
The result is a clear table where each row shows an item's title, image, quantity, and price.
Content repeat
The content repeat feature is another way to implement for loops in your templates.
Any blocks with content repeat applied will repeat for each item in a series, just as a for loop executes a specific piece of code for each item in a series.
In this example, the block will repeat for every item in the profile property Pets. For each pet, the block will display the name of the pet (i.e., petname).
The Item alias indicates how to reference each item that the content repeat feature iterates over. In this example, you can use {{ petname }} to display each item in the Pets variable.