<%
// Default unsubscribe URL
var unsubURL = "https://example.com/unsubscribe/default";
// Mapping rules based on delivery labels
var urlMapping = {
"password": "https://example.com/unsubscribe/password",
"paperless confirmation": "https://example.com/unsubscribe/paperless",
"billing": "https://example.com/unsubscribe/billing",
"promotion": "https://example.com/unsubscribe/promo"
};
// Normalize the label to lowercase
var label = context.delivery.label.toLowerCase();
// Matching logic
for (var key in urlMapping) {
if (label.indexOf(key) >= 0) {
unsubURL = urlMapping[key];
break;
}
}
document.write(unsubURL);
%>
This version replaces phone numbers with dynamic unsubscribe URLs.
It uses a mapping approach to match the delivery label to the appropriate unsubscribe link.
Here’s how it works:
✅ 1. Default URL
If no specific match is found, the script outputs a fallback URL:
var unsubURL = "https://example.com/unsubscribe/default";
This ensures the link always works, even if new types of emails are added without updating the mapping list.
✅ 2. Define a mapping object
Instead of many if/else blocks, we define pairs of:
keyword → corresponding unsubscribe link
This improves readability and future maintenance.
var urlMapping = {
"password": ".../password",
"paperless confirmation": ".../paperless",
"billing": ".../billing",
"promotion": ".../promo"
};
✅ 3. Normalize the delivery label
Converting to lowercase prevents mismatches:
var label = context.delivery.label.toLowerCase();
✅ 4. Match the keyword
The loop checks each mapping key.
If the label contains the keyword → corresponding link is applied.
for (var key in urlMapping) {
if (label.indexOf(key) >= 0) {
unsubURL = urlMapping[key];
break;
}
}
✅ 5. Output the value
Finally, output the selected URL:
document.write(unsubURL);
✅ Benefits (Why this is better than if/else)
✔ Cleaner logic
✔ Easier to scale — just add new items to mapping
✔ More readable for your team
✔ Reduces human error
✔ Ideal for shared fragments
✔ Automatically falls back to default URL
✅ Need advanced version?
If you want, I can generate:
✅ regex-based matching
✅ multiple fallback rules
✅ mapping by deliveryCode instead of deliveryLabel
✅ nested conditions (e.g., country + label)
✅ dynamic personalization with parameters (e.g., &cid={{context.profile.customer_id}})