You can use personalization in Magnolia without writing any Java code by using the cookie trait.

This page and its subpages explain the cookies trait and how to use it to suit your site. Tutorial: Creating a custom cookie trait provides an example custom cookie trait and takes you through the process of creating, setting and using custom cookies.

Magnolia provides the cookie trait out of the box. The trait is part of the Personalization module. You can use this trait for just about any kind of personalization feature. 

Magnolia's cookie trait

Magnolia's cookies trait Cookies reads data from cookies with the CookieParameterConverter and uses the CookieVoter to render the appropriate content variation according to configuration.

All required classes for the cookie trait are part of the magnolia-personalization-traits submodule  which is installed with the Personalization module. The cookie trait supports multiple cookies. You can configure as many cookies as you like for multiple use cases and use them simultaneously. 

There is no need to write further Java classes. All you need to do to create a custom cookie is to is adapt the cookie trait configuration and write some FreeMarker and JavaScript code. You can include your code in a light module. 

What is a cookie?

A cookie is a text file containing information about a specific website visit that is kept by the browser on the user's computer. When the browser next requests a page on the website, it sends the file to the web server. Cookies allow you to record visitor information and serve targeted content based on them. The information is stored in a browser cookie. 

Session cookies last only as long as a user is on a website and expire when the browser window is closed or the session times out. Persistent cookies (a.k.a tracking cookies) remain active for a period of time and are used whenever the website is accessed. 

Cookie traits in Magnolia demo

The DX Core Demo bundle has two example cookie traits:

  • tourType: Used in the Travel demo to set user's tour type preference.
  • weatherLocality: Additional example.

This video provides an overview of how cookies work and demonstrates how the tourType cookies is used on the Travel demo to serve personalized content based on a user's tour type preference.  


#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))