Skip to content
FacebookYouTubeX (Twitter)

JavaScript API - Listeners/Events

These event will triggers only when Pingstreams Widget fired some event in a specific situation. Once it’s up, you’ll be able to do something you need in the function handler.

window.Pingstreams(event_name, handler)

Register an event handler to an event type.

EventDescription
onLoadParamsFired when the parameters are loaded*
onInitFired when the widget is initialized
onAuthStateChangedThe event is generated when the user logs in or logs out
onOpenFired when the widget is open
onCloseFired when the widget is closed
onBeforeMessageSendFired before the message sending
onAfterMessageSendThis event is generated after the message has been sent
onOpenEyeCatcherFired when the callout box is open
onClosedEyeCatcherFired when the callout box is closed
onCloseMessagePreviewFired when the user click on close button in message preview while new message is received and widget is closed
onNewConversationComponentInitFired just after a new conversation is initialized
onBeforeDepartmentsFormRenderFired just before rendering Departments in the Departments view
onMessageCreatedFired when the widget receive a message
onNewConversationFired when the widget start a new conversation
onConversationUpdatedFired when the widget receive a conversation update

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

onLoadParams → onInit → onAuthStateChanged

The handler will have the signature function(event_data).

event_data is a Javascript CustomEvent. More info about CustomEvent here

ParameterTypeRequiredDescription
event_nameStringYESEvent name to bind to
handlerFunctionYESFunction with the signature function(event_data)
PropertyTypeDescription
detail.default_settingsObjectthe constructor default settings
<script type="application/javascript">
window.Pingstreams('onBeforeMessageSend', function(event_data) {
var message = event_data.detail.message;
console.log("onBeforeMessageSend called ", message);
});

window.Pingstreams('onAfterMessageSend', function(event_data) {
var message = event_data.detail.message;
console.log("onAfterMessageSend called ", message);
});
</script>

Example: Widget with visitor fullname and email from localStorage

Section titled “Example: 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.Pingstreams('onLoadParams', function(event_data) {
window.pingstreamsSettings.userFullname = localStorage.getItem("user_fullname");
window.pingstreamsSettings.userEmail = localStorage.getItem("user_email");
});
</script>

Example: Widget with welcome message with current date

Section titled “Example: Widget with welcome message with current date”
<script type="application/javascript">
window.Pingstreams('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:

PropertyTypeDescription
detail.messageObjectthe message that is being sent

Example: Programmatic setting custom user metadata

Section titled “Example: Programmatic setting custom user metadata”
<script type="application/javascript">
window.Pingstreams('onBeforeMessageSend', function(event_data) {
var message = event_data.detail.message;
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.Pingstreams('onBeforeMessageSend', function(event_data) {
var message = event_data.detail.message;
message.attributes.pagetitle = document.title;
});
</script>

This event is generated after the message has been sent.

Important payload of event_data:

PropertyTypeDescription
detail.messageObjectthe message that was sent
<script type="application/javascript">
window.Pingstreams('onAfterMessageSend', function(event_data) {
var message = event_data.detail.message;
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:

PropertyTypeDescription
detailObjectthe auth event
PropertyTypeDescription
eventstringPossible values: ‘online’ when user is logged in, ‘offline’ when user is logged out
isLoggedbooleanPossible values: true if the user is logged, false if not logged
user_idstringThe current user identifier
globalobjectAn object with all the widget global parameters
default_settingsobjectThe initial widget config parameters (window.pingstreamsSettings)
appConfigsobjectThe remote widget config parameters obtained from the remote Pingstreams server
<script type="application/javascript">
window.Pingstreams('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:

PropertyTypeDescription
detail.departmentsObjectthe 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. Attention you can modify the departments array only by reference.

<script type="application/javascript">
window.Pingstreams('onBeforeDepartmentsFormRender', function(event_data) {
var departments = event_data.detail.departments;
var lang = window.pingstreams.angularcomponent.component.g.lang;
if (lang && lang === 'en') {
var new_deps = departments.filter(function(dep) {
if (dep.description && dep.description.includes('English')) {
return dep;
}
});
} else {
var new_deps = departments.filter(function(dep) {
if (dep.description && dep.description.includes('French')){
return dep;
}
});
}
//modify the department array by reference
departments.length=0; //empty the array
console.log("new_deps",new_deps);
new_deps.forEach(function(d) { //populate the department array
departments.push(d);
});
});
</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:

PropertyTypeDescription
detail.newConvIdObjectthe 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.Pingstreams('onNewConversationComponentInit', function(event_data) {
const message = 'hello';
const recipientId = event_data.detail.newConvId;
const recipientFullname = 'Owner';
const type = 'text';
const metadata = {};
const attributes = {test:'test attributes', subtype: 'info'};
window.pingstreams.sendSupportMessage(
message,
recipientId,
recipientFullname,
type,
metadata,
attributes
)
});
</script>

This event is generated when the widget receive a message.

Important payload of event_data:

PropertyTypeDescription
detailObjectthe message that was received
<script type="application/javascript">
window.Pingstreams('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:

PropertyTypeDescription
detailObjectthe conversation that was received
<script type="application/javascript">
var now = Date.now();
window.Pingstreams('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>