Requesting personalized content with the delivery endpoint
This page describes configuration aspects essential for requesting personalized content with the Delivery API. Personalization in the Delivery API is a DX Core feature provided by the Personalization module in version 2.1.0 and higher.
To be able to use personalization in the Delivery API, you should:
Add the Blossom dependency (magnolia-personalization-blossom-compatibility) only if you are using the Blossom modules in your project:
Should you need to specify the module version, do it using <version>.
Make sure that in the content of your pom.xml for your webapp you include the following two dependencies if your project is generated from the Magnolia webapp archetype:
...
<dependencies>
...
<!-- include p13n for rest --><dependency><groupId>info.magnolia.personalization</groupId><artifactId>magnolia-personalization-rest</artifactId><version>2.1.1</version></dependency></dependencies>
...Copy
If you use personalization version 2.1.0, you also need to add the following dependencyManagement into your webapp’s pom.xml file.
...
<dependencyManagement><dependencies><!-- include dependency management for p13n 2.1.0 --><dependency><groupId>info.magnolia.personalization</groupId><artifactId>magnolia-personalization-parent</artifactId><version>2.1.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
...Copy
Configuration
Delivery of personalized content is based on sending one or more traits to an endpoint configured to handle personalized REST requests.
Endpoint configuration
In the configuration of an endpoint that is expected to handle personalized content, you must set the personalized property to true. See line 7 in the following example:
No personalization variants are returned if you address a root node, fo example:
host:port/.rest/delivery/pagesnav/v1/
If you requested a subpath of the root (host:port/.rest/delivery/pagesnav/v1/start), personalized content variant would be returned.
Sending a request to a URI that ends with just the endpointPath invokes the Query nodes method. It is irrelevant if you add the trailing slash (/) or omit the slash to the endpointPath.
In this case, the REST endpoint behaves as a query. That’s why we introduced the @nodes handle that you can use to request a root node with personalized content.
Headless personalization and caching
For headless personalization to work correctly with content caching in Magnolia, you have to set /modules/cache/config/contentCaching/<your-configuration>/cachePolicy@includePersonalizedDescendants to true.
Magnolia does not currently support caching for different headers or cookies. Therefore, it is impossible to specify for which headers or cookies a response should be cached.
To request personalized page or component variants, you need to configure at least one personalization trait. Additionally, a personalized page or component variant must be defined. The variant will be matched with a configured trait.
Once a trait has been configured, it can be sent in a REST request in one of the following forms:
A cookie.
A request header.
A request parameter (a query string of a GET request or body parameters of a POST request).
Traits are matched strictly by their names and values. In a trait configuration part of a module, trait rule and value fields must be preconfigured as single-value fields, for example as a textField or a comboBoxField. See the two example trait configurations below.
Any module can register a trait. For more details and trait configuration properties, see Registering a trait.
The traits that were present in the older (pre-2.1) versions of the Personalization module can also be used:
date
cookies
country
visitor
These traits are now provided by the magnolia-personalization-compatibility submodule.
Due to session security restrictions, the country trait can only be used if Magnolia and an SPA application are on the same domain.
Example 1: Cookie type trait called color
<light-module>/traits/color.yaml
$type:cookieTrait# name: is omitted as it is taken from the name of the yaml fileCopy
The following example is based on trait and content configurations available in the demo-rest-p13n, a light module showcasing personalization over REST in Magnolia. For more information how to install the demonstration module, see the README.md file in it.