Magnolia 5.6 reached end of life on June 25, 2020. This branch is no longer supported, see End-of-life policy.
This page describes how to work with cookies in a light module, especially how to read, set and remove them. The page concludes the whole tutorial by providing instructions to create a page template and bits of JavaScript code for component personalization through the visits
cookie.
Normally cookie manipulations can be done with the default JavaScript API as described here. However, they can be accomplished more easily by using the JavaScript Cookie library, which we will apply in the code provided further below for cookie manipulations in a FreeMarker page template.
JavaScript Cookie, hereinafter referred to as js-cookie, is a lightweight JavaScript API designed specifically for handling browser cookies.
The library is available:
travel-demo-theme
and hence in any Magnolia bundle containing the magnolia-travel-demo
module. You could simply attach this theme to your template to use the library's functions in your light module. However, if your project is to be completely independent from this theme, you will need a copy of the library from GitHub and provide it in your light module.Handling cookies via this library is simple. To read, set or remove a cookie named foo
, for example, just the following commands need to be called from a JavaScript code attached to a web page:
Cookies.get('foo'); Cookies.set('foo', 'value'); Cookies.remove('foo');
See the usage section in the library's repository for a full list of commands defined for cookie manipulations.
In the following steps we will create page template files and a JavaScript file for manipulating the visits
cookie, and provide all of the files in a light module called visits-cookie-365
. You can also download the files from the cookie-trait-examples repository.
Create the following light module structure in the directory defined by the magnolia.resources.dir
property:
visits-cookie-365/ ├── templates/ │ └── pages/ └── webresources/ ├── css/ └── js/
Add the js.cookie.js file to the js
subfolder of the module. The css
subfolder together with its content will influence mainly the visual aspect of the page.
Remember that in our use case, we want a cookie to count the number of times a user visits a web page. The counting will be done by the following JavaScript code, while reading, removing and setting a cookie will be done via calls to the js-cookie library from the code:
function setVisitsCookie() { var visitsCookie = isNaN(Cookies.get('visits')) ? 1 : parseInt(Cookies.get('visits')) + 1; Cookies.remove('visits'); Cookies.set('visits', visitsCookie, { expires: 365 }); }
visits
cookie has been stored in the browser.1
as the value of the visitsCookie
variable.visits
cookie, increment it by 1
, and assign it as the value of the visitsCookie
variable.visits
cookie so as not to end up with two visits
cookies after executing the next step.visits
cookie and use the value kept in the visitsCookie
variable as the value of the cookie. Define that the cookie should expire in 365 days. This will make sure that the cookie will be kept in the browser not just for the duration of the current session. The value 365
has been chosen arbitrarily.Save this function as the visits.js
file to the js
subfolder in the module.
Create a page template with a template definition and a template script. To keep things simple, we will not add any dialog definition to the template.
templateScript: /visits-cookie-365/templates/pages/cookiePage.ftl renderType: freemarker visible: true title: Visits cookie 365 areas: main: type: list availableComponents: textImage: id: mtk:components/textImage
textImage
component. The component will be used to create content variants. Add this template definition as the cookiePage.yaml
file to the pages
subfolder in the module.Add the following template script as the cookiePage.ftl
file to the pages
subfolder in the module:
[#assign title = content.title!"Cookie test page"] <!DOCTYPE html> <html> <head> <title>Cookie Test Page</title> <link rel="stylesheet" href="${ctx.contextPath}/.resources/visits-cookie-365/webresources/css/visits.css" type="text/css"> <script type="text/javascript" src="${ctx.contextPath}/.resources/visits-cookie-365/webresources/js/js.cookie.js"></script> <script type="text/javascript" src="${ctx.contextPath}/.resources/visits-cookie-365/webresources/js/visits.js"></script> [@cms.page /] </head> <body onload="setVisitsCookie()"> <header> <h2>${title}</h2> <h4>Use the white area below to test component personalization.</h4> </header> <main> [@cms.area name="main"/] </main> </body> </html>
visits
cookie.#setVisitsCookie
function is called every time the page is loaded using the onload
attribute of the body
tag.cms.area
directive will render the components of this area. Congratulations! Now you have a light module with a template which will allow you to test component personalization based on cookie values.
Use the light module on a running Magnolia instance. Go to the Pages app and create a page with the template called Visits cookie 365.
Add a textImage
component to the page and personalize it, i.e. create several variants of it defined by the custom visits
cookie trait. The following screenshot, for example, shows a message for a variant called VISITS1.
Publish your page and check the result. The screenshot below shows page content for any value > 4 of the visits
cookie, as set in the website.Cookie-Test-Page.xml page configuration file available in the cookie-trait-examples repository: