NAV Navbar
javascript

JavaScript Events

At some point, you may need to hook your custom code into JavaScript events, or you may want to instruct Google Analytics to track certain events. In this article you will find all of SnapEngage's JavaScript Events, and a few example uses.

Close Event

SnapEngage.setCallback('Close', function (type, status) {
    console.log('type: ' + type);
    console.log('status: ' + status);
});

The Close event is fired when a close button is clicked.

This callback can return two pieces of info: type of window that was closed, and widget status when the close event was fired.

Parameter values

Parameter Type Description
Anonymous Function Function This callback function accepts two optional parameters: type and status. Inside the anonymous function, you can check the returned value of the type and status parameters.
type String The type of window that was closed (see table of values below).
status String The widget status when the close event was fired (see table of values below).

Return values of type parameter

SnapEngage.setCallback('Close', function (type, status) {
    console.log('A visitor just closed the ' + type + ' window.');
});

// The callback above returns this
// when the visitor closes a chat window
'A visitor just closed the chat window.'
Type Value Description
String 'form' The visitor closed a prechat or offline form.
String 'chat' The visitor closed a chat window.
String 'proactive' The visitor closed a proactive chat window.

Return values of status parameter

SnapEngage.setCallback('Close', function (type, status) {
    console.log('A visitor closed a ' + type +
     ' and the widget is ' + status + '.');
})

// The callback above returns this
// when the visitor closes a form
'A visitor closed a form and the widget is offline.'
Type Value Description
String 'online' The visitor closed a window when the widget was online.
String 'offline' The visitor closed a window when the widget
was offline.

ChatMessageReceived Event

SnapEngage.setCallback('ChatMessageReceived', function (agent, msg) {
    console.log('agent: ' + agent);
    console.log('msg: ' + msg);
});

The ChatMessageReceived event is fired when a message from the agent is received by the visitor.

This callback can return two pieces of info: name of the agent that sent the message, and the msg that was sent.

Parameter values

Parameter Type Description
Anonymous Function Function This callback function accepts two optional parameters: agent and message. Inside the anonymous function, you can check the returned value of the agent and message parameters.
agent String The agent alias.
msg String The message that was received by the visitor.

Return values

SnapEngage.setCallback('ChatMessageReceived', function (agent, msg) {
    console.log('A visitor received a chat message from ' + agent);
    console.log('And the msg was: ' + msg);
});

// The callback above returns this
// when the visitor receives a chat message
'A visitor received a chat message from Samantha';
'And the msg was: hello ';

Example return values of agent and msg are inside the code example.

ChatMessageSent Event

SnapEngage.setCallback('ChatMessageSent', function (msg) {
    console.log('msg: ' + msg);
});

The ChatMessageSent event is fired when the visitor submits a chat message while in the chat session.

This callback can return one piece of info: the msg that was sent by the visitor.

Parameter values

Parameter Type Description
Anonymous Function Function This callback function accepts one optional parameter: msg. Inside the anonymous function, you can check the returned value of the msg parameter.
msg String The message that was sent by the visitor.

Return values

SnapEngage.setCallback('ChatMessageSent', function (msg) {
    console.log('A visitor sent this chat message: ' + msg);
});

// The callback above returns this
// when a visitor sends a chat message
'A visitor sent this chat message: What does SnapEngage integrate with?';

An example return value of msg is inside the code example.

MessageSubmit Event

SnapEngage.setCallback('MessageSubmit', function (email, msg) {
    console.log('email: ' + email);
    console.log('msg: ' + msg);
});

The MessageSubmit event is fired when the visitor submits an offline message (not a chat message).

This callback can return two pieces of info: the visitor's email, and the msg that was sent.

Parameter values

Parameter Type Description
Anonymous Function Function This callback function accepts two optional parameters: email and msg. Inside the anonymous function, you can check the returned value of the email and msg parameters.
email String The visitor's email address.
msg String The message.

Return values

SnapEngage.setCallback('MessageSubmit', function (email, msg) {
    console.log(email + ' just submitted an offline message.');
    console.log('The message was: ' + msg);
});

