drupal / visitor_attributes_eca_example
Copies the first-touch campaign, landing section and country onto new user accounts when visitors register.
Package info
git.drupalcode.org/project/visitor_attributes_eca_example.git
Type:drupal-recipe
pkg:composer/drupal/visitor_attributes_eca_example
Requires
- drupal/core: ^11.3 || ^12
- drupal/eca: ^3.1
- drupal/visitor_attributes: ^1.0.0-alpha4
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 01:10:09 UTC
README
This recipe copies three first-touch attributes onto a new user account when a visitor registers:
- the first-touch UTM campaign, into First-touch campaign
(
field_first_utm_campaign); - the landing section, into Landing section (
field_landing_section); - the country, into Country (first touch) (
field_first_country).
It's an example of lead attribution with Visitor Attributes and ECA. The same pattern works for other entities, such as a Webform submission or a Commerce order (see below).
Don't use it for access control
Visitors control their own UTM parameters and most other attributes: anyone
can add ?utm_campaign=spring to a link. Use the copied values for
reporting and follow-up, never to grant a role, a permission or access to
anything.
What it adds
- The ECA, ECA Content, ECA Misc, Visitor Attributes and Visitor Attributes ECA modules.
- Three plain text fields on user accounts. They aren't added to the user form or the user page, so people can't fill them in themselves, and only administrators see them (see "Seeing the values" below).
- One ECA model, Visitor attributes: lead attribution at registration.
It doesn't change the Visitor Attributes consent settings, Klaro, or who may register.
How the model works
On presave of a user account, the model checks, in order:
- The account is new. Later saves of the account (edits, logins, password resets) are ignored.
- The request is the registration page (route
user.register). Accounts created any other way are ignored. When an administrator creates an account, the request carries the administrator's cookie, so without this check the account would get the administrator's attributes. Drush, migrations and web service requests don't come from the new user's browser. SSO modules create accounts on their own routes: to cover them, see "Copy at login as well" below. - For each attribute, the attribute is not withheld. Without the
visitor's consent, attributes read as
unknown; the model then stores nothing rather than the word "unknown". - The field is empty, so a value set some other way is kept.
It then reads the attribute with Visitor: get attribute and sets the
field from that token. The get action gives the raw value. A [visitor:*]
token inside other text is HTML-escaped by Drupal's token system, so a
campaign called Q4 & sale would be stored as Q4 & sale.
It uses presave, not insert: the values are set before the account is saved, so the model doesn't have to save the account a second time.
An attribute that was never captured gives an empty field.
Requirements
- Visitor Attributes 1.0.0-alpha4 or later. The model uses the condition
visitor_attributes_is_not_withheld, which earlier releases calledvisitor_attributes_is_known. If you copy the model into a site on an earlier release, use the old ID. - A Visitor Attributes consent mode must be chosen (Configuration › System ›
Visitor attributes). Until then every attribute reads as
unknownand the model stores nothing. - Visitors must be allowed to register (Configuration › People › Account settings, Who can register accounts?).
- With a consent integration such as Klaro, values are kept between pages only for visitors who allow the Visitor attributes service. Without consent, only the registration page's own URL and referrer could be used, and that page is rarely the one the visitor arrived on.
- The country needs a geo provider (Visitor Attributes settings, Country). Without one the field stays empty.
Applying
drush recipe ../recipes/visitor_attributes_eca_example
Seeing the values
The fields aren't shown anywhere by default. To see them, add them to the
user page at Configuration › People › Account settings › Manage display, or
read them with Drush (replace 3 with the user ID):
drush php:eval "\$u = \Drupal\user\Entity\User::load(3); print \$u->get('field_first_utm_campaign')->value . ' / ' . \$u->get('field_landing_section')->value . ' / ' . \$u->get('field_first_country')->value . PHP_EOL;"
If you show them on the user page, remember that anyone who can view user profiles will see them.
Adapting it
- Copy at login as well. Add a second model on the User login event
(ECA User) that sets the same fields on the logged-in account when they're
empty. Keep the "is not withheld" and "field is empty" checks.
With SSO, this works when the provider sends the visitor back with a
redirect, as OpenID Connect usually does. When the provider posts the
visitor back from its own site, as SAML's POST binding does, the browser
doesn't send the visitor attributes cookie (it's
SameSite=Lax), so the stored attributes can't be read on that request. - Webform. Submission values aren't entity fields. With ECA Webform, set the submission's element values on presave of the submission. Check that the ECA Webform release you use supports your ECA version. Drafts are saved before the final submit, so also check that the submission is new, or that the element is empty.
- Core contact forms are never saved (
contact_messagehas no storage), so presave and insert never fire. Use the Contact Storage module, or form events (the values then reach the email but aren't stored). - Commerce. Payment gateway notifications come from the payment provider and carry no visitor cookie. Copy the attributes earlier, for example when the order is placed at checkout.
Removing it
Uninstalling the modules doesn't remove the fields or the model. Delete the model at Configuration › Workflow › ECA, and the fields at Configuration › People › Account settings › Manage fields (this deletes the stored values).