Snippets are smaller custom pieces of jinja, code, text, or HTML that can be saved and reused across campaigns. Their use can range from saving frequently used Jinja macros, to saving snippets of text, such as the unsubscription sentence that you use in all your SMS campaigns. Similar to HTML blocks or Custom rows, all Snippets are saved within the Asset Manager.

📘

Support

Snippets are currently supported within Email campaigns, Scenarios (SMS content, webhooks, browser and mobile push content, wait node...), Blocks, Custom rows, and Weblayers.

Snippets are not supported in: in-app messages, Experiments, Tags. Note that we will be adding support for all campaign types in the future.

Creating a snippet

You can create a new snippet in three ways:

  • Directly from the Asset Manager -> Snippets by clicking on the "New snippet" button at the top of the screen
  • From the client’s campaign or email node by clicking a (+) button on the bottom of the editor or node modal, selecting snippet in the asset selector and choosing the “New snippet” option
  • From the predefined templates by using the “Save as copy” option

All cases will open up a separate snippet builder window, where the client can create and preview the snippet.

It is possible to create constant or non-constant parameters of various types to be used within the snippets, and to define the parameter values upon using the snippet in a campaign. This is covered in more detail in the 'Using parameters' section.

Upon saving, all snippets are stored within the project templates, and can be accessed from the Asset Manager, section Snippets.

There are two types of Snippets templates:

  • predefined templates are created by Bloomreach and cover many different use cases. These are not stored in the client’s project but loaded in real time from a central repository. This means they cannot be edited directly but can be saved as copy into the project templates and then further edited using the “Save as copy” option
  • project templates are any custom snippets created by the client or by copying and changing the predefined templates

Predefined templates

Users can also use a predefined template directly from the campaign asset picker.

In case the template contains parameters, the user must first save the snippet into the project templates via “Save as copy” and then select the project copy for use in the campaign (In order to reference a snippet in a campaign it must have a project copy).

In case the template does not contain parameters, the user will have two options: “Save as copy” and then use the project template copy or “Copy as code” which will copy the code of the predefined template into the clipboard.

Snippet editor

Regardless of the way you go about creating a new snippet, clicking on the New Snippet button will open up a code editor with a Parameters tab on the right. The editor is where you will write the content of the snippet. That can be anything, ranging from plain text to Jinja macros or HTML code.

At any point, you can preview the snippet by clicking on the Test tab. This will allow you to select a specific customer to preview the snippet on, and specify the available parameters.

Using parameters

It is possible to create parameters to be used in the snippet. Parameters are like variable values that make it possible to insert a custom value within, e.g. each email separately. This will prompt the option to choose the value of the parameter when the snippet is selected within a campaign.

Parameters can be created using the parameters editor by clicking on the + Add parameter button, or can be defined directly in the code and created using the button “Load parameters from the code editor”.

Each parameter can be also assigned to the category. Categories should help to better organize the parameters within the snippet. Category can be created using the parameters editor by clicking on the _+ Add category _button. Both parameters and categories also support drag&drop functionality.

You can define following attributes of a parameter:

Parameter reference-> name of the reference of the parameter to be used in the code, The parameters display name is derived from it (similar to template parameters)

  • The derived parameter name will automatically split words if camel-back notation is used or word connected with underscore _
    reference paramName -> Param Name
    reference param_name -> Param Name
    Parameter type -> data type of the parameter
  • Available data types:
    • string (default) – a simple one-line text, type returned is string
    • text – multi-line text, type returned is string
    • number - type returned is integer or float
    • boolean - type returned is True/False
    • list - type accepted/returned is jinja list type e.g [1,2,3]
    • object - type accepted/returned is jinja dict type e.g {"key": value}
    • datetime – datetime picker, value returned is timestamp as number
    • date – date picker, value returned is timestamp as number with default hours set to 23:59:59 currently FE seems to create timestamp at 23:59:59 (CET)
    • time – time picker, value returned is string with the time “12:00”
    • color – color picker, accepts color values in HEX, rgb or rgba format, type returned is string
    • enum - selector for defined string values
    • image - image picker from the Bloomreach file management, value returned is a string that represents the URL of the image
    • recommendation - recommendation picker, value returned is an ID of the selected recommendation model
    • metric - metric picker, value returned is an ID of the selected metric
  • For most of the data types except (datetime, date, time and boolean) it is possible to use either constant values or jinja variables such as {{ customer.first_name }}
  • Parameters types are validated for constant values, in case a jinja variable is used as parameter the type of the parameter is not validated

Default value -> default parameter value. Default value will be used in case no specific parameter value is provided upon snippet usage in campaign (calling the snippet) and to prepopulate the parameters input.

  • For most of the data types except (datetime, date, time and boolean) it is possible to use either constant default values or jinja variables such as {{ customer.first_name }}
  • If no default value is provided, and the parameter is not filled it the jinja undefined value will be used

Required field -> define whether parameter must be filled in when using the snippet. Parameters marked as required must be filled in unless the default value is also used.

