Dynamic Content Syntax

You can use javascript to make dynamic contents in D·engage platform.

This template is similar to the embedded javascript (EJS) template language. (EJS site = https://ejs.co/)

But open and close delimiters for javascript blocks are {% and %}. You can use any javascript expression in these blocks.

Basic Example
{% if ($Contact.name) { %}
 <h2>{%= $Contact.name %}</h2>
{% } %}

Template system can interpolate values, using {%= … %}, as well as execute arbitrary JavaScript code, with {% … %}.

If you wish to interpolate a value and have it be HTML-escaped, use {%- … %}.

There are 3 global variables that you can use in templates.

$Contact: This object includes information about current contact. Properties of this object are columns of the master_contact table like contact_key. This object can be null in Push sends.

$Device: This object includes information about the current device. Properties of this object are columns of the master_device table like a token. This object is only available for Push contents and has a null value for other channels.

$Current: This object has extra columns that are coming from a selected audience. This can be selected Send List Table or a SQL Segment.

Query Data From data Space

You can make queries in your templates and get arbitrary data from any table in the data space. For this, you must use the $from function with the table name. Then you must add filter options by calling relevant functions of the query object.

Usage Example
var items = $from("myTable")
 .where("contact_key", "=", $Contact.contact_key)
 .where("name","=","abcd")
 .where("birthdate",">", "2018-04-01 14:11")
 .order("birthdate", "DESC")
 .take(5)
 .skip(2)
 .get();

Explanation

  • $from(“db_table_name”): Retrieve data from table “db_table_name”

    • db_table_name: case sensitive table name on database
  • .where(“column_name”,“operator”, “column_value”): (optional), Sql where clause, can be repeated

    • column_name: case sensitive table column name
    • operator: sql operator (“=”, “>”, “<”, “<>” etc.)
    • column_value: column value for where clause
  • .order(“column_name”, “direction”): (optional) sorting mechanism

    • direction: ASC = Ascending, DESC = descending
  • .take(n): (optional) limiting query, n = integer value

  • .skip(n): (optional) offset point, n = integer value

  • .random():(optional) shuffle data for randomization

After filters, for executing query you must use one of the following functions

  • .get(columns): get result as an array of objects. If you don’t give columns, all columns will be selected.

  • .first(columns): get first row as object. If you know that result is one row, you can use this. If you don’t give columns, all columns will be selected.

  • .value(column): get the given column value for the first row. If you that the result is an one row and one column value, you can use this.

Example
{%
 var products = $from("product_recommendation")
               .where("contact_key","=",$Contact.contact_key)
               .take(5)
               .get(); 
%}
{% if (products.length > 0) { %}
   <table>
   <tr>
       <th>Product Name</th><th>Price</th><th>Link</th>
   </tr>
   {% for (var i = 0; i < products.length; i++) { %}
       <tr>
           <td>{%= products[i].name %}</td>
           <td>{%= products[i].price %}</td>
           <td><a href="{%= products[i].link %}">link</a></td>
       </tr>   
   {% } %}
   </table>
{% } else { %}
   <h2>See new producst</h2>
   <a href="https://example.com/products?user_id={%= $Contact.contact_key %}">Go to products</a>
{% } %}

Canceling Send

If you want to cancel send for some contacts when evaluating javascript, you can use the $blockSend function.

$blockSend(reson): give a reason as string

Making API Calls to Remote Endpoints

If you want to make an HTTP request to get data from external sources. You have to define these Endpoints in the Dengage Admin

Panel by going to the “Settings > Custom API Endpoints” page. You can only add GET requests.

All the endpoints defined in the settings will be available in contents, under the $CustomApi object.

$CustomApi.getPromoCode($Contact.contact_key);

Utility Functions

FormatDate(date[, format]): Format dates

FormatDate(date);
FormatDate(new Date());
FormatDate(date, 'yyyy-MM-dd HH:mm');