Javascript Events
(Last Updated On: November 2, 2015)
Overview

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.


Event List


Close

Usage
Called when the form close button is clicked.
Parameters
type
What was closed: the form, a chat session in progress, or a proactive prompt not answered by the visitor with the respective string.
Values (string): ‘form’ / ‘chat’ / ‘proactive’
status
The agent status.
Values (string): ‘online’ / ‘offline’

ChatMessageReceived

Usage
Called when the agent submits a text message for the visitor.
Parameters
agent
(string) The Agent alias.
msg
(string) The message.

ChatMessageSent

Usage
Called when the visitor submits a chat message while in the chat session.
Parameters
msg
(string) The message.

MessageSubmit

Usage
Called when the visitor posts the form for an offline message (not a chat).
Parameters
email
(string) Visitor’s email address.
msg
(string) Visitors message/question.

Open

Usage
Called when the user opens the form on a button click, through an API call to startLink(), startChat(), etc.
Parameters
status
The agent status.
Values (string): ‘online’ / ‘offline’

OpenProactive

Usage
Called when the Proactive chat is displayed to a visitor. (Note, when the visitor replies, the 'StartChat' event fires. You can use these two events in conjunction to analyze your engagement score by comparing the number of 'OpenProactive' events to the number of 'StartChat' events.)
Parameters
agent
(string) Agent’s alias.
msg
(string) Proactive prompt message.

StartChat

Usage
Called when the visitor starts a chat, or responds to a Proactive invitation (see: ‘OpenProactive’).
Parameters
email
(string) Visitor’s email.
msg
(string) Visitor’s first message.
type
Type of chat.
Values (string): ‘manual’ / ‘proactive’

StartCallme

Usage
Called when the visitor starts a call-me.
Parameters
phone
(string) Visitor’s phone number.

Example Usage


All of the following examples should be used inside the “Advanced” Code Snippet.


Cookie-based conversion tracking
SnapEngage.setCallback('ChatMessageReceived', function (agent, msg) {
    var date = new Date();
    date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000)); // 30 Days in the future.
    document.cookie = 'chatWith=' + encodeURIComponent(agent) + '; expires=' + date.toGMTString() + '; path=/;'; //Create cookie.
});

Google Analytics Integration

For more information on Event Tracking in Google Analytics please see their Event Tracking Basics article and more advanced Event Tracking article.

Log an event every time a chat starts, along with type: ‘manual’ or ‘proactive’.

SnapEngage.setCallback('StartChat', function (email, msg, type) {
    _gaq.push(['_trackEvent', 'SnapEngage', 'chatStarted', type]);
});

-and/or-

Track Proactive Chat engagement rate along with Agent Alias tracking.
**Note: This code snippet ONLY tracks Proactive Chat Invitations, and Proactive Chat Responses.

var seAgent;
SnapEngage.setCallback('OpenProactive', function (agent, msg) {
    seAgent = agent;
    _gaq.push(['_trackEvent', 'SnapEngage', 'proactivePrompt', agent]);
});

SnapEngage.setCallback('StartChat', function (email, msg, type) {
    if (type == 'proactive') {
        _gaq.push(['_trackEvent', 'SnapEngage', 'proactiveEngaged', seAgent]);
    }
});

-and/or-

Set a “hasChatted” flag for Google Analytics goal tracking

SnapEngage.setCallback('StartChat', function (email, msg, type) {
    _gaq.push(['_trackEvent', 'SnapEngage', 'hasChatted', email]);
});
If you use the newer ‘Universal Analytics’ your JavaScript would have to be adjusted accordingly:

var seAgent;
SnapABug.setCallback('OpenProactive', function(agent, msg) {
    seAgent = agent;
    ga('send','event','SnapEngage', 'proactivePrompt', agent);
});

SnapABug.setCallback('StartChat', function(email, msg, type) {
    if (type == 'proactive') {
        ga('send','event','SnapEngage', 'proactiveEngaged', seAgent);
    }
});

Redirect on chat close
SnapEngage.setCallback('Close', function (type, status) {
    document.location = 'http://www.yoursite.com/support';
});

Clear a callback

You can clear out a callback by setting it to an empty function. For example, if you only want a behavior to happen once, but you’re hooking into an event which could fire multiple times.

SnapEngage.setCallback('ChatMessageSent', function(){});