Exclusion Scripts for Journeys
Exclusion Scripts in Journey Builder allow you to dynamically filter out subscribers from entering a journey using AMPscript or SSJS. These scripts run at the entry point of the journey to ensure only the right contacts proceed.
Where to Use Exclusion Scripts in Journey Builder?
You can use exclusion scripts in the Entry Source Settings under:
Data Extension Entry (Filter contacts before they enter).
Journey Settings (Prevent contacts from re-entering).
Decision Splits (Route contacts based on exclusion criteria).
- Basic AMPscript Exclusion Script Example Exclude contacts who have unsubscribed or are in a suppression list.
%%[
VAR @isUnsubscribed, @email
SET @email = AttributeValue(“EmailAddress”)
/* Check if email exists in the Unsubscribed Data Extension */
SET @isUnsubscribed = Lookup(“UnsubscribedDE”, “EmailAddress”, “EmailAddress”, @email)
IF NOT EMPTY(@isUnsubscribed) THEN
SET @journeyFilter = 1 /* Exclude from journey */
ELSE
SET @journeyFilter = 0 /* Allow entry */
ENDIF
]%%
How It Works?
- Looks up the UnsubscribedDE to check if the contact exists.
- If found, sets @journeyFilter = 1 (exclude contact).
- Otherwise, allows the contact to enter the journey.
2. Exclude Contacts Based on Email Domain
Block internal/excluded domains (e.g., @test.com, @example.com)
%%[
VAR @email
SET @email = AttributeValue(“EmailAddress”)
/* Exclude contacts with certain domains */
IF IndexOf(@email, “@test.com”) > 0 OR IndexOf(@email, “@example.com”) > 0 THEN
SET @journeyFilter = 1 /* Exclude */
ELSE
SET @journeyFilter = 0 /* Allow */
ENDIF
]%%
3.Exclude Contacts Based on Purchase History
Only allow customers who haven’t purchased in the last 30 days.
%%[
VAR @lastPurchaseDate, @subscriberKey
SET @subscriberKey = _SubscriberKey
/* Lookup last purchase date */
SET @lastPurchaseDate = Lookup(“PurchaseHistoryDE”, “LastPurchaseDate”, “SubscriberKey”, @subscriberKey)
/* If last purchase was within 30 days, exclude */
IF NOT EMPTY(@lastPurchaseDate) AND DateDiff(Now(), @lastPurchaseDate, “D”) <= 30 THEN
SET @journeyFilter = 1 /* Exclude */
ELSE
SET @journeyFilter = 0 /* Allow */
ENDIF
]%%
4.SSJS Exclusion Example for Advanced Filtering
Use Server-Side JavaScript (SSJS) to exclude contacts with invalid phone numbers.
<script runat=”server”>
Platform.Load(“Core”,”1.1.1″);
vr email = Attribute.GetValue(“EmailAddress”);
var phone = Attribute.GetValue(“MobileNumber”);
/* Exclude if phone number is missing or incorrect format */
if (phone == “” || phone.length < 10) {
Variable.SetValue(“journeyFilter”, 1); /* Exclude */
} else {
Variable.SetValue(“journeyFilter”, 0); /* Allow */
}
</script>
5.How to Apply Exclusion Scripts in Journey Builder?
- Open Journey Builder
- Select the Entry Source (Data Extension, API, etc.)
- Click on “Exclusion Script” in the settings panel.
- Paste the AMPscript or SSJS code to filter contacts.
- Validate & Save the journey.
Best Practices for Exclusion Scripts
Use Data Extensions for Lookup instead of long exclusion lists.
Keep Scripts Simple to avoid processing delays.
Test Scripts Before Activation using Test Data Extensions.
Combine Multiple Exclusion Conditions for efficiency.
Use AMPscript for Simple Logic, SSJS for Advanced Filtering.