Skip to content
FacebookYouTubeX (Twitter)

Web SDK v4

This guide will show you how to get started as quickly as possible with the Web SDK from Pingstreams. The Web SDK will give businesses and developers the flexibility to build and customize a chat experience that meet their specific design/brand requirements.

To chat with your visitors embed the widget on your site. Copy the following script and insert it in the HTML source between the HEAD tags:

<script type="application/javascript">
window.pingstreamsSettings =
{
  projectid: "YOUR_PINGSTREAMS_PROJECT_ID"
};
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "https://widget.pingstreams.com/v4/launch.js";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'pingstreams-jssdk'));
</script>

To get your PINGSTREAMS_PROJECT_ID go to the Pingstreams Dashboard and click on the Widget item of the menu:

Widget version 4.0 supports remote configuration of most parameters directly from the Widget menu of the Dashboard.

You can customize the widget passing the following parameters to window.pingstreamsSettings object.

  • projectid: The Pingstreams project id. Find your Pingstreams ProjectID in the Pingstreams Dashboard under the Widget menu.
  • preChatForm: You can require customers to enter information like name and email before sending a chat message by enabling the Pre-Chat form. Permitted values: true, false. The default value is false.
  • align: Make the chat available on the Right or on the Left of the screen. Permitted values: ‘right’, ‘left’. Default value is right.
  • calloutTimer: Proactively open the chat windows to increase the customer engagement. Permitted values: -1 (Disabled), 0 (Immediately) or a positive integer value. For example: 5 (After 5 seconds), 10 (After 10 seconds).
  • calloutTitle: The title of the callout window.
  • calloutMsg: The message of the callout window.
  • userFullname: Current user fullname. Set this parameter to specify the visitor fullname.
  • userEmail: Current user email address. Set this parameter to specify the visitor email address.
  • welcomeTitle: The welcome title to show on the widget home page.
  • welcomeMsg: Set the widget welcome message. Value type : string
  • widgetTitle: Set the widget title label shown in the widget header. Value type : string. The default value is Pingstreams.
  • startFromHome: If false when loaded the widget starts directly with a new conversation. If true the widget shows the home component. The default value is true.
  • logoChat: The url of the logo to show on the widget home page.
  • lang: With this configuration it is possible to force the widget lang. The widget will try to get the browser lang, if it is not possible it will use the default “en” lang
  • hideHeaderCloseButton: Hide the close button in the widget header. Permitted values: true, false. The default value is false.
  • isOpen: Read-only property. Set this property true in the script to automatically open the widget as soon as it is loaded. Permitted values: true, false. Default value : false.
  • fullscreenMode: if it is true, the chat window is open in fullscreen mode. Permitted values: true, false. Default value : false
  • themeColor: allows you to change the main widget’s color (color of the header, color of the launcher button, other minor elements). Permitted values: Hex color codes, e.g. #87BC65 and RGB color codes, e.g. rgb(135,188,101)
  • themeForegroundColor: allows you to change text and icons’ color. Permitted values: Hex color codes, e.g. #425635 and RGB color codes, e.g. rgb(66,86,53)
  • departmentID: to skip departments selection, you can set the department ID upon which the widget must start the new conversation. See the tutorial here.
  • isShown: Read only property. This property returns the visibility of the whole widget including the widget balloon. If true the widget is visible otherwise (false) the widget is hidden. Use window.pingstreams.show() and window.pingstreams.hide() methods to change the widget visibility.
  • allowTranscriptDownload: allows the user to download the chat transcript. The download button appears when the chat is closed by the operator. Permitted values: true, false. Default value: false
  • marginX: Set the side margin, left or right depending on the align property. Value type: string. Default value : “20px”
  • marginY: Set the distance from the page bottom margin. Value type: string. Default value : “20px”
  • autoStart: Set if the widget performs an automatic anonymous authentication at the startup. Default value : true
  • startHidden: Set if the widget starts in hidden mode. Default value : false
  • persistence: You can specify how the Authentication state persists when using the Pingstreams JS SDK. This includes the ability to specify whether a signed in user should be indefinitely persisted until explicit sign out or cleared when the window is closed. Permitted values: local, session. Default value : local. Local value indicates that the state will be persisted even when the browser window is closed. An explicit sign out is needed to clear that state. Session value indicates that the state will only persist in the current session or tab, and will be cleared when the tab or window in which the user authenticated is closed.
  • showWaitTime: Show the expected response time from your agents in the home widget window. Value type : boolean. The default value is true.
  • showAvailableAgents: Show the available agents with avatar in the home widget window. Value type : boolean. The default value is true.
  • showLogoutOption: Show the logout options in the home widget window. Value type : boolean. The default value is true.
  • isLogEnabled: Enable the widget log. Value type: boolean. The default value is false.

