Within the Lift ecosystem, "contexts" can be thought of as pre-defined functionality that makes data available to the personalization tools, when that data exists in the current state (of the site/user/environment/whatever else).
The simplest use of contexts is in mapping their data to User Defined Fields (UDFs) on
/admin/config/content/personalize/acquia_lift_profiles. When the context is available, its data is assigned to a UDF field and included with Lift requests. For example, the Personalize URL Context module (part of the Personalize suite) does exactly this with query string contexts.
The first thing to do is to implement
hook_personalize_visitor_contexts(). These will make the Personalize module aware of your code, and will allow it to load your context declaration class.
Our module is called yuba_lift:
The latter hook tells Personalize that we have a class called
YubaLift located at
/plugins/visitor_context/YubaLift.inc (relative to our module's folder).
The context class
Our context class must extend the abstract
PersonalizeContextBase and implement a couple required methods:
getOptions method is what we're interested in; it returns an array of context options (individual items that can be assigned to UDF fields, among other uses). The options are grouped into a 'Yuba' group, which will be visible in the UDF selects.
With this code in place (and cache cleared - for the hooks above), the 'Yuba' group and its context options become available for mapping to UDFs.
Values for options
In the example above we hardcoded our values. In real use cases, the context options' values would vary from page to page, or be entirely omitted (when they're not appropriate) - this will, of course, be specific to your individual application.
With the values in place, we add them to a JS setting (
Drupal.settings.yuba_lift.contexts), and also load a JS file. You could store the values in any arbitrary JS variable, but it will need to be accessible from the JS file we're about to create.
The last piece of the puzzle is creating a new object within
Drupal.personalize.visitor_context that will implement the
getContext method. This method will look at the enabled contexts (provided via a parameter), and map them to the appropriate values (which were passed via
That's it! You'll now see your UDF values showing up in Lift requests. You may also want to create new column(s) for the custom UDF mappings in your Lift admin interface.
You can grab the completed module from my GitHub.