BRAZE TUTORIAL — Liquid — How To Send/Abort Messages To Specific Age Ranges Based On Birthday

BRAZE TUTORIAL — Liquid — How To Send/Abort Messages To Specific Age Ranges Based On Birthday

You get an email! You get an email! But you, you don’t get an email! Happy Birthday!

birthday

If you have a use case where you need to abort messages to users in or outside of a specific age range, then you’ve found the perfect post.

First, let’s get straight to it; here’s the full code below:

{% assign birthdate = %}
{% if birthdate %}
{% else %}
{% abort_message("no birthdate available") %}
{% endif %}

{% assign birth_year = birthdate | slice: 0, 4 | plus: 0 %}
{% assign birth_month = birthdate | slice: 5, 2 | plus: 0 %}
{% assign birth_day = birthdate | slice: 8, 2 | plus: 0 %}

{% assign current_year = "now" | date: "%Y" | plus: 0 %}
{% assign current_month = "now" | date: "%m" | plus: 0 %}
{% assign current_day = "now" | date: "%d" | plus: 0 %}

{% assign age = current_year | minus: birth_year %}

{% if current_month < birth_month %}
{% assign age = age | minus: 1 %}
{% endif %}

{% if current_month == birth_month and current_day < birth_day %}
{% assign age = age | minus: 1 %}
{% endif %}

{% if age < 18 or age > 45 %}
{% abort_message("not in age range") %}
{% endif %}

And also, full transparency: yes, this was written using the AI Liquid Assistant.

However, unless you fully understand what’s going on inside the code, we would not recommend simply copy and pasting this!

So let’s break down this code, section by section, to fully understand what’s happening here.

Standard Attribute: date_of_birth

{% assign birthdate = %}

The first thing we’re doing is creating our own variable called birthdate, and this is simply to avoid having to write out the full syntax .

It’s easier, avoids syntax issues, and just looks cleaner. However, this step is optional; referencing the data by full name will work perfectly fine.

Checking for Existence of birthdate

{% if birthdate %}
{% else %}
{% abort_message("no birthdate available") %}
{% endif %}

Next, we’re checking to see if there’s actually a value stored under birthdate. And sure, this section can come before the first line of code, but it accomplishes the same result.

If there is actually a value under birthdate, then great, we do nothing and move on.

OTHERWISE, or else, we abort the send, with the abort message: “no birthdate available”.

After all, how can we determine someone’s age if we don’t have their birthdate?

Breaking Down birthdate

{% assign birth_year = birthdate | slice: 0, 4 | plus: 0 %}
{% assign birth_month = birthdate | slice: 5, 2 | plus: 0 %}
{% assign birth_day = birthdate | slice: 8, 2 | plus: 0 %}

Here’s we’re breaking down our birthdate into 3 parts: year, month, and day. But what’s with all the slicing?

First, before we look into the slice filter, we need to understand the structure of the birthdate data, which is in ISO-8601 format: YYYY-MM-DD.

So an example value behind the variable birthdate could be “2000–07–13”.

Let’s look specifically at the first line:

{% assign birth_year = birthdate | slice: 0, 4 | plus: 0 %}

First, we are simply creating another variable called birth_year so we can store exactly the birth_year.

The birth_year variable takes the value behind birthdate, which is “2000–07–13”, but we are going to slice this value.

The 0 represents where we should start slicing. 0 represents the very first character, so we are going to start at the very first character, “2”.

The 4 represents how many characters the filter should return. So 4 means “grab the 4 characters, starting from the very first character”.

This returns “2000”. Which is exactly the birth year.

Lastly, why are we adding 0 to it? 2000 + 0 = 2000, so why do we need to do this extra step?

We have to realize that a timestamp variable is actually a String. Meaning “2000” is not the Number 2000, but it’s actually a String “2000”.

And although it may work to mathematically compare two Strings, it’s best practice to first convert this variable, or cast this variable, into a Number before we do any mathematical comparisons.

And we can convert a String into a Number by adding 0 to the String. Fun Fact: to convert a Number into a String, we append an empty character “”.

Now, we’re using a lot of technical terms here, so please feel free to leave any questions in the comments.

If you’re really dedicated and curious to learn more, please check out our Liquid For Technical Marketers program that’ll make this use case feel like a piece of cake.

Back to Liquid: we’ve broken down the birth_year, and we’re doing the exact same thing for month and date.

birth_month starts at the 5th index (or the 6th character) and grabs 2 characters. This successfully avoids the first hyphen.

And same thing for birth_day.

And every single date, if written in ISO-8601 format, will have exactly 10 characters that follow this exact same structure.

Breaking Down the Current Time

{% assign current_year = "now" | date: "%Y" | plus: 0 %}
{% assign current_month = "now" | date: "%m" | plus: 0 %}
{% assign current_day = "now" | date: "%d" | plus: 0 %}

Next, we’re applying a very similar step, but to the current timestamp. And the next section will explain why this is necessary.

There’s a special keyword called “now” that will always return the current timestamp.

For example, “now”, right now, will return 2025–01–09T21:29:25 PT. But that was about 19 seconds ago at this point.

And the “date” filter is used to convert this long timestamp into a desired time format.

In this snippet, the 3 lines are converting the timestamp to “YYYY”, “MM”, “DD”, respectively.

So current_year = “2025”, current_month = “01", and current_day = “09".

Calculating The Age

{% assign age = current_year | minus: birth_year %}

Now, we’re finally ready to do some math, and figure out your age!

The math is simple: your age is the current year minus your birth_year. So for someone whose birthday is on “2000–07–13”, the math would be:

2025–2000 = 25.

Except… they wouldn’t be. Because their birthday hasn’t passed yet.

This calculation works for those whose birthday already passed that year. If not, that’s when the next section comes in handy.

Adjusting The Age Based On Birthday

{% if current_month < birth_month %}
{% assign age = age | minus: 1 %}
{% endif %}

{% if current_month == birth_month and current_day < birth_day %}
{% assign age = age | minus: 1 %}
{% endif %}

There are 2 simple checks we’re making here:

1. If the current_month is less than the birth_month, that means the user’s birthday hasn’t passed yet, so the age calculation from the previous section is off by 1.

Simple fix: we subtract 1 from the age.

2. If the current_month is actually the same as the birth_month but current_day is less than your birth_day, that means your birthday is happening this month, but hasn’t happened yet. So exciting!!

Once again, simple fix: we subtract 1 from the age.

Notice how we’re using an “and” logical operator in the second snippet. So if the current_month equals the birth_month, but the current_day is greater than birth_day, meaning your birthdate already passed, then we won’t make this adjustment.

Age Range

{% if age < 18 or age > 45 %}
{% abort_message("not in age range") %}
{% endif %}

And lastly, here is our age logic. If the user’s age is less than 18 or greater than 45, or in other words, if the user is outside of the age range 18–45, inclusive, then they will be aborted from this message.

This means that the intended audience for the message is only those who are in between the ages if 18–45.

Even if you are 17 years and 364 days old, you will not receive this message.

Thank you!

Thank you for reading, and please reach out with any questions!

fornowmarketing.com

allan@fornowmarketing.com

Previous
Previous

Braze Reflection: Personalized EOY Wrapped — Custom HTML Paginated IAM

Next
Next

BRAZE TUTORIAL — SQL — How To Exports Users Who Opened/Clicked A Specific Email Campaign