assert
Description
Checks certain things are in place before sending a message.
assert( mixed expression [, string failuremessage] )
Assert that expression
is true. If expression
evaluates to false
, null
, 0
, or ""
, terminates execution of the Zephyr script.
Terminating execution via a failed assert()
will have the following consequences:
- Within transactional messages, will prevent the message from sending
- Within triggers, will prevent any further execution of the trigger
- Within hosted pages or preview, will cause an error preventing rendering the page
- Within campaigns, will prevent the campaign from being sent to a specific user
Essentially, assert()
can be used as a safety check to make sure that certain things are in place before a user is sent a message. For example, if you have an abandoned shopping cart email that should never be sent to someone without items in their cart, you can put the following in the Setup section (under the Advanced tab in the template editor):
{assert(profile.purchase_incomplete, "User has nothing in shopping cart")}
If you have a template that always requires that certain variables be passed, you could assert against those variables being non-null:
{assert(friend_requests > 0, "User has no friend requests")}
Or if you want your campaign to send only if there are more than 5 pieces of content in the data feed.
{assert(length(content) > 5, "Not enough content")}
Example
Cancel if there’s not enough content to show a user
{content = filter(content, lambda c: c.vars.sailthru_vertical && c.vars.sailthru_topic == profile.vars.favorite_topic)}
{assert(length(content) > 1, "No content in the user's favorite topic!")}
Explanation: This script is using the filter function to only include any content that has a topic variable that equals a “favorite_topic” variable on the user’s profile. It then checks how many items are left using the length() function. If there’s more than 1 (i.e. no items), then the assert() function allows the email to go out. If there isn’t, the email will not deploy to that specific user.