How to Set A Cookie in SSJS

This post is related to a StackExchange question with the same name: How to Set A Cookie in SSJS. Using Server-Side Javascript, you can set a session cookie in Salesforce Marketing Cloud.

Use Case:

On a CloudPage, you may want to set a cookie so that if a user revisits the page, they can be shown something else on the page or redirected to a different page.

Caveats:

This SSJS function only allows you to set session cookies and not persistent cookies. This means that if the user closes their browser, the cookie will be deleted and you’ll lose the relevant information stored in that cookie.

SSJS

The SetCookie function allows you to set the cookie name, cookie value, and date and time when the cookie expires. The boolean value of ‘true’ is optional to indicate whether or not the cookie only works with an HTTPS connection.

					


As Jörn Berkefeld mentions in the linked StackExchange post, the date can also be manipulated using SSJS to make it more readable.

Properties are below. Values 3 and 4 (the date and time and HTTPS connection) are optional.

					


Leave a Reply

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