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); 
	%>
2 Replies

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

Avatar

Level 3

Hi @derekw42533281,

You are correct — the logic remains the same. You only need to change the field being evaluated to determine which unsubscribe URL should be rendered. Since this code will be used in a footer fragment, it is important to keep it clean, reusable, and scalable.

Instead of using multiple if statements, the recommended approach is to use a mapping-based solution, which is easier to maintain as more delivery types are added.


Sample Code (Best Practice)

 

<%
var unsubLink = "https://default-unsubscribe-link.com";

// Normalize the delivery label for consistent comparison
var deliveryLabel = context.delivery.label.toLowerCase();

// Map delivery identifiers to unsubscribe URLs
var unsubLinkMap = {
    "password": "https://password-unsubscribe-link.com",
    "paperless confirmation": "https://paperless-unsubscribe-link.com"
};

// Determine the correct unsubscribe link
for (var key in unsubLinkMap) {
    if (deliveryLabel.indexOf(key) !== -1) {
        unsubLink = unsubLinkMap[key];
        break;
    }
}

document.write(unsubLink);
%>

Why This Is Recommended

  • Scalable – New delivery conditions can be added easily

  • Reusable – Suitable for footer fragments across multiple templates

  • Maintainable – Avoids repetitive conditional logic

  • Safe – Case-insensitive comparison prevents mismatch issues


Additional Recommendation

If possible, consider using context.delivery.internalName instead of label, as labels can be modified by users and may impact the logic:

 

var deliveryName = context.delivery.internalName.toLowerCase();

This makes the solution more robust and less prone to breaking due to delivery name changes.

Thanks,
Giriprasath B