Skip to main content

Using Javascript code expressions in Alia

Render dynamic content in your popup with Javascript

Bill Wohlers avatar
Written by Bill Wohlers
Updated this week

Alia supports rendering dynamic content in a few different places within a popup:

  • Text elements (select the "Code" editor)

  • Button text

  • Properties set when a form is submitted

This content is like any other text, except that you can insert Javascript snippets using double brackets. For example:

Your market is: {{ $alia.user.market }}

The content inside the brackets supports any Javascript expression. For example, the below code would render the same thing:

{{ "Your market is: " + $alia.user.market }}

More complex syntax (such as JSON and statements) may not work as expected.

You can insert expressions using our visual editor inside any text that supports expressions by clicking on this icon on the right:

For security reasons, expressions that are more complex than a simple variable will render only a blank string in the editor. Use the "Preview" button to see what the expression would actually render.

Values available in custom expressions

In addition to arbitrary Javascript, custom expressions have a few useful variables/values defined in their scope.

Variables

Use variables to easily inject some common values.

question_text

The text of the containing question element (nearest ancestor).

We recommend using this instead of specifying the question twice - otherwise, it's easy to forget to also update the value that's sent to integrations.

answer_text

The text of the containing answer element (nearest ancestor).

We recommend using this instead of specifying the answer twice - otherwise, it's easy to forget to also update the value that's sent to integrations.

sms_consent

Legally compliant SMS consent text for when the user is entering their phone and clicking a button to proceed. This text contains a link to your Terms & Privacy Policy, which can be set in settings.

sms_consent_reply_y

Legally compliant SMS consent text for when the CTA is to reply 'Y'. This text contains a link to your Terms & Privacy Policy, which can be set in settings.

email_consent

Legally compliant email consent text. This text contains a link to your Terms & Privacy Policy, which can be set in settings.

phone

The user's masked phone number, or empty if the user hasn't entered their phone yet. Will only show the country code and last four or two digits for US and international numbers, respectively. All other digits will be masked.

$alia object

You can use the $alia object in custom expressions, which allows you to reference user data like how they've responded to particular survey questions. See documentation for the $alia object here.

Rewards

You can claim rewards and reference either their discount code or text (e.g. "15% off") using the reward() function.

You can create and edit a reward expression using our visual editor:

Or with raw Javascript:

{{ reward({ id: 42, property: "text" }) }}

The reward() function accepts to arguments: a reward ID and what property to show (code or text).

When the reward() function is called:

  • If the user has already claimed the reward with this ID, the code/text for the reward they already claimed will be shown.

  • Otherwise, the reward will be claimed (thus generating a new code, if the reward uses a unique discount code).

You can find reward IDs in the rewards page.

Countdowns

You can render a countdown using the countdown() function. Note that for simple cases, you can use the countdown element, which provides mostly the same functionality with an easier to use interface.

The countdown() function accepts different arguments depending on if you want to count down a fixed time (e.g. 30 seconds), to a specific date, or to the end of a time period (e.g. end of the day).

To count down by a fixed time:

{
secs?: number;
mins?: number;
hours?: number;
}

To count down to a specific date:

{
date: Date | string | (() => Date | string);
}

To count down to the end of a period:

{
endOf: "minute" | "hour" | "day" | "week" | "month" | "year";
}

Did this answer your question?