Skip to content

Commit 1887149

Browse files
IBX-11237: websiteId section added (#3281)
* websiteId section added * typos * code language fixed * fixes, content added * fixes after rev * fixes after doc rev * fixes + rector * rector fix
1 parent ed5d949 commit 1887149

1 file changed

Lines changed: 83 additions & 1 deletion

File tree

docs/templating/twig_function_reference/recommendations_twig_functions.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,91 @@ ibexa_tracking_track_event(
7474

7575
- **eventType** - type: string, defines the type of tracking event to be sent, for example, `visit`, `contentvisit`, `buy`, `basket`, `itemclick`. For more information, see [Tracking events for recommendations](https://content.raptorservices.com/help-center/tracking-events-parameters-reference).
7676
- **data** (optional) - type: mixed, accepts the primary object associated with the event, such as a Product or Content, can be null if not required. For more information, see [tracking event examples](#tracking-events).
77-
- **context** (optional)- type: array, additional event data, such as quantity, basket details, or custom parameters. For more information, see [example usage](#context-parameter-example-usage).
77+
- **context** (optional)- type: array, additional event data, such as quantity, basket details, [website ID](#websiteid-parameter), or custom parameters. For more information, see [example usage](#context-parameter-example-usage).
7878
- **template** (optional) - type: string, path to a custom Twig template used to render the tracking event, allows overriding the default tracking output.
7979

80+
### `websiteId` parameter
81+
82+
The `websiteId`, also known as a **Login ID**, (`p7`) parameter for [Raptor tracking](https://content.raptorservices.com/help-center/introduction-to-tracking-documentation) can be optionally provided as a **context** to `ibexa_tracking_track_event()` function.
83+
84+
When storing customer data in an external Customer Relationship Management (CRM) system, set the `websiteId` to an identifier of the customer stored there.
85+
86+
The following example shows how you pass that value, assuming a custom Twig function `get_custom_crm_identifier` integrating with that CRM exists:
87+
88+
``` html+twig
89+
{# Section rendered only for logged-in users #}
90+
{{ ibexa_tracking_track_event('visit', product, {
91+
websiteId: get_custom_crm_identifier(ibexa_user_get_current().login)
92+
}) }}
93+
```
94+
95+
Set the `websiteId` parameter for logged-id users, for which you have data uniquely identifying them.
96+
The value of this parameter serves as a persistent, cross-device identifier of the user.
97+
Example values are [User ID](https://content.raptorservices.com/help-center/user-tracking-understanding-soft-ids-hard-ids-raptor-identity-matching#:~:text=IDs%3A-,UserId%20%28Website%20ID) or the Cookie ID.
98+
99+
The value of `websiteId` parameter is resolved in the following order:
100+
101+
1. Explicit `websiteId` passed in the `ibexa_tracking_track_event()` context.
102+
2. Custom [`WebsiteIdContextProviderInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-ConnectorRaptor-Tracking-ContextProvider-WebsiteIdContextProviderInterface.html) implementations (the first one returning a non-null value wins).
103+
3. The built-in provider, which uses the logged-in user's identifier (`ruid`).
104+
105+
If no value is resolved, the event is sent without the `p7` parameter.
106+
107+
To resolve `websiteId` on the project level, implement the interface as follows:
108+
109+
``` php
110+
namespace App\Tracking;
111+
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
112+
use Ibexa\Contracts\Core\Repository\PermissionResolver;
113+
use Ibexa\Contracts\ConnectorRaptor\Tracking\ContextProvider\WebsiteIdContextProviderInterface;
114+
final readonly class MyCrmWebsiteUserIdProvider implements WebsiteIdContextProviderInterface
115+
{
116+
public function __construct(
117+
private ConfigResolverInterface $configResolver,
118+
private PermissionResolver $permissionResolver,
119+
)
120+
{
121+
}
122+
public function getWebsiteId(): ?string
123+
{
124+
$currentUserId = $this->permissionResolver->getCurrentUserReference()->getUserId();
125+
// Don't resolve for anynomous user
126+
if ($this->isAnonymousUser($currentUserId)) {
127+
return null;
128+
}
129+
return $this->getWebsiteUserIdForCurrentUser($currentUserId);
130+
}
131+
/**
132+
* @phpstan-return non-empty-string
133+
*/
134+
private function getWebsiteUserIdForCurrentUser(int $userId): string
135+
{
136+
// Implement custom logic resolving user identifier from the CRM
137+
return 'custom-identifier-for-the-user-retrieved-from-the-CRM';
138+
}
139+
private function isAnonymousUser(int $userId): bool
140+
{
141+
return (int) $this->configResolver->getParameter('anonymous_user_id') === $userId;
142+
}
143+
}
144+
```
145+
146+
The provider is registered automatically.
147+
Implementing the interface is sufficient, no service configuration is required.
148+
149+
!!! note
150+
151+
Custom provider takes precedence over the built-in one.
152+
A provider must return either `null` or a non-empty string.
153+
154+
If you register multiple providers, control their order by tagging the service with a priority (higher priority is checked first):
155+
156+
``` yaml
157+
App\Tracking\MyWebsiteIdProvider:
158+
tags:
159+
- { name: ibexa.connector.raptor.tracking.website_id_context_provider, priority: 50 }
160+
```
161+
80162
### Tracking events
81163
82164
The following events are supported and can be triggered from Twig templates:

0 commit comments

Comments
 (0)