Example 1. Widget with user fullname and email

Section titled “Example 1. Widget with user fullname and email”
<script type="application/javascript">
window.pingstreamsSettings =
{
  projectid: "5b55e806c93dde00143163dd",
  userFullname: "Andrea Leo",
  userEmail: "andrea@example.com"
};
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id; //js.async=!0;
  js.src = "https://widget.pingstreams.com/v4/launch.js";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'pingstreams-jssdk'));
</script>

Example 2. Widget with preChatForm and left alignment:

Section titled “Example 2. Widget with preChatForm and left alignment:”
<script type="application/javascript">
window.pingstreamsSettings =
{
  projectid: "5b55e806c93dde00143163dd",
  preChatForm: true,
  align: 'left'
};
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "https://widget.pingstreams.com/v4/launch.js";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'pingstreams-jssdk'));
</script>

You can also pass the above configurations as a Url parameter with the pingstreams_ prefix. For example:

https://widget.pingstreams.com/v4/index.html?pingstreams_isOpen=true&pingstreams_align=right

This will open the widget:

window.pingstreams.open();

This will minimize the widget:

window.pingstreams.close();

This will hide the widget:

window.pingstreams.hide();

This will show the widget:

window.pingstreams.show();

If your app is characterized by very few page refreshes (ie., content is swapped out on the client side but no page refresh happens, Angular, React, jQuery, etc..) and lots of asynchronous JS, you’ll need to update Pingstreams when your user’s data changes. A reInit call simulates a page refresh, causing Pingstreams to reload the widget and all the configurations.

window.pingstreams.reInit();

This method make a signin using a JWT Custom Token as described here.

window.pingstreams.signInWithCustomToken(customJwt);

This will logout the widget:

window.pingstreams.logout();

This parameter configures the PreChatForm visibility:

window.pingstreams.setPreChatForm(true|false);

This method sends a message to the current support conversation:

const recipientId = window.pingstreams.angularcomponent.component.g.activeConversation
const message = 'hello';
const type = 'text';
const metadata = {};
const attributes = {};
window.pingstreams.sendSupportMessage(
  message,
  recipientId,
  type,
  metadata,
  attributes
)

The function pingstreamsAsyncInit is called when the basic apis of the widget are loaded. Inside the pingstreamsAsyncInit function the object window.pingstreams is defined and can be used.

window.pingstreams.on(event_name, handler)

Register an event handler to an event type.

Available events:

  • onLoadParams - Fired when the parameters are loaded.
  • onInit - Fired when the widget is initialized
  • onAuthStateChanged - The event is generated when the user logs in or logs out
  • onOpen - Fired when the widget is open
  • onClose - Fired when the widget is closed
  • onBeforeMessageSend - Fired before the message sending.
  • onAfterMessageSend - This event is generated after the message has been sent.
  • onOpenEyeCatcher - Fired when the callout box is open
  • onClosedEyeCatcher - Fired when the callout box is closed
  • onNewConversationComponentInit - Fired just after a new conversation is initialized
  • onBeforeDepartmentsFormRender - Fired just before rendering Departments in the Departments view
  • onMessageCreated - Fired when the widget receive a message
  • onConversationUpdated - Fired when the widget receive a conversation update

Initial events lifecycle: onLoadParams -> onInit -> onAuthStateChanged

The handler will have the signature function(event_data).

event_data is a Javascript CustomEvent. More info about CustomEvent here

Arguments:

  • event_name (String, YES) - Event name to bind to
  • handler (Function, YES) - Function with the signature function(event_data)
<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onBeforeMessageSend', function(event_data) {
    var message = event_data.detail;
    console.log("onBeforeMessageSend called ", message);
  });
  window.pingstreams.on('onAfterMessageSend', function(event_data) {
    var message = event_data.detail;
    console.log("onAfterMessageSend called ", message);
  });
}
</script>

This event will be fired before the Pingstreams parameters is loaded. Use this event to change at runtime your Pingstreams settings.

Important payload of event_data:

  • detail.default_settings (Object) - the constructor default settings

Example 4. Widget with visitor fullname and email from localStorage

Section titled “Example 4. Widget with visitor fullname and email from localStorage”
<script type="application/javascript">
//set fullname to localstorage
localStorage.setItem("user_fullname", "Andrea from localStorage");
localStorage.setItem("user_email", "andrea@example.com");

window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onLoadParams', function(event_data) {
    window.pingstreamsSettings.userFullname = localStorage.getItem("user_fullname");
    window.pingstreamsSettings.userEmail = localStorage.getItem("user_email");
  });
}
</script>

Example 5. Widget with welcome message with current date

Section titled “Example 5. Widget with welcome message with current date”
<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onLoadParams', function(event_data) {
    window.pingstreamsSettings.welcomeMsg = " Hello at: " + new Date().toLocaleString();
  });
}
</script>

