This usecase describes example steps for using the API to retreive chat history for existing logged-in users. Additionally, visitor sessions should only last for the duration of their browser session. A logout function will reset the chat to an empty state and treat the visitor as a new customer.

1. Setup Session-Based ID

Chatvisor gives any user who visits a page which has our JavaScript implemented their own ID. Usually, these IDs are stored and read from localstorage and can last long. In this case, we want any user who is not logged in to always be presented with a completely fresh Chat session, even if they have had a conversation before.

To achieve this, Chatvisor has to be switched to "Session-Based IDs", which are stored in sessionstorage and deleted once the user closes the browser window.

More information on this setting can be found here.

2. Add tagging code to the application

On a page where the user is signed in, add a call to the tagging method with user data available from your application. This adds user information to an existing browser session.

CV.user.tag({
    "email": "[email protected]",
    "firstname": "John",
    "lastname": "Doe",
    "labels": []
})

In your logout function, include a call like this in order to reset the user session. This removes the current chat history from their chat window and treats the user as a new customer from then on.

CV.user.reset();

If you want to assign a customer to a specific agent, you can include the assignedUser property like this:

CV.user.tag({
    "email": "[email protected]",
    "firstname": "John",
    "lastname": "Doe",
    "assignedUser": "[email protected]"
    "labels": []
})

3. Check results

With this setup done, any user who visits the page will always receive a fresh Session ID and thus will see an empty chat.

A user who logs in will automatically have their chat history retrieved and visible in the chat window.

A user who logs out will not see their chat history anymore and instead be treated as a new customer.