Setting a visitor’s name and email in the Pre-Chat Form using JavaScript API

The instructions below are for desktops and laptops only.

You can use tawk.to’s JavaScript API to auto-populate your customer’s name and email address in the Details tab, Pre-Chat Form, and Offline Form.

All visitors are assigned a default ID starting with “V” followed by a 16-digit number, like this:

Using the API functions in this guide, you can replace the default ID with the visitor’s name and email address.


There are 2 primary ways to do this:

  • With hash (recommended for secure data transfers)

  • Without hash

Note:
Your visitor’s details must be available through your system for the following API functions to work. This means visitors should be logged into your store/website before starting a conversation in your tawk.to live chat widget.

With hash 

Using a hash can help secure your customers’ data. We employ the HMACSHA256 encryption algorithm to generate a hash.


For steps on generating the hash, read this article: How to generate a hash for JavaScript API


Important: Enable Secure Mode in your JavaScript API settings before generating the hash.

Here’s a PHP example you can use to generate the hash:

Using visitor{}

Enter the hash you generated into the hash field of the visitor{} function.

Here’s an example:

Single-page applications/AJAX logins

If you use a single-page app/website or AJAX logins, enter the hash you generated into the hash field of the setAttributes() function.


Here’s an example:

Without hash 

You can also set the visitor’s name and email address using the visitor{} function without a hash.


Here’s an example:

Additional information

tawk.to’s JavaScript API allows you to automate processes at the beginning or end of a chat. You can learn more about enabling JavaScript API here: JavaScript API


If duplicate contacts are created due to automation, you can merge them into one profile. To learn more about merging contacts, read this article: How to merge contacts


If you have feedback about this article, or if you need more help:


Was this article helpful?

113 out of 186 liked this article

Still need help? Message Us