Skip to main content

Snippets

Only load when previously used

Scenario: You have a /contact page and a /home and /news page. You always show the chat on the /contact page, but you only want to show the chat on the /home and /news page when the client had started a conversation on the /contact page.

For this to work you load in the script like normal on the /contact page, but for /home and /news we need to only load the script when a chat has been started before. You can check if the cookie “parleychat_sendedMessageTime” has been set and only load the script if that is the case.

Example explained
if(localStorage.getItem("parleychat_sendedMessageTime") !== null){
// Get a reference to the head tag
const head = document.getElementsByTagName('head')[0];
// Create an HTML script element
const js = document.createElement("script");
// Set the content type of that script element
js.type = "text/javascript";
// Set the src of that script element
js.src = "path/to/app.js";
// Add the element to the head tag
head.appendChild(js);
}
Example without comments
if(localStorage.getItem("parleychat_sendedMessageTime") !== null){
const head = document.getElementsByTagName('head')[0];
const js = document.createElement("script");
js.type = "text/javascript";
js.src = "path/to/app.js";
head.appendChild(js);
}

Open messenger on page load

It is possible to automatically open the chat window when providing the GET parameter openmessenger. This could be useful when you send people a link through a text message when they try to contact the support by phone.

This is how the url could look like:
http://parleycdn.com/demo?openmessenger

How to add userAdditionalInformation

Implemented in version 1.2.10+

You can add userAdditionalInformation to provide extra information, from the user, to the Service that is handling the messages.

The information should be set in a key-value pair and can contain anything you like (as long as the Service can interpret these key-value's)

Some examples are:

  • a custom name for the user
  • The name of a bot that should work on the messages send in this conversation
  • The name of the Inbox where you want the messages to be placed in

Example code:

window.parleySettings.userAdditionalInformation = {
name: "Test user",
inbox: "Parley messages",
bot: "Our Robot Coworker"
};

// This information is send upon each new "register" of a device (POST /devices)
// Which can be called manually like this:
window.parleySettings.registerNewDevice(
"someauthheader",
function() {
console.log("OK");
},
function(error) {
console.log("error:", error);
},
true
);

More info on this function