Built-in variables

Leanplum variables

Leanplum includes many predefined variables to access user profile data. New variables may be added over time. To preserve compatibility, any custom variables you declare have precedence over the built-in variables.

Behavioral data

Includes information about a user’s activity, such as what events and states they have triggered. These variables are contextual, and evaluated for each user.

Variable nameTypeDescription
appVersionStringWhich version of your app the user is using.
firstOccurrenceOf['eventOrStateName']DateThe time that the event or state first occurred. Dates can be formatted using the date filter.
lastOccurrenceOf['eventOrStateName']DateThe time that the event or state last occurred.
occurrencesOf['eventOrStateName']IntegerThe number of times the user has tracked the event or state.
valueOf['eventName']NumberThe lifetime value of a particular event
lifetimeValueNumberThe user’s lifetime value designated by the purchase event.

A gaming app could, for example, include personalized information about a user's high scores in their message:

{{ userAttribute['Player name']}}, 
congrats on your level 14 score of {{valueOf['level14']}}!!

You now have {{lifetimeValue['totalCoins']}} total coins. 

Ready for level 15?

Lifecycle data

Includes information about when and how often a user is engaging with your app.

Variable nameTypeDescription
numPriorSessionsIntegerThe number of sessions recorded in the user’s lifetime.
firstRunDateThe time that the user was first seen.
lastActiveDateThe time that the user was last active.
minutesSpentInAppNumberThe number of lifetime minutes spent in the app.

Localization data

Localization data include the user’s location and localization preferences.

Variable nameTypeDescription
countryStringThe user’s country code.
regionStringThe user’s region (state, province, etc.)
cityStringThe user’s city.
localeStringThe user’s locale code.
languageStringThe user’s language code.
timezoneStringThe user’s timezone code.

Technology data

Includes information about the user’s current device.

Variable nameTypeDescription
deviceIdStringThe user’s current device ID.
deviceModelStringThe user’s current device model.
osNameStringThe user’s current OS name (e.g. iOS, Android)
osVersionStringThe user’s current OS version.
browserNameStringThe user’s current browser name.
browserVersionStringThe user’s current browser version.
pushEnabledBooleanWhether the user can receive push notifications, including data-only notifications.
textPushEnabledBooleanWhether the user can receive push notifications that can display text.

Traffic Source data

Includes information about where your users came from when installing your app. To use these, you need to set up an integration with one of our attribution partners. See partners.

Variable nameTypeDescription
sourcePublisherIdStringThe publisher ID of the referrer.
sourcePublisherStringThe publisher name of the referrer.
sourceSubPublisherStringThe sub publisher name of the referrer.
sourceSiteStringThe site name of the referrer.
sourceCampaignStringThe campaign name of the referrer.
sourceAdGroupStringThe ad group name of the referrer.
sourceAdStringThe ad name of the referrer.

User data

Includes the user’s ID and custom attributes.

Variable nameTypeDescription
developerModeBooleanWhether the user is a developer.
userBucketIntegerThe user’s randomized bucket from 0-999, used for randomizing content to different groups of users.
userIdStringThe user’s ID, which may be the device ID if no user ID was provided.
userAttribute['attributeName']StringA custom user attribute value within the user’s profile.

Contextual data

For triggered messages, contextual variables are used to access information about the trigger that activated the message. For example, a shopping cart abandonment message could be triggered by an “Add to Cart” event. This event may contain parameters including the item, quantity, and price of the item that was added to the cart.

Messages sent via the sendMessage API
Variable nameTypeDescription
messageIdStringThe numeric ID of the current message.
valueAnyA custom value that may be provided when calling the sendMessage API programmatically.

🚧

Null values sent through the sendMessage API will insert the key as a string.

If values sent through the sendMessage API /Manual delivery evaluate to null or the key is missing in the sent JSON values, the value will equal the string of the key. Examples:

Jinja code used:
{{value['value1']}}

Values sent:

  1. For a correct value passed in the API call
    {"value1": "some text"}
    The text "some text" will be inserted.

  2. For a null value
    {"value1": null}
    The text "value1" will be inserted.

  3. If no value is passed
    {}
    The text "value1" will be inserted.

