Introduction
The Sensefuel JavaScript tag provides a plug-and-play solution for product search and personalized recommendations on your e-commerce website. It also automatically manages user tracking to optimize algorithms and deliver actionable analytical data.
In some cases, you may want to use our APIs to implement search, manage listing pages, or serve recommendations while still benefiting from the user tracking handled by our tag. This documentation explains how to retrieve the user ID generated by our tag and use it with our APIs.
Sensefuel Tag Features
The Sensefuel JavaScript tag ensures several key functions:
- Compliance with GDPR regulations
- Assignment of a unique user ID
- Assignment of a session ID
- Tracking of the shopping journey (searches, product listing views, product detail views, add-to-cart actions, order confirmations, etc.)
Retrieving the User ID
3.1. Accessing the Sensefuel JavaScript Interface
The user ID is accessible through a JavaScript interface exposed by the Sensefuel tag. This interface is available via the global variable window.sensefuel_a038457e.
To retrieve the user ID, use the following call:
window.sensefuel_a038457e.trackingInfo().user.id;
3.2. Precaution
Before accessing this interface, ensure that the tag is fully loaded on the page. If the call is made before the tag becomes available, retrieving the user ID will fail.
Using the User ID with Sensefuel APIs
We distinguish two use cases for Sensefuel APIs:
4.1. Dynamic Calls in Front-End (Client-Side Rendering)
In this case, the API is called dynamically at runtime from the browser (either directly or through an intermediate server you control).
- Wait until the
sensefuel_a038457einterface is available. - Retrieve the user ID:
const userId = window.sensefuel_a038457e.trackingInfo().user.id;
Pass this ID to our Discovery or Recommend APIs.
4.2. Calls from a Server (Server-Side Rendering)
For SEO and performance reasons, you may choose to call our APIs server-side to pre-generate personalized content.
- On the first page visited by a user, no user ID is available yet. The page can be generated without this ID.
- Once the page is loaded, the Sensefuel tag automatically generates a user ID.
- To send this ID to the server for subsequent visits, store it in a first-party cookie:
cookie = sensefuel_user_id=${window.sensefuel_a038457e.trackingInfo().user.id}; path=/; SameSite=Lax;
- The server can then retrieve this cookie on each request or page load and use the user ID to call our APIs.
The example cookie code is provided for guidance only. You may adjust the path, SameSite, Expires, Domain, and any other cookie options to comply with your own cookie policy (https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie).
4.2.1. User ID Updates
The Sensefuel JavaScript tag may regenerate a new user ID in certain cases, especially if the user refuses cookies according to GDPR rules. In this situation:
- A new ID will be generated for each session.
- Make sure to update the first-party cookie on each page load.
Conclusion
By following this documentation, you can retrieve the user ID generated by the Sensefuel tag and use it to personalize API calls. Whether on the client side or the server side, this integration ensures personalization of search results, list page ranking, and product recommendations through Sensefuel APIs.