drupal / visitor_attributes_example
Adds a Haven page that shows different content to visitors from the spring and summer campaigns.
Package info
git.drupalcode.org/project/visitor_attributes_example.git
Type:drupal-recipe
pkg:composer/drupal/visitor_attributes_example
Requires
- drupal/canvas: ^1.12
- drupal/canvas_conditional_container: ^1.0.0-beta2
- drupal/context: ^5.0.0-rc4
- drupal/core: ^11.3
- drupal/haven_theme: ^1
- drupal/klaro: ^3.1
- drupal/visitor_attributes: ^1.0.0-alpha2
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-28 16:23:13 UTC
README
This recipe adds a Get involved page at /get-involved, for sites using
the Haven theme. The middle of the page depends on the campaign link the
visitor last arrived from:
utm_campaign=spring: Spring planting days are here.utm_campaign=summer: Summer shoreline clean-ups.- Any other campaign, no campaign, or no consent: Ways to help.
The page remembers the campaign on later visits, even when the URL no longer has the parameter. A new campaign link switches it.
It's an example of Visitor Attributes used with Canvas Conditional Container and Context. It uses a Condition Group: two conditional containers in priority order, of which only the first whose rules pass is shown, and a Default for everyone else.
Compare it with the Canvas Conditional Container Example
recipe, which reads utm_campaign from the URL with Condition Query. That
banner disappears as soon as the visitor moves to another page. This one
remembers the campaign, for visitors who allow it.
Don't use it for access control
Visitors control their own UTM parameters: anyone can add
?utm_campaign=spring to a link. Use visitor attributes to choose what to
show, never to hide private content or grant access.
What it adds
- The Canvas, Context, Klaro, Canvas Conditional Container, Visitor Attributes and Visitor Attributes Klaro modules.
- Visitor Attributes settings:
- Consent: Klaro decides whether attributes may be stored.
- utm_campaign allowlist:
springandsummer. This replaces any existing allowlist forutm_campaign. Add your other campaign values back at Configuration › System › Visitor attributes.
- Two Contexts in the group Campaigns: Spring campaign visitors and
Summer campaign visitors. Each is active when the visitor's
latest-touch
utm_campaignisspringorsummer. - A Canvas page at
/get-involved. Everyone sees an intro and a closing call to action. Between them, a Condition Group holds:- Variant 1: a conditional container using Spring campaign
visitors, with the spring banner and a button to
/projects; - Variant 2: a conditional container using Summer campaign visitors, with the summer banner and a button to the contact form;
- Default: a short "Ways to help" section.
- Variant 1: a conditional container using Spring campaign
visitors, with the spring banner and a button to
- The Use Contexts in conditional containers permission for the Content editor role, so editors can use Contexts in their own containers.
- A Context Home page (group Pages), active on the front page.
- A change to Haven's page template (
canvas.page_variant.theme_haven_theme): the footer's newsletter form moves into a conditional container that uses the Home page Context, so the form shows on the home page only. The heading and text above it stay on every page. See "The newsletter form" under "Things to know".
Requirements
- A Drupal CMS site built with the Haven site template. The page uses
components from the Haven theme (
haven_theme), and the permission goes to thecontent_editorrole. - Canvas Conditional Container 1.0.0-beta2 or later, for Condition Groups.
- Visitor Attributes 1.0.0-alpha2 or later, for the latest-touch attributes.
- Context 5.0.0-rc4 or later.
The page template change finds the newsletter form by the UUIDs Haven ships
it with. If you removed that form from the footer, or replaced it with
another one, applying the recipe fails. Remove the
canvas.page_variant.theme_haven_theme action from recipe.yml first.
Applying
drush recipe ../recipes/visitor_attributes_example
Letting visitors consent
Visitor Attributes stores nothing for a visitor until they allow the Visitor attributes service in Klaro. Without consent, a campaign counts only on the page whose own URL has it.
This recipe doesn't change Klaro's settings. Klaro's default dialog mode,
kept by Drupal CMS, is Silent: no consent dialog and no button. Drupal
CMS adds a My privacy settings link to the footer that opens Klaro, so
visitors can still allow the service there. If your site doesn't have that
link, go to Configuration › User interface › Klaro!
(/admin/config/user-interface/klaro) and either:
- choose another Klaro! Dialog Mode, such as Notice dialog or Consent dialog modal, or
- check Show Toggle Button.
The status report shows a warning while visitors have no way to consent.
Demo script
About ten minutes. It shows the page switching between campaigns, and, with Twig debugging on, how the rules were evaluated, in the page's HTML source.
Setup
Do this before the audience arrives.
- Apply the recipe (see "Applying") and rebuild caches:
drush cr. - Turn on Twig debugging. As an administrator, go to Configuration ›
Development › Development settings
(
/admin/config/development/settings). Note the current settings, so you can put them back: on a new site, all four checkboxes are unchecked.- Check Twig development mode. Twig debug mode and Disable Twig cache are checked with it; leave them checked.
- Leave Do not cache markup unchecked. It turns off Dynamic Page Cache, which step 9 shows.
- Click Save settings.
- Rebuild caches again:
drush cr. Saving the form doesn't clear pages that are already cached, and those have no debug comments. - Open a new private browser window, so you start as a new visitor with no cookies.
Reading the source. Open the page source (Ctrl+U, or Cmd+Option+U
on a Mac, or put view-source: before the URL) and search for Condition
Group. Twig debugging adds many other comments, about templates; skip
those. What to look for:
- The group's comment comes first and says which variant it showed, by
position and UUID:
shows variant 1,shows variant 2, orshows default. - Then one comment per variant the group checked, with each rule: the
Context's machine name and label, and
trueorfalse. Read the label, not the UUID. A shown container's comment comes before its content; a hidden container's comment replaces it. - Variants after the one shown have no comment: once a variant matches, the ones below it can't change the result, so they're removed.
- The footer's newsletter container adds a comment on every page. On
/get-involvedit sayshidden, because its Context is Home page.
Demo
Type or paste every URL into the address bar. Don't click links to change the campaign: see step 7.
Before consent. Visit
/get-involved?utm_campaign=spring. The spring banner shows, because the campaign is in this page's URL. Now visit/get-involved. The page shows Ways to help: without consent, nothing was stored. In the source:<!-- Condition Group … shows default: no variant matches. --> <!-- Conditional container … hidden: failed. Rule group 1, rule 1: context:spring_campaign_visitors (Spring campaign visitors), ok, false → false. Context "spring_campaign_visitors" is not active. --> <!-- Conditional container … hidden: failed. Rule group 1, rule 1: context:summer_campaign_visitors (Summer campaign visitors), ok, false → false. Context "summer_campaign_visitors" is not active. -->Consent. In the footer, click My privacy settings. Switch on Personalization, then click Save.
Spring. Visit
/get-involved?utm_campaign=spring. The spring banner shows. In the source, variant 1 matched, and summer has no comment:<!-- Condition Group … shows variant 1, … (container). --> <!-- Conditional container … shown: passed. Rule group 1, rule 1: context:spring_campaign_visitors (Spring campaign visitors), ok, true → true. Context "spring_campaign_visitors" is active. -->Remembered. Visit
/get-involved, with no parameter. The spring banner still shows.Summer. Visit
/get-involved?utm_campaign=summer. The summer banner shows, and visiting/get-involvedagain keeps it. In the source, spring was checked first and failed, then summer matched:<!-- Condition Group … shows variant 2, … (container). --> <!-- Conditional container … hidden: failed. Rule group 1, rule 1: context:spring_campaign_visitors (Spring campaign visitors), ok, false → false. Context "spring_campaign_visitors" is not active. --> <!-- Conditional container … shown: passed. Rule group 1, rule 1: context:summer_campaign_visitors (Summer campaign visitors), ok, true → true. Context "summer_campaign_visitors" is active. -->A campaign not on the allowlist. Visit
/get-involved?utm_campaign=autumn. The page shows Ways to help.autumnisn't on the allowlist, so it's stored asother, which matches neither variant. The source shows both containershidden: failed, thenshows default.- Links within the site don't change the campaign. Explain that only a
visit from outside the site starts a new touch: a typed or pasted URL, or
a link on another site or in an email. A link from one page of the site
to another doesn't, even with
utm_campaignin its URL, because pagers, filters and language links often keep the query string. To show it, use the curl commands under "Checking with curl". - Not access control. Anyone can type these URLs. Use this to choose content, never to protect it.
- Caching (optional). Open the browser's developer tools, then the
Network tab, and reload. Select the first request (the page itself)
and look at its response headers:
X-Drupal-Dynamic-Cache: HITorMISS. Dynamic Page Cache stores one copy of the page per campaign value, shared by every visitor with that value. The first request for a value after a cache rebuild is aMISS, later ones aHIT. Spring is already aHITin step 3, because step 1 stored the spring copy.X-Drupal-Cache: UNCACHEABLE (response policy). Pages that use a visitor attribute skip Internal Page Cache.
Restore
- Go to
/admin/config/development/settings, uncheck Twig development mode, and click Save settings. The other Twig checkboxes are unchecked with it. - Rebuild caches:
drush cr. Pages cached during the demo keep their debug comments until then. The comments name your Contexts, so they mustn't reach a production site. - Close the private window.
Checking with curl
Without a browser, use curl with Klaro's consent cookie. -k accepts a
local development certificate; replace the host with your site's.
# Allows the service, arrives on a spring link, and keeps the cookie.
curl -k -c jar.txt -b 'klaro=%7B%22visitor_attributes%22%3Atrue%7D' \
'https://example.ddev.site/get-involved?utm_campaign=spring' -o /dev/null
# Later visit, no parameter: the spring banner is in the page.
curl -k -b jar.txt -b 'klaro=%7B%22visitor_attributes%22%3Atrue%7D' \
https://example.ddev.site/get-involved | grep -c 'Spring planting days'
# A link from the site itself with summer: not a new touch, still spring.
curl -k -b jar.txt -c jar.txt -b 'klaro=%7B%22visitor_attributes%22%3Atrue%7D' \
-e https://example.ddev.site/about \
'https://example.ddev.site/get-involved?utm_campaign=summer' | grep -c 'Spring planting days'
# A summer link from outside: switches to summer.
curl -k -b jar.txt -c jar.txt -b 'klaro=%7B%22visitor_attributes%22%3Atrue%7D' \
'https://example.ddev.site/get-involved?utm_campaign=summer' -o /dev/null
curl -k -b jar.txt -b 'klaro=%7B%22visitor_attributes%22%3Atrue%7D' \
https://example.ddev.site/get-involved | grep -c 'Summer shoreline'
rm jar.txt
Each grep -c prints 1.
In the Canvas editor
The editor preview always shows both variants and the default, because the editor's request isn't a visitor's. A badge on the group says the first matching variant shows, top to bottom. Each container's badge is labeled Variant 1 or Variant 2 and lists its rules. Because the rules use a visitor attribute, the badge also says that pages showing the container bypass the page cache. The footer's newsletter container shows its own badge too, with the Home page Context.
To change the order, drag a container up or down in the Variants slot.
Try adding a variant
Visitor Attributes has more attributes to try in a third variant, each through a new Context:
- First-touch campaign (
first_utm_campaign): keeps the first campaign a visitor arrived from. Compare it with the latest-touch Contexts here. - Referrer (
referrer_domain):socialfor visitors from social networks,searchfor search engines. - Returning visitor (
returning_visitor):yesfor visitors first seen more than 30 minutes ago. - Country (
country): needs a geo provider, such as Visitor Attributes GeoIP. Without one, the country is empty and the variant never matches. - Site parameters: your own query parameter, such as
?partner=acme, at Configuration › System › Visitor attributes › Site parameters.
Put specific variants above general ones: the first match wins.
Things to know
- The whole page varies, not only the group. The group decides while the
page is built, so
/get-involvedisn't stored in Internal Page Cache and getsCache-Control: no-cache. CDNs that followCache-Controldon't store it either. Dynamic Page Cache still stores one version per value, so the page stays reasonably fast. Pages without the group keep Internal Page Cache. - The group varies by the variants it checked. A spring visitor's page depends only on the spring rule; a summer visitor's depends on both. Put rules with few possible values first.
- Context checks every Context on every page. The first build of each page reads the attribute and isn't stored in Internal Page Cache. Later requests for pages without the group are stored as usual. See "Context module" in the Visitor Attributes README.
- Don't add a Blocks reaction to these Contexts. Context doesn't yet vary its reactions by their conditions (Context issue #3044314), so a Blocks reaction here could show one visitor's blocks to others. Use containers or block visibility instead.
- Campaign names are matched without regard to case, so
utm_campaign=Springcounts. Values not on the allowlist are stored asother. - Latest touch, not first touch. The Contexts use the latest campaign link, so a new link switches the page. A visit with no campaign and no link from another site changes nothing. See "Latest touch" in the Visitor Attributes README.
- The newsletter form. Webform forms can't be cached: Drupal core marks every entity form, including Webform's, as uncacheable, and Canvas renders blocks in place rather than as placeholders. So any page that shows the form isn't stored in Dynamic Page Cache. Haven puts the form in the footer of every page, so without this change no page is stored there. With it, every page except the home page can be. The home page still isn't. To show the form on more pages, add them to the Home page Context, or move the form out of the container in the Canvas editor.
- Google Ads auto-tagging adds
gclid, notutm_*. Addutm_campaign=springto the ad's final URL or tracking template.