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.
Id | Page name | End date | Redirect |
1 | My awesome page | 5/2/2019 6:56:38 AM | https://example.com |
2 | My awesome page | 5/30/2019 12:00:00 AM | https://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:
- Get the current date.
- Get the records from the Data Extension where the Page name is a match.
- Loop through every record and retrieve the End date and the redirect URL.
- 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.
Hi, I am trying to copy the whole code and DE but it doesn’t even work in that without editting anything. Is there a reason with this error during publish,
An error occurred while previewing this content.
This can happen for many reasons, including incomplete or incorrect MC scripting (AMPscript, SSJS or GTL) or missing subscriber context.
Click Cancel to review your code or Publish to push the updated content live.
Don’t worry about the preview. When there is a redirect on the page it generally never works.