graduation-capSession-context Tutorial

How to configure the Session Context Service

By default, an anonymous user session expires after one hour. You can refresh the anonymous session by sending a request to the Refreshing an anonymous tokenarrow-up-right endpoint.

circle-exclamation
circle-exclamation

How to manage user sessions and session context files

Sessions and session context files can be managed both by your customers and employees.

In the following scenario, we are going to perform the following actions from the perspective of an employee:

  1. Create one of the following user sessions (these requests are normally sent on the storefront, but are here for illustrative purposes):

circle-exclamation

Create an anonymous user session

An anonymous user session starts when a non-logged user enters the storefront and sends a request to the Requesting an anonymous tokenarrow-up-right endpoint. That request returns only an anonymous access token, no session context is created yet.

circle-check
rectangle-terminalAPI Referencechevron-right
circle-exclamation

Once a session context has been created (when a cart was created), it contains the following information:

  • cartId — present when a cart was created (session context is created at that time).

  • customerId — this field is empty for anonymous users.

  • currency — fetched from the site settings of the tenant's default site.

  • targetLocation — fetched from the home base address specified in the tenant's default site's settings.

  • siteCode — fetched from the site settings of the tenant's default site.

An anonymous customer's session is terminated if one of those two requirements is met:

  • The customer has logged in.

  • The session has reached its lifetime.

Create a customer session

The session is created when a customer logs in to the storefront. This means that the anonymous session that was established when the user entered the site is migrated into a customer session when a request is sent to the Logging in a customerarrow-up-right endpoint.

circle-exclamation
circle-check
rectangle-terminalAPI Referencechevron-right

Retrieve a session context by using a session Id

The session context is created when a cart is created - when a customer adds an item to the cart. The session-context details can be retrieved using the sessionId and include the following information:

  • cartId — if a cart was created during the anonymous session, the cart's Id is present. If not, adding products to cart triggers the Session Context Service, creates the session context if needed, and adds a cartId.

  • customerId — retrieved from the saas token returned during login, based on the user password provided.

  • currency — fetched from the customerprefferedCurrency entry in the customer's profile.

  • targetLocation — fetched from the customer's profile. Either default shipping address, or default billing address. Empty if none of those exist.

  • siteCode — fetched from the customerprefferedSite entry in the customer's profile.

circle-exclamation

The customer's session is terminated when the customer logs out.

1

Get the session ID

When retrieving the session-context details, you must supply the sessionId. It can be retrieved from the storefront or by sending the request to the Retrieving own session contextarrow-up-right endpoint with the customerToken - in the request. Then, the response includes the sessionId.

Example:

The response is:

2

Retrieve the session context by sending a request to the Retrieving a session contextarrow-up-right endpoint. Use the sessioncontext.context_manage scope.

circle-check

The response is:

circle-check
rectangle-terminalAPI Referencechevron-right

Add new attributes to the session context

If you want to personalize user experience on your website, you can add custom attributes to selected session contexts. Your attributes are not validated, as they are only stored in the Emporix Commerce Engine and can be deleted at any time. However, you can make the data actionable by incorporating a solution of your choice.

Example:

If you want to suggest products on the storefront that are relevant to the weather conditions near the user's location, you can add two new attributes: latitude and longitude to the session context. The data can be collected on the storefront every time a user enters your website, and it will be kept in the Emporix Commerce Engine. You can then implement a mechanism that uses the collected data, for example to display relevant products first, according to the weather.

For example, it's raining in the user's location, so the list of suggested products on the storefront prioritizes umbrellas and other rain equipment.

circle-exclamation

In the following example, we add an attribute by sending a request to the Adding a new attribute to a session contextarrow-up-right endpoint with the sessioncontext.context_manage scope (with underscore in session_context).

circle-check
rectangle-terminalAPI Referencechevron-right
circle-exclamation

Last updated

Was this helpful?