1/** 2 * Events 3 */ 4var events = new function() { 5 6 /** 7 * Sets up save page views 8 */ 9 this.setupSavePageViews = function() { 10 this.saveEvent(config_data.user_id, config_data.user_environment_id, 'page_view', 'Page viewed at URL ' + window.location.href + '.', null, '', '', ''); 11 }; 12 13 /** 14 * Sets up save ajax actions 15 */ 16 this.setupSaveAjaxActions = function() { 17 // Intercept all global AJAX responses and send a ping with ajax action to the server 18 // except for url_ping and ajax_ping 19 jQuery(document).ajaxSuccess(function(event, xhr, settings) { 20 var ajaxAction = ''; 21 if (settings.data) { 22 var dataParts = settings.data.split("&"); 23 ajaxAction = dataParts[0].split("=")[1]; 24 } else if (settings.url) { 25 ajaxAction = utils.getUrlParamByName(settings.url, "action"); 26 } else { 27 // cannot get ajax action 28 return; 29 } 30 var temp = config_data.ignore_ajax_actions[ajaxAction]; 31 if (jQuery.inArray(ajaxAction, config_data.ignore_ajax_actions) != -1) 32 return; // ignore this ajax call 33 34 var statusText = xhr.statusText; 35 var description = 'Ajax action ' + ajaxAction + ' was made.'; 36 37 events.saveEvent(config_data.user_id, config_data.user_environment_id, 'ajax_action', description , null, null, '', statusText); 38 }); 39 }; 40 41 /** 42 * Sets up saving custom events 43 */ 44 this.setupSaveCustomEvents = function() { 45 for (var index in config_data.custom_events) { 46 var customEvent = config_data.custom_events[index]['custom_event']; 47 var description = config_data.custom_events[index]['description']; 48 var eventType = config_data.custom_events[index]['event_type']; 49 var isFormSubmit = config_data.custom_events[index]['is_form_submit'] == "1" ? true : false; 50 var isMouseClick = config_data.custom_events[index]['is_mouse_click'] == "1" ? true : false; 51 var isTouchscreenTap = config_data.custom_events[index]['is_touchscreen_tap'] == "1" ? true : false; 52 this.bindCustomEvent(customEvent, description, eventType, isFormSubmit, isMouseClick, isTouchscreenTap); 53 } 54 }; 55 56 /** 57 * Binds custom events 58 */ 59 this.bindCustomEvent = function(selector, description, eventType, isFormSubmit, isMouseClick, isTouchscreenTap) { 60 if (isTouchscreenTap) { 61 this.setupSaveTouchscreenTapEvent(selector, eventType, description); 62 } 63 if (isMouseClick) { 64 this.setupSaveMouseClickEvent(selector, eventType, description); 65 } 66 if (isFormSubmit) { 67 jQuery(selector).submit(function() { 68 events.saveEvent(config_data.user_id, config_data.user_environment_id, eventType, description , null, null, false, selector); 69 }); 70 } 71 }; 72 73 /** 74 * Sets up saving mouse clicks and touchscreen tap events 75 */ 76 this.setupSaveMouseClickAndTouchScreenTapEvents = function() { 77 this.setupSaveTouchscreenTapEvent(); 78 this.setupSaveMouseClickEvent(document); 79 }; 80 81 /** 82 * Trigger setup for saving touchscreen tap events 83 */ 84 this.setupSaveTouchscreenTapEvent = function(selector, eventType, description, data) { 85 86 var touchData = { 87 started : null, // detect if a touch event is sarted 88 currrentX : 0, 89 yCoord : 0, 90 previousXCoord : 0, 91 previousYCoord : 0, 92 touch : null 93 }; 94 95 jQuery(selector).on("touchstart", function(e) { 96 touchData.started = new Date().getTime(); 97 var touch = e.originalEvent.touches[0]; 98 touchData.previousXCoord = touch.pageX; 99 touchData.previousYCoord = touch.pageY; 100 touchData.touch = touch; 101 }); 102 103 jQuery(selector).on( 104 "touchend touchcancel", 105 function(e) { 106 var now = new Date().getTime(); 107 // Detecting if after 200ms if in the same position. 108 // FIXME taps are not always recorded if the browser 109 // takes over before the AJAX call is made. So this 110 // means we will get some, and lose some. 111 if ((touchData.started !== null) 112 && ((now - touchData.started) < 200) 113 && (touchData.touch !== null)) { 114 var touch = touchData.touch; 115 var xCoord = touch.pageX; 116 var yCoord = touch.pageY; 117 if ((touchData.previousXCoord === xCoord) 118 && (touchData.previousYCoord === yCoord)) { 119 120 if (jQuery('#wpadminbar').length > 0) { 121 yCoord -= jQuery('#wpadminbar').height(); 122 } 123 124 if (description == null || description == '') { 125 description = 'A touchscreen tap was made at x = ' + xCoord + ' and y = ' + yCoord + '.'; 126 } 127 if (eventType == null || eventType == '') { 128 eventType = 'touchscreen_tap'; 129 } 130 if (data == null) { 131 if (selector !== document) { 132 data = selector; 133 } else { 134 data = ''; 135 } 136 } 137 events.saveEvent(config_data.user_id, config_data.user_environment_id, eventType, description, xCoord, yCoord, true, data); 138 } 139 } 140 touchData.started = null; 141 touchData.touch = null; 142 }); 143 }; 144 145 /** 146 * Trigger and setup for savig mouse click events 147 */ 148 this.setupSaveMouseClickEvent = function(selector, eventType, description, data) {
149 // support mouse clicks always 150 jQuery(document).on('click', selector, function(e) { 151 var event = e ? e : window.event; 152 var coords = utils.getEventXYCoords(event); 153 var xCoord = coords.xCoord; 154 var yCoord = coords.yCoord; 155 156 if (description == null || description == '') { 157 description = 'A mouse click was made at x = ' + xCoord + ' and y = ' + yCoord + '.'; 158 } 159 if (eventType == null || eventType == '') { 160 eventType = 'mouse_click'; 161 } 162 if (data == null) { 163 if (selector !== document) { 164 data = selector; 165 } else { 166 data = ''; 167 } 168 } 169 events.saveEvent(config_data.user_id, config_data.user_environment_id, eventType, description, xCoord, yCoord, false, data); 170 }); 171 }; 172 173 /** 174 * Saves an event 175 * 176 * @param userId 177 * @param userEnvironmentId 178 * @param eventType 179 * @param url 180 * @param description 181 * @param xCoord 182 * @param yCoord 183 * @param pageWidth 184 */ 185 this.saveEvent = function(userId, userEnvironmentId, eventType, description, xCoord, yCoord, isTap, data) { 186 187 // remove hash tags from URL 188 var url = window.location.href; 189 var hashIndex = url.indexOf('#'); 190 if (hashIndex > 0) { 191 url = url.substring(0, hashIndex); 192 } 193 194 userId = config_data.user_id; 195 userEnvironmentId = config_data.user_environment_id; 196 197 var data = { 198 action : "save_user_event", 199 nonce : config_data.ajax_nonce, 200 pluginVersion : config_data.plugin_version, 201 202 userId : userId, 203 userEnvironmentId : userEnvironmentId, 204 eventType : eventType, 205 url : url, 206 description : description, 207 xCoord : xCoord, 208 yCoord : yCoord, 209 pageWidth : utils.getPageWidth(), 210 data : '', 211 ipAddress : config_data.ip_address, 212 sessionId : config_data.session_id, 213 debug : debug, 214 drawHeatMapEnabled : drawHeatmapEnabled, 215 spotRadius : spotRadius, 216 widthAllowance : config_data.width_allowance 217 }; 218 219 220 jQuery.post(config_data.ajax_url, data, function(response) { 221 var eventData = jQuery.parseJSON(response); 222 223 if ((data.eventType == "mouse_click" || data.eventType == "touchscreen_tap") && debug == true && drawHeatmapEnabled === true) { 224 if (isHeatmap) { 225 // if heatmap.js 226 heatmap.store.addDataPoint(xCoord, yCoord, 1); 227 } else { 228 var heatValue = eventData.heat_value; 229 drawing.drawArc(xCoord, yCoord, heatValue); 230 } 231 } 232 }); 233 }; 234};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.