How to create an expiration date for Cloud pages

How to create an expiration date for Cloud pages

An expiration date is used to prevent the customers from accessing a Cloud page by the end of a marketing campaign and redirect them to another URL.

Unfortunately, Marketing Cloud doesn’t provide this feature out-of-the-box, but thanks to AMPscript and server-side Javascript, creating a seamless expiration date flow is somewhat easy.

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

How it works

Each marketing campaign has an end date and we need to make sure that after this date, no extra data is recorded by the Cloud pages.

Therefore, we need to compare the end date to the current date and create a redirect to a predefined URL.

We can use a Data Extension to store this data and then access it in the beginning of every Cloud page to check if the end date has passed.

Data extension

In order to store our data efficiently and allow non-technical users to modify or add data on the fly, we only need 4 fields.

Id field is a Primary Key, used to be able to edit the record through the Marketing Cloud interface.

Page name field is the lookup parameter we are going to use for identifying the records related to our page.

End date is a date field, that can be filled out by clicking on the field from the Data Extension interface and choosing the pop-up calendar date. Note that the interface doesn’t allow us to enter the hours and minutes yet, but it doesn’t really matter in this use case.

Redirect is a simple text field where we store the redirect URL.

Now, let’s create this Data Extension, name it DatesDE, and fill it out with the following data.

IdPage nameEnd dateRedirect
1My awesome page5/2/2019 6:56:38 AMhttps://example.com
2My awesome page5/30/2019 12:00:00 AMhttps://lipsum.com

Comparing the dates and retrieving the records

In AMPscript and server-side Javascript, retrieving the data from the Data Extension is a walk in the park. All we need is to specify the Page name lookup parameter (which should be unique for each Cloud page) and retrieve the field data from each record using the for loop.

Then, the date comparison method differs between the 2 languages.

In AMPscript, the DateDiff function is perfect for a date comparison. Depending on your needs, it can return a number in hours, days, months or years. The Now function returns the system date that can be easily converted to a local date using SystemDateToLocalDate.

%%[
	SET @Now = SystemDateToLocalDate(NOW())
	SET @EndDate = DateParse("5/2/2019 6:56:38 AM")
	IF DateDiff(@Now,@EndDate,"D") > 0 THEN
		/* Do something */
	ENDIF
]%%

In server-side Javascript, on the other hand, we first need to use DateTime in combination with SystemDateToLocalDate and Now to retrieve the current date.

As for the date comparison, Javascript supports it natively, given that both varibles are Date objects.

<script runat="server">
	Platform.Load("core","1.1");
	var Now = DateTime.SystemDateToLocalDate(Now());
	var EndDate = new Date("5/2/2019 6:56:38 AM");
	if (Now < EndDate) {
		/* Do something */
	} 
</script>

Full code

Both AMPscript and server-side Javascript perform the same actions:

  1. Get the current date.
  2. Get the records from the Data Extension where the Page name is a match.
  3. Loop through every record and retrieve the End date and the redirect URL.
  4. For each record, compare the current date with the End date and redirect the customer to a new URL if the current date is past the End date.

AMPscript

<!doctype html>
<html>
<body>
	%%[
    	SET @Now = SystemDateToLocalDate(NOW())
		SET @EndDateRows = LookupRows("DatesDE","Page name","My awesome page")
		IF ROWCOUNT(@EndDateRows) > 0 THEN
        	FOR @i = 1 TO ROWCOUNT(@EndDateRows) DO
              SET @EndDate = FIELD(ROW(@EndDateRows,@i),'End date')
              SET @RedirectURL = FIELD(ROW(@EndDateRows,@i),'Redirect')
              IF DateDiff(@Now,@EndDate,"D") > 0 THEN
                  Redirect(@RedirectURL)
              ENDIF
            NEXT @i
		ENDIF
	]%%
	<h1>Expiration date w/ AMPscript</h1>
</body>
</html>

Server-side Javascript

<!doctype html>
<html>
<body>
	<script runat="server">
        Platform.Load("core","1.1");
      	var Now = DateTime.SystemDateToLocalDate(Now());
		var DatesDE = DataExtension.Init('DatesDE');
        var EndDateRows = DatesDE.Rows.Lookup(['Page name'],['My awesome page']);
      	if(EndDateRows.length > 0) {
          	for(i = 0; i < EndDateRows.length; i++)  {
        		var EndDate = new Date(EndDateRows[i]['End date']);
        		var RedirectURL = EndDateRows[i]['Redirect'];
                if (Now < EndDate) {
                  	Redirect(RedirectURL,false);
                } 
            }    
        }
	</script>	
	<h1>Expiration date w/ SSJS</h1>
</body>
</html>

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 *

email
Up Next:

How to send a Triggered Send email with server-side Javascript

How to send a Triggered Send email with server-side Javascript