This code shows an alternative method to act on a specific event. This method should be used if you wish to act on the call-started, call-ended, chat-started, or chat-ended events, as these events require Engagement Engine to poll the server to check if the events have occurred, and this method shows how to do that.
There are a number of points to note when looking at this sample:
This code converts the server string to an object to make it easier to work with.
Events on which you wish to act should be defined within the eeHandlers variable. This sample includes the events eeRuleRan, call-started, call-ended, and eeCustomEvent. The code included for each of these events displays a console message. You should replace the line
console.log('Analytics Tracking - Rule Run: ' + data.name);
with whatever code you wish to run when these events occur.When handling the eeCustomEvent event, the string data.data contains the value that is specified in the Other Event Name field on the Log Event Action which logs the event.
This sample includes a setting for poll-delay. This is used to specify the intervals at which the server is polled to check for call-started, call-ended, chat-started, and chat ended events. The value is the number of milliseconds between server polls. The minimum value for this is 10,000ms (10 seconds). The higher the frequency of server polls, the greater the impact on performance.
As this sample requires server polling, it uses the
ATGSvcs.rules.analytics.launch
method to handle the events as this method includes server polling capability, whereas theATGSvcs.eventSubscribe
used in the Act On Event Without Polling Server sample does not include server polling capability.Once this script has loaded, any events named in the eeHandlers variable will automatically trigger that handler.
(function() {
var ruleIdToObj = function(id) {
return id && ATGSvcs.rules.rulehash[id];
}
var serverDataToObj = function(data) {
var serverData = data.split(','),
dataArray,
dataObj = {};
for (var i=0; i < serverData.length; i++) {
dataArray = serverData[i].split("=");
dataObj[dataArray[0]] = dataArray[1];
}
return dataObj;
}
var eeHandlers = {
callstarted: function(data) {
var dataObj = serverDataToObj(data.data),
rule = ruleIdToObj(dataObj.ruleId);
if (rule) {
console.log('Analytics Tracking - Call Started: ' + rule.name);
}
},
callended: function(data) {
var dataObj = serverDataToObj(data.data),
rule = ruleIdToObj(dataObj.ruleId);
if (rule) {
console.log('Analytics Tracking - Call Ended: ' + rule.name);
}
},
eeRuleRan: function(data) {
console.log('Analytics Tracking - Rule Run: ' + data.name);
},
eeCustomEvent: function(data) {
console.log('Analytics Tracking - Custom Event - ' + data.data);
}
}
ATGSvcs.rules.analytics.launch ({
'poll-delay': 15000,
'eeRuleRan': function(e, data) { eeHandlers.eeRuleRan(data.source); },
'call-started': function(e, data) { eeHandlers.callstarted(data); },
'call-ended': function(e, data) { eeHandlers.callended(data); },
'eeCustomEvent': function(e, data) { eeHandlers.eeCustomEvent(data); },
},
});
console.log('End: Rules / Events from Logs');
})();