Improve API to explain how to change visitor name
Although there are some explanations in the API doc I think they could be more extensive. It took me some time to figure out how to do it and from the forum threads it seems I'm not the only one to have struggled.
What about something like:
To set the visitor name you have to options.
1) Before embedding the app code, define the TawkAPI object with the visitor info. On load, the Tawk.to script will pick up the parameters and set the user name and email
TawkAPI = {
visitor: {
name: 'The visitor name',
email: 'visitor@domain.com'
}
}
// Paste the widget code found in the Admin area
// <script src="https://embed.tawk.to/YOUR_SITE_ID/default"> etc.
Both name and email must be strings.
2) You can make a dynamic API call once the chat script has started. However, you need to pass a hash to authenticate the API call (see https://www.tawk.to/javascript-api/#securemode).
The hash must be generated on the server then sent to the client. After that, you can define the visitor javascript object and send it to the .setAttributes() method.
(javascript on client)
const visitor = {
name: '<?php echo $userName; ?>',
email: '<?php echo $userEmail; ?>',
hash: '<?php echo hashhmac("sha256", $userEmail, "<APIKEY>"); ?>'
}
Tawk_API.setAttributes(visitor);

-
Aghogho Bernard commented
How about setting phone numbers. Why does it accommodate name and email only ?
-
Craig Rairdin commented
Documentation for setAttributes() should explain that if you send name, email, and hash, any other values passed in are ignored. They have to be sent in a separate setAttributes() call.
-
Peter Buckner commented
If only that simple. Doesn't appear that #1 (define Tawk_API object with visitor) works anymore.
-
Anonymous commented
Finally this works, thanks man!