How to create a link with an expiration date using AMPscript

How to create a link with an expiration date using AMPscript

This article explains how to create a link in an email message with an expiration date using AMPscript in Salesforce Marketing Cloud.

Not a fan of reading? Jump to the code snippet.

How it works

In order to create a link with an expiration date, we need to build an email with an encoded link and a page that will redirect the customer to another page upon success or fail.

Email message

There is no need to use Base64Encode or any other encoding method if we use the CloudPagesURL function.

Please remember that adding RedirectTo function allows us to avoid any errors when additional parameters are added automatically to every link of the email (tracking or likewise).

The date parameter printed by the Now function will represent the date of when the email was sent and provides us with a reference point.

%%[
	SET @RedirectPageID = 1234
	SET @SendDate = SystemDateToLocalDate(NOW())
	SET @ExpiredIn = 1
	SET @Destination = 'http://example.com'
	SET @Fallback = 'http://google.com'
]%%
<a href="%%=RedirectTo(CloudPagesURL(@RedirectPageID,'SendDate',@SendDate,'ExpiredIn',@ExpiredIn,'Destination',@Destination,'Fallback',@Fallback)=%%">
	Click me
</a>

Redirect page

The only goal of this page is to check if the link has expired and redirect the customer to another page, where the data will be processed accordingly.

Once we capture the send data and the number of days after which the link is expires, it’s very easy to create a comparison using the DateDiff function.

%%[
    SET @SendDate = QueryParameter("SendDate")
	SET @ExpiredIn = QueryParameter("ExpiredIn")
	SET @Destination = QueryParameter("Destination")
	SET @Fallback = QueryParameter("Fallback")
    SET @Today = SystemDateToLocalDate(NOW())

    IF DateDiff(@SendDate,@Today,'D') >= @ExpirationInDays THEN
    	Redirect(@Destination)
    ELSE
    	Redirect(@Fallback)
    ENDIF    
]%%

Note that the DateDiff function will result in the error 500 if one of the compared values is null or empty. It’s always better to add an extra condition in order to avoid our code being broken.

%%[
    SET @SendDate = QueryParameter("SendDate")
	SET @ExpiredIn = QueryParameter("ExpiredIn")
	SET @Destination = QueryParameter("Destination")
	SET @Fallback = QueryParameter("Fallback")
    SET @Today = SystemDateToLocalDate(NOW())

	IF NOT EMPTY(@SendDate) AND NOT EMPTY(@ExpiredIn) THEN
		IF (DateDiff(@SendDate,@Today,'D') >= @ExpiredIn) THEN
			Redirect(@Destination)
		ELSE
			Redirect(@Fallback)
		ENDIF 
	ELSE
		Redirect(HTTPRequestHeader("Referer"))
	ENDIF 
]%%

That’s it folks! This example supports the date difference in days, but you are obviously free to use whatever time measure you like.

Have I missed anything?

Please poke me with a sharp comment below or use the contact form.

  1. Its Throwing an error (Im New to Ampscript and SFMC )
    Add Content Preview and TestBACK
    ×The subscriber preview failed to generate. Review the details, correct all issues, and try again.
    An error occurred when attempting to resolve a function call. See inner exception for detail. Function Call: RedirectTo(CloudPagesURL(@RedirectPageID,’SendDate’,@SendDate,’ExpiredIn’,@ExpiredIn,’Destination’,@Destination,’Fallback’,@Fallback) Index: 15555 TransCode Type: HTML Content Type: HTML Substitution Level: Subscriber Message Context: Preview
    At least one open parenthesis [(] does not have a matching close parenthesis [)] Function Call: RedirectTo(CloudPagesURL(@RedirectPageID,’SendDate’,@SendDate,’ExpiredIn’,@ExpiredIn,’Destination’,@Destination,’Fallback’,@Fallback) Parameter Value: CloudPagesURL(@RedirectPageID,’SendDate’,@SendDate,’ExpiredIn’,@ExpiredIn,’Destination’,@Destination,’Fallback’,@Fallback Missing Close Parenthesis Count: 1

Leave a Reply

Your email address will not be published. Required fields are marked *

Salesforce Marketing Cloud
Up Next:

Best tips and tricks in AMPscript and server-side JavaScript

Best tips and tricks in AMPscript and server-side JavaScript