// The callback above returns this
// when an offline message is submitted
'sam@test.com just submitted an offline message.'
'The message was: hello!';

Example return values of email and msg are inside the code example.

Open Event

SnapEngage.setCallback('Open', function (status) {
    console.log('status: ' + status);
});

The Open event is fired when the chat form is opened. The form may be opened by the user directly (button click), or as the result of an API call.

This callback can return one piece of info: the widget status when the event was fired.

Parameter values

Parameter Type Description
Anonymous Function Function This callback function accepts an optional parameter: status. Inside the anonymous function, you can check the returned value of the status parameters.
status String The widget status at the time the event was fired.

Return values of status parameter

SnapEngage.setCallback('Open', function (status) {
    console.log('A form just opened and the widget status is: ' + status);
});

// The callback above returns this
// when a form is opened and the widget is offline
'A form just opened and the widget status is: offline'
Type Value Description
String 'online' The widget is online.
String 'offline' The widget is offline.

OpenProactive Event

SnapEngage.setCallback('OpenProactive', function (agent, msg) {
    console.log('agent: ' + agent);
    console.log('msg: ' + msg);
});

The OpenProactive event is fired when the proactive chat is displayed to a visitor. (Note, when the visitor replies, the StartChat event fires.)

This callback can return two pieces of info: the name of the agent used in the proactive message, and the proactive msg itself.

Parameter values

Parameter Type Description
Anonymous Function Function This callback function accepts two optional parameters: agent and msg. Inside the anonymous function, you can check the returned value of the agent and msg parameters.
agent String The agent alias.
msg String The proactive prompt message.

Return values

SnapEngage.setCallback('OpenProactive', function (agent, msg) {
    console.log('A proactive message just popped up. The agent shown is ' +
     agent + '.');
    console.log('The proactive message used is: ' + msg);
});

// The callback above returns this
// when a proactive prompt opens
'A proactive message just popped up. The agent shown is Samantha.';
'The proactive message used is: You know nothing, Jon Snow.';

Example return values of agent and msg are inside the code example.

StartChat Event

SnapEngage.setCallback('StartChat', function (email, msg, type) {
    console.log('email: ' + email);
    console.log('msg: ' + msg);
    console.log('type: ' + type);
});

The StartChat event is fired when the visitor starts a chat, or responds to a proactive invitation.

This callback can return three pieces of info: the visitor's email, the visitor's first msg, and the type of chat (manual or proactive).

Parameter values

Parameter Type Description
Anonymous Function Function This callback function accepts three optional parameters: email, msg, and type. Inside the anonymous function, you can check the returned value of the email, msg, and type parameters.
email String The visitor's email address.
msg String The visitor's first message.
type String The type of chat.

Return values

SnapEngage.setCallback('StartChat', function (email, msg, type) {
    console.log(email + ' just started a ' + type + ' chat.');
    console.log('The message was: ' + msg);

});

// The callback above returns this
// when the visitor starts a manual chat
'sam@test.com just started a manual chat.';
'The message was: The King in the North!';

Example return values of email, msg, and type are inside the code example.

Return values of type parameter

Type Value Description
String 'manual' The visitor started a manual chat.
String 'proactive' The visitor started a proactive chat.

StartCallme Event

SnapEngage.setCallback('StartCallme', function (phone) {
    console.log('phone: ' + phone);
});

The StartCallMe event is fired when the visitor starts a call-me.

This callback can return one piece of info: the visitor's phone number.

Parameter values

Parameter Type Description
Anonymous Function Function This callback function accepts one optional parameter: phone. Inside the anonymous function, you can check the returned value of the phone parameters.
phone String The visitor's phone number.

Return values

SnapEngage.setCallback('StartCallMe', function (phone) {
    console.log('A visitor just started a callme request.');
    console.log('Their phone number is ' + phone);
});

// The callback above returns this
// when the visitor starts a callme request
'A visitor just started a callme request.'
'Their phone number is 555-1234';

Example return values of phone are inside the code example.