Tooltip -> Tooltip will be displayed by the parameter name when inputting the parameters values in campaign

Parameters can be used within the the code as standard jinja variables using the keyword params and the defined reference name e.g:

{{ params.parameterName }} or {{ params["parameter name"] }}

{% set otherVariable = params.parameterName %}

{% if param.paramName = "something" %}

…

{% endif %}

Multi words param references can only be used with the params[""] notation.

Calling snippets with parameters:

{{ snippet('<snippet id>', {'parameterName': 'value'}) }}

Using Jinja as a parameter

It is allowed to use simple jinja variables/expressions as parameters of snippets. Parameters can’t contain complicated jinja expressions within {%... %} or strings containing jinja within {%... %} _such as _set, if, for, loop etc.

Contextual variables such as 'event', 'webhook', 'email.view' are not available directly in the snippets but can be used as parameters in campaigns where those variables are available.

Notation

There are two types of notation of jinja variables as parameters depending on their usage.

1. When using snippets as the jinja reference the variables must be used directly (without {{ }})

For example:

{{ snippet('<snippet id>', {'purchasePrice': event.total_price }) }}

{{ snippet('<snippet id>', {'finishDate': ((time + 604800) | from_timestamp('%d-%m-%y')) }) }}

{{ snippet('<snippet id>', {'products': [1,2,3] }) }}

Strings containing jinja personalization must be created using ~, otherwise they would not be rendered:

{{ snippet('<snippet id>', {'mainText': 'Hello ' ~ customer.first_name ~ ', welcome!' }) }}

2. When filling in the parameters via the inputs in the asset picker, here you need to use the {{}} notation so we can recognize its a variable and generate the reference correctly e.g without “”

In the parameters inputs, it's also possible to input a string containing jinja and it will be automatically translated into the ~ notation. For example, the following parameters inputs:

355

will be translate into this reference:

{{ snippet('606321bfe04f9d2be61e0812', {'purchasePrice': event.total_price, 'mainText': 'Hello '~ (customer.first_name) ~', welcome!', 'fallbackValue': customer.last_name}) }}

Using snippets in a campaign

Snippets can be accessed from within supported campaigns via the _(+) _button at the bottom right of the screen. This button will open a modal with the asset picker for snippets and blocks.

Clicking on Snippets will load available snippets from the Asset Manager. Note that if the campaign is a part of an initiative, it will only show assets from the same initiative, or assets marked as Global objects.

Selecting a snippet will open the snippet preview with a customer selector, snippet description, and the parameters inputs. When entering the parameters, the preview will be regenerated once you stop inputting values (click outside).

Language variants behavior

The language variant of the snippet will be selected based on the selected main campaign template language version. To illustrate here is how following situations would be solved:

Case 1:
We have 3 assets: campaign template with sk variant, block with no language variant and snippet with sk variant and a customer with sk language:

  • template (sk) -> using block (default) -> using snippet (sk)
  • template (sk) -> using snippet(sk)

Case 2:
We have 3 assets: campaign template with no language variant, block with no language variant and snippet with sk variant and a customer with sk language:

  • template (default) -> block (default) -> snippet (default)
  • template (default) -> snippet(default)

📘

The language variant of the snippet will be selected based on the selected main campaign template language version.

Insert options

A snippet can be inserted into a campaign by clicking a (+) button on the bottom of the editor or action node modal and selecting a snippet in the asset selector. Chosen snippets will be displayed in preview with the option to specify the parameters.

After selecting and configuring the snippet, there are two ways of inserting the snippet into your campaign:

  • Copy as a reference -> will create a jinja reference with the inserted parameters and copy it onto the clipboard
  • Copy as code -> will copy the full content of the snippet into clipboard

(1) Copy as a reference

This will copy the reference to the snippet as a jinja tag that can be pasted into code. This will not allow changes to the snippet directly in the campaign and WILL be updated when the snippet is changed.

Referencing a snippet using Jinja:

  1. Without parameters:
`{{ snippet("<snippet id>") }}`
  1. With parameters:
`{{ snippet("<snippet id>", {"<param name>": <param value>, "<param name>": <param value> }) }}`

(2) Copy as code

This will take the full snippet content from the default language template and copy it to the clipboard. The client can simply paste the code into the desired campaign part. This code can be changed as desired, but will NOT be updated when the snippet is changed, neither will it consider language variants.

🚧

This option is only available for snippets without parameters.

Limitations

  • Hard Limits:
    • Max size 10kB of the template of a snippet per translation
    • Max 30 distinct translations per snippet
    • Max 20 of distinct snippets being used in a single campaign template/block
    • Max 20 distinct parameters can be created per snippet
    • Max length 100 characters of a parameter name or category
    • Max length 1000 characters for parameter tool-tip
  • Parameters can’t contain complicated jinja expressions within {%... %} or strings containing jinja within {%... %} such as set, if, for loop etc.
  • It is not possible to use snippet inside a snippet