Skip to main content
All CollectionsTriggers
Control triggers using JavaScript
Control triggers using JavaScript

Learn to apply advanced control using JavaScript within a trigger program.

Updated over 3 weeks ago

In a trigger program, you can enhance control by adding JavaScript. This allows for more advanced customization beyond standard trigger configuration.

Add JavaScript to a trigger program

To include JavaScript in a trigger program:

  1. Go to Triggers in the left-side menu.

  2. Select the Trigger programs tab.

  3. Open the trigger program you want to modify.

  4. Under Advanced features, select Edit Script.
    ​

  5. Enter your JavaScript in the Trigger script field, then select Save.

  6. To test your script, select Run. The output appears in the Script output field.

This enables precise control over trigger execution using custom JavaScript logic.


Script examples

Use the following JavaScript examples to customize trigger behavior in Fresh Relevance.

Disable a trigger

To prevent a trigger from running, set result.proceed to false:

result.proceed = false;

Run trigger only for certain types of products

This script allows the trigger to run only if the first product in the cart belongs to type1. If the product type does not match, the trigger is blocked.

const isMatch = false;
if(helpers.getProducts().length && helpers.getProducts()[0].ex) {
isMatch = helpers.getProducts()[0].ex['type'] === 'type1';
}
result.proceed = isMatch;

Override action wait times

To control when trigger actions occur, specify exact timestamps in the waituntil list. Each entry corresponds to a wait time, with null allowing the default behavior.

You must use the UTC timezone.

For a trigger program with three actions, this script overrides the first and third wait times:

result.proceed = true;
result.waituntil = [ new Date("2015-01-01 12:00:00"), null, new Date("2016-12-25 12:00:00"), ];

To dynamically calculate wait times based on user data:

// 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 a browse abandonment trigger only after multiple product views

To ensure a trigger runs only if at least two products have been browsed, use:

result.proceed = true;
if(helpers.getProducts().length < 2) {
result.proceed = false;
}

This logic also applies to cart abandonment triggers, ensuring they activate only when at least two products are in the abandoned cart.

Count products in specific categories

To get the number of products matching categories, use this script which uses the categories photography or lenses as examples:

const prods_in_categories = helpers.productsInCategories(helpers.getProducts(), ['photography', 'lenses']);

To count only products that match both categories, add true as a third parameter:

const prods_in_categories = helpers.productsInCategories(helpers.getProducts(), ['photography', 'lenses'], true);

Count products with a specific attribute value

To count the number of products with a SKU, use this example which with examples of test_camera or test_lense:

const prods_in_attribute = helpers.productsMatch(helpers.getProducts(), 'sku', ['test_camera', 'test_lense']);

Retrieve a list of all categories

To return a string of all product categories in the cart, separated by a custom delimiter, use.

const carted_categories = helpers.getCategories(helpers.getProducts(), '||');

If no separator is specified, a comma is used by default.

Retrieve a list of all product IDs

To return a string of all product IDs in the cart, separated by a custom delimiter:

const carted_product_ids = helpers.getPrids(helpers.getProducts(), '||');

If no separator is specified, a comma is used by default.

Retrieve a list of attribute values from products

To return a string of all SKU values in the cart, separated by a custom delimiter:

const carted_skus = helpers.getAttributes(helpers.getProducts(), 'sku', '||');

If no separator is specified, a comma is used by default.

Treat top customers differently

To trigger an action only for customers classified as top customers in the last 30 days (if this analytic is enabled in Analytics), use:

const isTopCustomer = false;
if(analytics && analytics.top_customer_30) {
isTopCustomer = analytics.top_customer_30;
}
result.proceed = isTopCustomer;

To target non-top customers instead:

const isTopCustomer = false;
if(analytics && analytics.top_customer_30) {
isTopCustomer = analytics.top_customer_30;
}
result.proceed = !isTopCustomer;

Treat recent purchasers differently

To trigger an action only for customers who have made a purchase in the last seven days (if this analytic is enabled in Analytics), use:

const hasPurchasedInLastWeek = false;
if(analytics && analytics.purchased_qty_7) {
hasPurchasedInLastWeek = analytics.purchased_qty_7 > 0;
}
result.proceed = hasPurchasedInLastWeek;

To target customers who have not purchased in the past week:

const hasPurchasedInLastWeek = false;
if(analytics && analytics.purchased_qty_7) {
hasPurchasedInLastWeek = analytics.purchased_qty_7 > 0;
}
result.proceed = !hasPurchasedInLastWeek;

If the triggers fire for any other type of shopper, they are blocked.

Treat iPhone users differently

To trigger an action only for new visitors using an iPhone within the last seven days (if this analytic is enabled in Analytics), use:

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;

If the trigger fires for any other type of shopper, it is blocked.


Manage custom signals

Custom signals allow triggers to execute based on specific trigger rules defined on your website. These signals can be routed through the system and stored in the person record, making them available for mail merge or scripting.

Did this answer your question?