Creating custom traits without Java

You can use personalization in Magnolia without writing any Java code by using the cookie trait for just about any kind of personalization feature.

The cookie trait supports multiple cookies. You can configure as many cookies as you like for multiple use cases and use them simultaneously. Cookies are supported also in headless scenarios.

Version 2.1.0 of the Personalization module provides two implementations of cookie traits.

In the new implementation, the Cookie trait leverages a generalized NameAndValueTraitDetectorFilter that is also used to retrieve traits from the request header (collectHeaders) and request parameters (collectRequestParameters). The NameAndValueVoter is then used to render the appropriate content variation according to configuration.

In addition, this implementation comes with the @TraitType annotation for easier configuration:

<light-module>/traits/color.yaml
$type: cookieTrait
# name: is omitted as it is taken from the name of the yaml file
Use this implementation for personalization of headless SPA projects.

The new implementation is part of the magnolia-personalization-traits submodule.

The deprecated approach is part of the magnolia-personalization-compatibility submodule and was originally introduced in version 1.1.2 of the Personalization module.

The Cookies trait (info.magnolia.personalization.cookie.Cookies) uses a specialized CookiesDetectorFilter that can read cookies from the HTTP request with httpServletRequest.getCookies(), but cannot collect the traits from the request header or request parameters. The CookieVoter is then used to render the appropriate content variation according to configuration.

The Creating a custom cookie trait without Java tutorial takes you through the process of creating, setting and using custom cookies. 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.

The DX Core Demo bundle has two example cookie traits:

  • tourType, used in the Travel Demo to set user’s tour type preference.

  • weatherLocality

Feedback

DX Core

×

Location

This widget lets you know where you are on the docs site.

You are currently perusing through the DX Core docs.

Main doc sections

DX Core Headless PaaS Legacy Cloud Incubator modules