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.

Pay me a coffee

Want to say thanks? Pay me a coffee! Remember, I turn coffee into code.

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