You can restore user conversations for logged in users, every time they initiate a conversation from a different browser or device. To do so, for every user, an unique external ID and restore ID has to be defined. 


Two attributes needed to restore a user conversation,


  • External ID - The external ID has to be unique to each logged in user. This information should be passed to Freshchat from your side. For example, user email ID can be an external ID.


  • Restore ID - This will be created by Freshchat when a logged in user (from the customer’s website) initiates a conversation on Freshchat messenger for the first time. Restore ID is also unique to each created user. This ID will be passed to your website on the user creation callback function. You have to store this ID in your database, once it is passed by Freshchat. The next time the website user pings using the Freshchat messenger from a different browser or mobile app, this restore ID, along with the external ID must be passed from your side to Freshchat for the user conversation to be restored. Otherwise every conversation initiated by the user on a new / fresh browser session will create a new user and the conversation history will be lost.


Sample Script:


<script>
  var restoreId = RESTOREID //Which need to be fetched from your DB
  window.fcWidget.init({
    token: "8d3a4a04-5562-4f59-8f66-f84a269897a1",
    host: "https://wchat.freshchat.com",
    externalId: '1234567',
    restoreId: restoreId ? restoreId : null
  });
  window.fcWidget.user.get(function(resp) {
    var status = resp && resp.status,
        data = resp && resp.data;
    if (status !== 200) {
      window.fcWidget.user.setProperties({
        firstName: "John",              // user's first name
        lastName: "Doe",                // user's last name
        email: "john.doe@gmail.com",    // user's email address
        phone: "8668323090",            // phone number without country code
        phoneCountryCode: "+1",         // phone's country code
        plan: "Estate",                 // user's meta property 1
        status: "Active",               // user's meta property 2
        "Last Payment": "12th August"   // user's meta property 3
      });
      window.fcWidget.on('user:created', function(resp) {
        var status = resp && resp.status,
            data = resp && resp.data;
        if (status === 200) {
          if (data.restoreId) {
            // Update restoreId in your database
          }
        }
      });
    }
  });
</script>


Steps:

  1. Paste the entire script from above on your website. 


Note:

Restore ID won’t be available when a website user logs in for the first time. It gets created only after a user initiates a conversation on the Freshchat messenger for the first time.

 

  1. Define external ID. This step is mandatory.


Note:

External ID should be unique for each user. Example: Email ID of each user can be defined as their external ID.


  1. init() and fcWidget.user.get() start running when a user logs in for the first time on your website.

  2. When a logged in user sends a message on Freshchat messenger for the first time, the user gets created in Freshchat, with information from fcWidget.user.setProperties()and call back function for user creation runs.

  3. Parallely, restore ID is created in Freshchat for that user and will be available on    fcWidget.on(‘user created’) event.

  4. In  if (data.restoreId) condition, add your code to save the restore ID from Freshchat to your database.

  5. When a user starts a conversation from a different browser or device next time, restore ID should be fetched from your database and sent to Freshchat, to restore user conversation. To do this, you have to add code which fetches the restore ID from your database before init().

  6. Finally, pass this restore ID to Freshchat in init().


Note:

Restore ID is like a password for that instance of the user created. If you do create a new user again for the same external ID without restoring the old user, you will get a new restore ID. This is for security reasons so that an impersonator cannot restore your conversations.