Code Snippet

Dynamic Content Syntax

You can use javascript to make dynamic content 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.

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

If you are going to send these parameters via API. {%=$Current.parameter%} name should be used. This parameter should be added to the content via the panel.

 <h2>{%=$Current.order_id%}</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 the 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. The 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.

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 the 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 the query you must use one of the following functions

  • .get(columns): get a 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 one row and one column value, you can use this.

{%
 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');