You might want to apply finer control to your triggers than you can achieve with standard trigger configuration. You can use snippets of JavaScript inside a trigger program to achieve this.
To add JavaScript to a trigger program:
Go to Triggers in the left side menu.
Select the Trigger Programs tab.
Select the trigger program you want to edit.
Under Advanced Features, select Edit Script.
Enter your JavaScript in the Trigger script field, and select Save.
To test your script, select Run. You see the output of the script in the Script output field.
Script examples
Disable a trigger
result.proceed = false;
Run trigger only for certain types of products
The following script sets the trigger to run only if the type of the first product was equal to type1.
If the trigger was fired for a cart containing any other type of first product, it would be blocked and would not run.
const isMatch = false;
if(helpers.getProducts().length && helpers.getProducts()[0].ex) {
isMatch = helpers.getProducts()[0].ex['type'] === 'type1';
}
result.proceed = isMatch;
Override action times
For precise control of trigger action timing, you can include the following script. The waituntil
list contains one item per wait, in order, each of them a datetime to replace the corresponding wait, or null to let the wait proceed as normal. UTC timezone must be used.
For example, for a trigger program with 3 actions, and therefore 3 corresponding waits, this script overrides the first and third wait time:
result.proceed = true;
result.waituntil = [ new Date("2015-01-01 12:00:00"), null, new Date("2016-12-25 12:00:00"), ];
The dates you specify don't need to be fixed. For example, you could calculate them based on data from fields in the person record:
// Setup a date - this would usually be captured from the website script or calculated here.
person.ex.future_date = new Date("2025-01-01 12:00:00");
// now return the waituntil values.
result.proceed = true;
result.waituntil = [ person.ex.future_date, null, person.ex.future_date2, ];
Run browse abandonment trigger only if several products have been browsed
The following condition only allows the trigger to run if at least 2 products have been browsed.
The same trigger would also work for cart abandonment, and would only allow it to run if there were at least 2 products in the abandoned cart:
result.proceed = true;
if(helpers.getProducts().length < 2) {
result.proceed = false;
}
Get a count of the number of products that belong to a given list of category IDs
This script returns the number of products that match either photography or lenses:
const prods_in_categories = helpers.productsInCategories(helpers.getProducts(), ['photography', 'lenses']);
To only return products that match both photography and lenses, add a true
parameter on the end:
const prods_in_categories = helpers.productsInCategories(helpers.getProducts(), ['photography', 'lenses'], true);
Count of the number of products with a specific parameter value
This script returns the number of products that have a sku of either test_camera, or test_lense. This can be used for extra and optional product data by simply adding ex.
or opt.
at the start:
const prods_in_attribute = helpers.productsMatch(helpers.getProducts(), 'sku', ['test_camera', 'test_lense']);
Get a list of all categories
This returns a string of all categories in the cart, separated by the parameter you define:
If you don’t define a separator, then a comma is used.
const carted_categories = helpers.getCategories(helpers.getProducts(), '||');
Get a list of all product IDs
This returns a string of all product IDs in the cart, separated by the parameter you define:
If you don’t define a separator, then a comma is used.
const carted_product_ids = helpers.getPrids(helpers.getProducts(), '||');
Get a list of attribute values from the products
This returns a string of all the values of the attribute defined (in this case sku) in the cart, separated by the parameter you define:
If you don’t define a separator, then a comma is used.
const carted_skus = helpers.getAttributes(helpers.getProducts(), 'sku', '||');
Treat top customers differently
This script identifies your top customers, measured over the last 30 days.
As long as this analytic has been enabled. Learn more in Analytics.
If the trigger is fired for a cart belonging to any other type of shopper, it is blocked and doesn’t run.
const isTopCustomer = false;
if(analytics && analytics.top_customer_30) {
isTopCustomer = analytics.top_customer_30;
}
result.proceed = isTopCustomer;
You could also set up another trigger which sends to those shoppers who are not a top customer:
const isTopCustomer = false;
if(analytics && analytics.top_customer_30) {
isTopCustomer = analytics.top_customer_30;
}
result.proceed = !isTopCustomer;
Treat recent purchasers differently
This script identifies customers who have purchased in the last week.
As long as this analytic has been enabled. Learn more in Analytics.
If the trigger is fired for a cart belonging to any other type of shopper, it is blocked and doesn’t run.
const hasPurchasedInLastWeek = false;
if(analytics && analytics.purchased_qty_7) {
hasPurchasedInLastWeek = analytics.purchased_qty_7 > 0;
}
result.proceed = hasPurchasedInLastWeek;
You could also set up another trigger which would send to anyone who has not purchased in the past week:
const hasPurchasedInLastWeek = false;
if(analytics && analytics.purchased_qty_7) {
hasPurchasedInLastWeek = analytics.purchased_qty_7 > 0;
}
result.proceed = !hasPurchasedInLastWeek;
Treat customers with an iPhone differently
This script identifies new visitors using an iPhone, measured over the last 7 days.
As long as this analytic has been enabled. Learn more in Analytics.
If the trigger is fired for a cart belonging to any other type of shopper, it is blocked and doesn’t run.
const isNewCustomerWithIphone = false;
if(analytics && analytics.new_visitor_7 && analytics.device_mobile_7) {
isNewCustomerWithIphone = analytics.new_visitor_7 && analytics.device_mobile_7=='iOS';
}
result.proceed = isNewCustomerWithIphone;
Manage custom signals
Custom signals can be used from your website to allow triggers to be run based on trigger rules.
When custom signals are used, they can be routed throughout the system and stored in the person record. From here, they can be used in mail merge or scripting.