To handle this behavior correctly, try the following condition:
{% set v = value['value1'] %}
{% if v and (v != "value1") %}
Value1 is not null. The value passed is: {{v}}
{%else%}
Value1 is null.
{% endif %}

Scheduled server-side messages

Variable nameTypeDescription
messageIdStringThe numeric ID of the current message.

Triggered server-side messages

Variable nameTypeDescription
messageIdStringThe numeric ID of the current message.
parameter['parameterName']StringThe value of a parameter of the event or state that triggered the message.
previousValueStringThe previous value of the user attribute change that triggered the message.
currentValueStringThe current value of the user attribute change that triggered the message.
triggerTimeDateThe time that the initial trigger occurred.

In-app messages

Variable nameTypeDescription
messageIdStringThe numeric ID of the current message.
parameter['parameterName']StringThe value of a parameter of the event or state that triggered the message.
previousValueStringThe previous value of the user attribute change that triggered the message.
currentValueStringThe current value of the user attribute change that triggered the message.

🚧

In-app message limitations for contextual values.

For in-app messages, the contextual values above cannot be used with advanced jinja features such as if-statements, for-loops, or filters. The values above can only be printed, e.g. {{previousValue}}.

Linked data variables

Linked data allows you to connect your user profile data with data stored in other sources, like in your own data warehouse or within another service provider. Some examples of linked data include real-time information such as whether an item is in stock, whether a flight is on time or delayed, weather forecasts, or inferred demographic data based on the current user’s attributes.

You can set up linked data sources in your app settings on the Leanplum dashboard. Linked data sources have a name and a URL of the source. The URL itself can be a Leanplum template, which can in turn include other information from a user’s profile. The URL may also include empty placeholders ({}) which must be filled in when accessing the data source.

All linked data is accessible from the linkedData variable:

Variable NameData TypeDescription
linkedData['data source']String, Array, or DictionaryData available from a particular named data source defined in your app settings. If possible, the data may be cached for several minutes to reduce repeated requests to the particular source. If the data source outputs JSON data, the data is parsed as a dictionary or array, which can be accessed further using loops or dot notation.
linkedData['data source'][arg1][arg2][...]String, Array, or DictionaryFor parameterized data sources, specify the appropriate number of arguments using dot or subscript notation to fill in all of the missing placeholders.

Examples

Menu

You may have a “menu” data source which emits the following JSON:

{
  "date": "March 10, 2016",
  "items": [
    {
      "name": "Lasagna",
      "image": "http://mydomain.com/images/1001.jpg",
      "price": 11
    }, {
      "name": "Ravioli",
      "image": "http://mydomain.com/images/1002.jpg",
      "price": 12
    }, {
      "name": "Pizza",
      "image": "http://mydomain.com/images/1003.jpg",
      "price": 10
    }
  ]
}

To render the menu in a template, you may use the following markup:

{% for item in linkedData.menu.items %}
  <div>
    <h2>{{ item.name }}: ${{ item.price|round(2) }}</h2>
    <img src="{{ item.image }}" />
  </div>
{% endfor %}

Product pricing

You may have an “item” data source with a parameter for the item SKU, which looks up information about a particular product.

{
  "inStock": true,
  "price": 39.99
}

You can display the price of an item with this template:

The price of the item is 
{{ linkedData.item[parameter.itemSku].price|numberformat('$#,###.00') }}.

Handle missing Data

In the above example, if there was a problem retrieving the data, the price would be null. Leanplum will automatically skip sending the message if a null value is rendered within a message.

In the next example, we display if the item is in stock. Here, we are not rendering a null value, so we need to explicitly skip the message using skipmessage().

{% set item = linkedData.item[parameter.itemSku] %}
{% if item == null %}
 {{ skipmessage() }}
{% elif item.inStock %}
 Your item is in stock at {{ userAttribute.preferredStore }}!
{% else %}
  Your item is not in stock at this time. 
  We’ll notify you when it becomes available.
{% endif %}