Expand my Community achievements bar.

Get ready! An upgraded Experience League Community experience is coming in January.

Adding multiple unsub links in a content block

Avatar

Level 4

Hey Everyone! 

 

I had a similar request a while back but for phone numbers. I think the code would be the same but would need to replace the field it's reading to determine the which unsub link it needs to direct to.

Looking for help to know what I need to use or if there is a better way. 
Also, this is going to be in a fragment we use for our footers.


Current code:

<%
  
var phoneNumber="1-800-xxx-xxxx";

if(context.delivery.label.toLowerCase().indexOf("password") >= 0){
		phoneNumber = "1-800-xxx-xxxx";
	}
if(context.delivery.label.toLowerCase().indexOf("paperless confirmation") >= 0){
		phoneNumber = "1-800-xxx-xxxx";
	}
    document.write(phoneNumber); 
	%>
1 Reply

Avatar

Level 2

<%

// 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}})