This code sample shows how to perform an action when a specific event occurs.
There are a number of points to note when looking at this sample:
Events on which you wish to act should be defined within the eeHandlers variable. This sample includes the events eeRuleRan, eeOffered, eeAccepted, and eeDeclined. The code included for each of these events displays a console message. You should replace the relevant
console.log
line with whatever code you wish to run when these events occur.Once this script has loaded, any events named in the eeHandlers variable will automatically trigger that handler.
If you wish to check for RuleRan events that occurred before this code was loaded, set the variable trackHistoricalRuleRan to true. This enables you to merge the RuleRan events with the historical log and then search the combined log for a specific event. The event log and the RuleRan log are refreshed each time a page loads, so using this variable can only check back to page load time. The sample code provided here copies the event log, adds the rule log, and then sorts them into the order they occurred. It then goes through the historic events and rules and acts on any events or rules as specified by the relevant handler.
(function() { var trackHistoricalRuleRan = true; var ruleIdToObj = function(id) { return id && ATGSvcs.rules.rulehash[id]; } var eeHandlers = { eeRuleRan: function(data) { console.log('Analytics Tracking - Rule Run: ' + data.name); }, eeOffered: function(data) { if (data.rule) { console.log('Analytics Tracking - Invite Offered: ' + (data.rule.name || 'unknown')); } }, eeAccepted: function(data) { if (data.rule) { console.log('Analytics Tracking - Invite Clicked: ' + (data.rule.name || 'unknown')); } }, eeDeclined: function(data) { if (data.rule) { console.log('Analytics Tracking - Invite Declined: ' + (data.rule.name || 'unknown')); } }, var historicalLog = ATGSvcs.rules.eventLog.slice(); if (trackHistoricalRuleRan) { historicalLog = historicalLog.concat(ATGSvcs.rules.ruleLog); historicalLog.sort(function(a, b) { return (a['ts'] - b['ts']); }); } for (var i=0; i < historicalLog.length; i++) { var logEntry = historicalLog[i]; if (logEntry.event) { var handler = eeHandlers[logEntry.event.replace('-', '')]; if (handler) { handler(logEntry.data); } } else if (trackHistoricalRuleRan && logEntry.id) { var rule = ruleIdToObj(logEntry.id); if (rule) { var handler = eeHandlers['eeRuleRan']; if (handler) { handler(rule); } } } } ATGSvcs.eventSubscribe ({ 'eeRuleRan': function(e, data) {eeHandlers.eeRuleRan(data); }, 'eeOffered': function(e, data) { eeHandlers.eeOffered(data); }, 'eeAccepted': function(e, data) { eeHandlers.eeAccepted(data); }, 'eeDeclined': function(e, data) { eeHandlers.eeDeclined(data); }, }); console.log('End: Rules / Events from Logs'); })();