This event will be fired before the message sending. Use this event to add user information or custom attributes to your chat message.

Important payload of event_data:

  • detail (Object) - the message that is being sent

Example. Programmatic setting custom user metadata

Section titled “Example. Programmatic setting custom user metadata”
<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onBeforeMessageSend', function(event_data) {
    var message = event_data.detail;
    message.attributes.userCompany = "Frontiere21";
  });
}
</script>

Example. Add a custom attribute (page title) to the message.

Section titled “Example. Add a custom attribute (page title) to the message.”
<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onBeforeMessageSend', function(event_data) {
    var message = event_data.detail;
    message.attributes.pagetitle = document.title;
  });
}
</script>

This event is generated after the message has been sent.

Important payload of event_data:

  • detail (Object) - the message that was sent
<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onAfterMessageSend', function(event_data) {
    var message = event_data.detail;
    console.log("onAfterMessageSend called ", message);
  });
}
</script>

This event is generated when the authentication state changed (Ex: user sign-in, user logout, etc.)

Important payload of event_data:

  • detail (Object) - the auth event

Auth Event description:

  • event (number) - Possible values: 0 if wasn’t logged (with autoStart false), 200 already logged, 201 new login, 400 error, -2 from reinit method, -1 from logout
  • isLogged (boolean) - Possible values: true if the user is logged, false if not logged
  • user_id (string) - The current user identifier
  • global (object) - An object with all the widget global parameters
  • default_settings (object) - The initial widget config parameters (window.pingstreamsSettings)
  • appConfigs (object) - The remote widget config parameters obtained from the remote Pingstreams server
<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onAuthStateChanged', function (event_data) {
    console.log("onAuthStateChanged ----> ", event_data.detail.event);
    if (!event_data.detail.isLogged) {
      console.log("NOT logged");
      window.pingstreams.signInWithCustomToken("JWT CHANGE IT");
    } else {
      console.log("logged in");
    }
  });
}
</script>

This event is generated before rendering the Departments selection View. Use this event if you want to filter the default Departments list based on some conditions.

Important payload of event_data:

  • detail.departments (Object) - the array of the default Departments

In the following example Departments are filtered based on the current widget language. Actually a Department doesn’t provide a specific “language” field. In this example Department language is put in the Department description field. A next update will provide specific Department “tags” (or “labels”) that will be used to save specific information into the Department resources.

<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onBeforeDepartmentsFormRender', function(event_data) {
    var departments = event_data.detail.departments;
    var lang = window.pingstreams.angularcomponent.component.g.lang;
    if (lang && lang === 'en') {
      return departments.filter(function(dep) {
        if (dep.description.includes('English')) {
          return dep;
        }
      });
    } else {
      return departments.filter(function(dep) {
        if (dep.description.includes('French')){
          return dep;
        }
      });
    }
  });
}
</script>

This event is generated as soon as a new conversation view is rendered. Use this event if you want to execute some actions on a Conversation start.

Important payload of event_data:

  • detail.newConvId (Object) - the id of the conversation that fired the event

In the following example a hidden message is sent as soon as a conversation starts. Sending a hidden message is useful to fire a bot welcome message, if one is invited in the conversation.

<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onNewConversationComponentInit', function(event_data) {
    const message = 'hello';
    const recipientId = event_data.detail.newConvId;
    const type = 'text';
    const metadata = {};
    const attributes = {test:'test attributes', subtype: 'info'};
    window.pingstreams.sendSupportMessage(
      message,
      recipientId,
      type,
      metadata,
      attributes
    )
  });
}
</script>

This event is generated when the widget receive a message.

Important payload of event_data:

  • detail (Object) - the message that was received
<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  window.pingstreams.on('onMessageCreated', function(event_data) {
    var message = event_data.detail;
    console.log(" TRIGGER onMessageCreated -> ", message);
  });
}
</script>

This event is generated when the widget receive a conversation update.

Important payload of event_data:

  • detail (Object) - the conversation that was received
<script type="application/javascript">
window.pingstreamsAsyncInit = function() {
  var now = Date.now();
  window.pingstreams.on('onConversationUpdated', function(event_data) {
    var dateConvUpdate = event_data.detail.conversation.timestamp
    console.log(" TRIGGER onConversationUpdated -> ", event_data.detail.conversation);
    console.log("now-> ", now);
    console.log("dateConvUpdate-> ", dateConvUpdate);
    if(now < dateConvUpdate){
      console.log(" New conversation!!!");
    }
  });
}
</script>

Enabling authenticated visitors in the Chat widget

Section titled “Enabling authenticated visitors in the Chat widget”

You can configure your widget to authenticate visitors using the Javascript API and JWT token. More info Widget Authentication