1/** 2 * Developer's Notice: 3 * 4 * Note: JS in this file (and this file itself) is not guaranteed backwards compatibility. JS can be added, changed or removed at any time without notice. 5 * For more information see the `Backwards Compatibility Guidelines for Developers` section of the README.md file. 6 */ 7/** 8 * Handles: 9 * - JS Events handling 10 * 11 * @since 7.15.0 12 */ 13var MonsterInsights = function () { 14 /* MonsterInsights JS events tracking works on all major browsers, including IE starting at IE 7, via polyfills for any major JS function used that 15 is not supported by at least 95% of the global and/or US browser marketshare. Currently, IE 7 & 8 which as of 2/14/17 have under 0.25% global marketshare, require 16 us to polyfill Array.prototype.lastIndexOf, and if they continue to drop, we might remove this polyfill at some point. In that case note that events tracking 17 for IE 7/8 will continue to work, with the exception of events tracking of downloads. */ 18 var lastClicked = []; 19 var internalAsOutboundCategory = ''; 20 var beforeUnloadChanged = false; 21 22 this.setLastClicked = function (valuesArray, fieldsArray, tracked) { 23 valuesArray = typeof valuesArray !== 'undefined' ? valuesArray : []; 24 fieldsArray = typeof fieldsArray !== 'undefined' ? fieldsArray : []; 25 tracked = typeof tracked !== 'undefined' ? tracked : false; 26 27 lastClicked.valuesArray = valuesArray; 28 lastClicked.fieldsArray = fieldsArray; 29 }; 30 31 this.getLastClicked = function () { 32 return lastClicked; 33 }; 34 35 this.setInternalAsOutboundCategory = function (category) { 36 internalAsOutboundCategory = category; 37 }; 38 39 this.getInternalAsOutboundCategory = function () { 40 return internalAsOutboundCategory; 41 }; 42 43 this.sendEvent = function (type, action, fieldsArray) { 44 __gtagTrackerSend(type, action, fieldsArray, []); 45 }; 46 47 function __gtagTrackerIsDebug() { 48 if (window.monsterinsights_debug_mode) { 49 return true; 50 } else { 51 return false; 52 } 53 } 54 55 function cloneFields(fields, allowedKeys, disallowedKeys) { 56 var clone = {}; 57 58 for (var key in fields) { 59 if (!fields.hasOwnProperty(key)) { 60 continue 61 } 62 63 if (allowedKeys && allowedKeys.indexOf(key) === -1) { 64 continue 65 } 66 67 if (disallowedKeys && disallowedKeys.indexOf(key) > -1) { 68 continue 69 } 70 71 clone[key] = fields[key]; 72 } 73 74 return clone; 75 } 76 77 function __gtagMaybeTrackerV4(type, action, fieldsArray) { 78 if (!monsterinsights_frontend.v4_id || type !== 'event') { 79 return; 80 } 81 82 var eventCategory = fieldsArray.event_category || ''; 83 84 var fieldsToRemove = [ 85 'event_name', 86 'event_category', 87 'event_label', 88 'value', 89 ]; 90 91 var fields = cloneFields(fieldsArray, null, fieldsToRemove); 92 fields.action = action; 93 fields.send_to = monsterinsights_frontend.v4_id; 94 95 let hitType = eventCategory.replace('-', '_'); 96 if (eventCategory.indexOf('outbound-link') !== -1) { 97 hitType = 'click' 98 } else if (eventCategory === 'download') { 99 hitType = 'file_download' 100 } 101 102 __gtagTracker(type, hitType, fields); 103 } 104 105 function __gtagTrackerSendDual(type, action, fieldsArray, valuesArray) { 106 type = typeof type !== 'undefined' ? type : 'event'; 107 action = typeof action !== 'undefined' ? action : ''; 108 valuesArray = typeof valuesArray !== 'undefined' ? valuesArray : []; 109 fieldsArray = typeof fieldsArray !== 'undefined' ? fieldsArray : {}; 110 111 __gtagMaybeTrackerV4(type, action, fieldsArray); 112 113 lastClicked.valuesArray = valuesArray; 114 lastClicked.fieldsArray = fieldsArray; 115 lastClicked.fieldsArray.event_action = action; 116 lastClicked.tracked = true; 117 __gtagTrackerLog('Tracked: ' + valuesArray.type); 118 __gtagTrackerLog(lastClicked); 119 } 120 121 /** 122 * This attempts to be compatible with the gtag function. 123 * 124 * @see https://developers.google.com/analytics/devguides/collection/gtagjs 125 * @param type string Type of request, event, timing, config. 126 * @param action string Event action or UA for config. 127 * @param fieldsArray object The configuration object. 128 * @param valuesArray object The values for the log. 129 * @private 130 */ 131 function __gtagTrackerSend(type, action, fieldsArray, valuesArray) { 132 type = typeof type !== 'undefined' ? type : 'event'; 133 action = typeof action !== 'undefined' ? action : ''; 134 valuesArray = typeof valuesArray !== 'undefined' ? valuesArray : []; 135 fieldsArray = typeof fieldsArray !== 'undefined' ? fieldsArray : {}; 136 137 __gtagTracker(type, action, fieldsArray); 138 139 lastClicked.valuesArray = valuesArray; 140 lastClicked.fieldsArray = fieldsArray; 141 lastClicked.fieldsArray.event_action = action; 142 lastClicked.tracked = true; 143 __gtagTrackerLog('Tracked: ' + valuesArray.type); 144 __gtagTrackerLog(lastClicked); 145 } 146 147 function __gtagTrackerNotSend(valuesArray) { 148 valuesArray = typeof valuesArray !== 'undefined' ? valuesArray : []; 149 150 lastClicked.valuesArray = valuesArray; 151 lastClicked.fieldsArray = []; 152 lastClicked.tracked = false;
153 __gtagTrackerLog('Not Tracked: ' + valuesArray.exit); 154 __gtagTrackerLog(lastClicked); 155 } 156 157 function __gtagTrackerLog(message) { 158 if (__gtagTrackerIsDebug()) { 159 console.dir(message); 160 } 161 } 162 163 function __gtagTrackerStringTrim(x) { 164 return x.replace(/^\s+|\s+$/gm, ''); 165 } 166 167 function __gtagTrackerGetDomain() { 168 var i = 0, currentdomain = document.domain, p = currentdomain.split('.'), s = '_gd' + ( 169 new Date() 170 ).getTime(); 171 while (i < (p.length - 1) && document.cookie.indexOf(s + '=' + s) == -1) { 172 currentdomain = p.slice(-1 - ( 173 ++i 174 )).join('.'); 175 document.cookie = s + "=" + s + ";domain=" + currentdomain + ";"; 176 } 177 document.cookie = s + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;domain=" + currentdomain + ";"; 178 return currentdomain; 179 } 180 181 function __gtagTrackerGetExtension(extension) { 182 extension = extension.toString(); 183 extension = extension.substring(0, ( 184 extension.indexOf("#") == -1 185 ) ? extension.length : extension.indexOf("#")); /* Remove the anchor at the end, if there is one */ 186 extension = extension.substring(0, ( 187 extension.indexOf("?") == -1 188 ) ? extension.length : extension.indexOf("?")); /* Remove the query after the file name, if there is one */ 189 extension = extension.substring(extension.lastIndexOf("/") + 1, extension.length); /* Remove everything before the last slash in the path */ 190 if (extension.length > 0 && extension.indexOf('.') !== -1) { /* If there's a period left in the URL, then there's a extension. Else it is not a extension. */ 191 extension = extension.substring(extension.lastIndexOf(".") + 1); /* Remove everything but what's after the first period */ 192 return extension; 193 } else { 194 return ""; 195 } 196 } 197 198 function __gtagTrackerTrackedClick(event) { 199 return event.which == 1 || event.which == 2 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey; 200 } 201 202 function __gtagTrackerGetDownloadExtensions() { 203 var download_extensions = []; 204 if (typeof monsterinsights_frontend.download_extensions == 'string') { 205 download_extensions = monsterinsights_frontend.download_extensions.split(","); 206 } 207 return download_extensions; 208 } 209 210 function __gtagTrackerGetInboundPaths() { 211 var inbound_paths = []; 212 if (typeof monsterinsights_frontend.inbound_paths == 'string') { 213 inbound_paths = JSON.parse(monsterinsights_frontend.inbound_paths); 214 } 215 216 return inbound_paths; 217 } 218 219 function __gtagTrackerTrackedClickType(event) { 220 if (event.which == 1) { 221 return 'event.which=1'; 222 } else if (event.which == 2) { 223 return 'event.which=2'; 224 } else if (event.metaKey) { 225 return 'metaKey'; 226 } else if (event.ctrlKey) { 227 return 'ctrlKey'; 228 } else if (event.shiftKey) { 229 return 'shiftKey'; 230 } else if (event.altKey) { 231 return 'altKey'; 232 } else { 233 return ''; 234 } 235 } 236 237 function __gtagTrackerLinkType(el) { 238 var download_extensions = __gtagTrackerGetDownloadExtensions(); 239 var inbound_paths = __gtagTrackerGetInboundPaths(); 240 var type = 'unknown'; 241 var link = el.href; 242 var extension = __gtagTrackerGetExtension(el.href); 243 var currentdomain = __gtagTrackerGetDomain(); 244 var hostname = el.hostname; 245 var protocol = el.protocol; 246 var pathname = el.pathname; 247 link = link.toString(); 248 var index, len; 249 var category = el.getAttribute("data-vars-ga-category"); 250 251 if (category) { 252 return category; 253 } 254 255 if (link.match(/^javascript\:/i)) { 256 type = 'internal'; /* if it's a JS link, it's internal */ 257 } else if (protocol && protocol.length > 0 && ( 258 __gtagTrackerStringTrim(protocol) == 'tel' || __gtagTrackerStringTrim(protocol) == 'tel:' 259 )) { /* If it's a telephone link */ 260 type = "tel"; 261 } else if (protocol && protocol.length > 0 && ( 262 __gtagTrackerStringTrim(protocol) == 'mailto' || __gtagTrackerStringTrim(protocol) == 'mailto:' 263 )) { /* If it's a email */ 264 type = "mailto"; 265 } else if (hostname && currentdomain && hostname.length > 0 && currentdomain.length > 0 && !hostname.endsWith('.' + currentdomain) && hostname !== currentdomain) { /* If it's a outbound */ 266 type = "external"; 267 } else if (pathname && JSON.stringify(inbound_paths) != "{}" && pathname.length > 0) { /* If it's an internal as outbound */ 268 var inbound_paths_length = inbound_paths.length; 269 for (var inbound_paths_index = 0; inbound_paths_index < inbound_paths_length; inbound_paths_index++) { 270 if (inbound_paths[inbound_paths_index].path && inbound_paths[inbound_paths_index].label && inbound_paths[inbound_paths_index].path.length > 0 && inbound_paths[inbound_paths_index].label.length > 0 && pathname.startsWith(inbound_paths[inbound_paths_index].path)) {
271 type = "internal-as-outbound"; 272 internalAsOutboundCategory = "outbound-link-" + inbound_paths[inbound_paths_index].label; 273 break; 274 } 275 } 276 /* Enable window.monsterinsights_experimental_mode at your own risk. We might eventually remove it. Also you may/can/will burn through GA quota for your property quickly. */ 277 } else if (hostname && window.monsterinsights_experimental_mode && hostname.length > 0 && document.domain.length > 0 && hostname !== document.domain) { /* If it's a cross-hostname link */ 278 type = "cross-hostname"; 279 } 280 281 if (extension && ( 282 type === 'unknown' || 'external' === type 283 ) && download_extensions.length > 0 && extension.length > 0) { /* If it's a download */ 284 for (index = 0, len = download_extensions.length; index < len; ++index) { 285 if (download_extensions[index].length > 0 && ( 286 link.endsWith(download_extensions[index]) || download_extensions[index] == extension 287 )) { 288 type = "download"; 289 break; 290 } 291 } 292 } 293 294 if (type === 'unknown') { 295 type = 'internal'; 296 } 297 return type; 298 } 299 300 function __gtagTrackerLinkTarget(el, event) { 301 302 /* Is actual target set and not _(self|parent|top)? */ 303 var target = ( 304 el.target && !el.target.match(/^_(self|parent|top)$/i) 305 ) ? el.target : false;
306 307 /* Assume a target if Ctrl|shift|meta-click */ 308 if (event.ctrlKey || event.shiftKey || event.metaKey || event.which == 2) { 309 target = "_blank"; 310 } 311 return target; 312 } 313 314 function __gtagTrackerGetTitle(el) { 315 if (el.getAttribute("data-vars-ga-label") && el.getAttribute("data-vars-ga-label").replace(/\n/ig, '')) { 316 return el.getAttribute("data-vars-ga-label").replace(/\n/ig, ''); 317 } else if (el.title && el.title.replace(/\n/ig, '')) { 318 return el.title.replace(/\n/ig, ''); 319 } else if (el.innerText && el.innerText.replace(/\n/ig, '')) { 320 return el.innerText.replace(/\n/ig, ''); 321 } else if (el.getAttribute('aria-label') && el.getAttribute('aria-label').replace(/\n/ig, '')) { 322 return el.getAttribute('aria-label').replace(/\n/ig, ''); 323 } else if (el.alt && el.alt.replace(/\n/ig, '')) { 324 return el.alt.replace(/\n/ig, ''); 325 } else if (el.textContent && el.textContent.replace(/\n/ig, '')) { 326 return el.textContent.replace(/\n/ig, ''); 327 } else { 328 return undefined; 329 } 330 } 331 332 function __gtagTrackerGetInnerTitle(el) { 333 var children = el.children; 334 var count = 0; 335 var child; 336 var value; 337 for (var i = 0; i < children.length; i++) { 338 child = children[i];
339 value = __gtagTrackerGetTitle(child); 340 if (value) { 341 return value; 342 } 343 /* max search 100 elements to ensure performance */ 344 if (count == 99) { 345 return undefined; 346 } 347 count++; 348 } 349 return undefined; 350 } 351 352 function __gtagTrackerClickEvent(event) { 353 var el = event.srcElement || event.target; 354 var valuesArray = []; 355 var fieldsArray; 356 357 /* Start Values Array */ 358 valuesArray.el = el; 359 valuesArray.click_type = __gtagTrackerTrackedClickType(event); 360 361 /* If GA is blocked or not loaded, or not main|middle|touch click then don't track */ 362 if ('undefined' === typeof __gtagTracker || !__gtagTrackerTrackedClick(event)) { 363 valuesArray.exit = 'loaded'; 364 __gtagTrackerNotSend(valuesArray); 365 return; 366 } 367 368 /* Loop up the DOM tree through parent elements if clicked element is not a link (eg: an image inside a link) */ 369 while (el && ( 370 typeof el.tagName == 'undefined' || el.tagName.toLowerCase() != 'a' || !el.href 371 )) { 372 el = el.parentNode; 373 } 374 375 /* if a link with valid href has been clicked */ 376 if (el && el.href && !el.hasAttribute('xlink:href')) { 377 var link = el.href; /* What link are we tracking */ 378 var extension = __gtagTrackerGetExtension(el.href); /* What extension is this link */ 379 var download_extensions = __gtagTrackerGetDownloadExtensions(); /* Let's get the extensions to track */ 380 var inbound_paths = __gtagTrackerGetInboundPaths(); /* Let's get the internal paths to track */ 381 var home_url = monsterinsights_frontend.home_url; /* Let's get the url to compare for external/internal use */ 382 var currentdomain = __gtagTrackerGetDomain(); /* What domain are we on? */ 383 var type = __gtagTrackerLinkType(el); /* What type of link is this? */ 384 var target = __gtagTrackerLinkTarget(el, event); /* Is a new tab/window being opened? */ 385 var action = el.getAttribute("data-vars-ga-action"); 386 var label = el.getAttribute("data-vars-ga-label"); 387 388 /* Element */ 389 valuesArray.el = el; /* el is an a element so we can parse it */ 390 valuesArray.el_href = el.href; /* "http://example.com:3000/pathname/?search=test#hash" */ 391 valuesArray.el_protocol = el.protocol; /* "http:" */ 392 valuesArray.el_hostname = el.hostname; /* "example.com" */ 393 valuesArray.el_port = el.port; /* "3000" */ 394 valuesArray.el_pathname = el.pathname; /* "/pathname/" */ 395 valuesArray.el_search = el.search; /* "?search=test" */ 396 valuesArray.el_hash = el.hash; /* "#hash" */ 397 valuesArray.el_host = el.host; /* "example.com:3000" */ 398 valuesArray.el_classes = el.getAttribute('class') 399 valuesArray.el_id = el.id 400 401 /* Settings */ 402 valuesArray.debug_mode = __gtagTrackerIsDebug(); /* "example.com:3000" */ 403 valuesArray.download_extensions = download_extensions; /* Let's get the extensions to track */ 404 valuesArray.inbound_paths = inbound_paths; /* Let's get the internal paths to track */ 405 valuesArray.home_url = home_url; /* Let's get the url to compare for external/internal use */ 406 407 /* Parsed/Logic */ 408 valuesArray.link = link; /* What link are we tracking */ 409 valuesArray.extension = extension; /* What extension is this link */ 410 valuesArray.type = type; /* What type of link is this */ 411 valuesArray.target = target; /* Is a new tab/window being opened? */ 412 valuesArray.title = __gtagTrackerGetTitle(el); /* Try link title, then text content */ 413 414 /* only find innerTitle if we need one */ 415 if (!valuesArray.label && !valuesArray.title) { 416 valuesArray.title = __gtagTrackerGetInnerTitle(el); 417 } 418 419 /* Let's track everything but internals (that aren't internal-as-externals) and javascript */ 420 if (type !== 'internal' && type !== 'javascript') { 421 422 var __gtagTrackerHitBackRun = false; /* Tracker has not yet run */ 423 424 /* HitCallback to open link in same window after tracker */ 425 var __gtagTrackerHitBack = function () { 426 /* Run the hitback only once */ 427 if (__gtagTrackerHitBackRun) { 428 return; 429 } 430 maybePreventBeforeUnload(); 431 __gtagTrackerHitBackRun = true; 432 433 if ( el.attributes.download ) { 434 var linkTag = document.createElement('a'); 435 linkTag.href = el.href; 436 linkTag.download = el.download; 437 linkTag.click(); 438 } else { 439 window.location.href = link; 440 } 441 }; 442 443 var __gtagTrackerNoRedirectExternal = function () { 444 valuesArray.exit = 'external'; 445 __gtagTrackerNotSend(valuesArray); 446 }; 447 448 var __gtagTrackerNoRedirectInboundAsExternal = function () {
449 valuesArray.exit = 'internal-as-outbound'; 450 __gtagTrackerNotSend(valuesArray); 451 }; 452 var __gtagTrackerNoRedirectCrossHostname = function () { 453 valuesArray.exit = 'cross-hostname'; 454 __gtagTrackerNotSend(valuesArray); 455 }; 456 457 if (target || type == 'mailto' || type == 'tel') { /* If target opens a new window then just track */ 458 if (type == 'download') { 459 fieldsArray = { 460 event_category: 'download', 461 event_label: label || valuesArray.title, 462 file_extension: valuesArray.extension, 463 file_name: valuesArray.link.replace(/^.*\//g, ''), 464 link_text: label || valuesArray.title, 465 link_url: link, 466 link_domain: valuesArray.el_hostname, 467 link_classes: valuesArray.el_classes, 468 link_id: valuesArray.el_id, 469 }; 470 } else if (type == 'tel') { 471 fieldsArray = { 472 event_category: 'tel', 473 event_label: label || valuesArray.title.replace('tel:', ''), 474 tel_number: link.replace('tel:', ''), 475 link_text: label || valuesArray.title, 476 link_url: link, 477 link_classes: valuesArray.el_classes, 478 link_id: valuesArray.el_id, 479 }; 480 } else if (type == 'mailto') { 481 fieldsArray = { 482 event_category: 'mailto', 483 event_label: label || valuesArray.title.replace('mailto:', ''), 484 email_address: link.replace('mailto:', ''), 485 link_text: label || valuesArray.title.replace('mailto:', ''), 486 link_url: link, 487 link_classes: valuesArray.el_classes, 488 link_id: valuesArray.el_id, 489 }; 490 } else if (type == 'internal-as-outbound') { 491 fieldsArray = { 492 event_category: internalAsOutboundCategory, 493 event_label: label || valuesArray.title, 494 event_name: 'click', 495 is_affiliate_link: true, 496 affiliate_label: internalAsOutboundCategory.replace('outbound-link-', ''), 497 link_text: label || valuesArray.title, 498 link_url: link, 499 link_domain: valuesArray.el_hostname, 500 link_classes: valuesArray.el_classes, 501 link_id: valuesArray.el_id, 502 outbound: true, 503 }; 504 } else if (type == 'external') { 505 fieldsArray = { 506 event_category: 'outbound-link', 507 event_label: label || valuesArray.title, 508 is_affiliate_link: false, 509 link_text: label || valuesArray.title, 510 link_url: link, 511 link_domain: valuesArray.el_hostname, 512 link_classes: valuesArray.el_classes, 513 link_id: valuesArray.el_id, 514 outbound: true, 515 }; 516 } else if (type == 'cross-hostname') { 517 fieldsArray = { 518 event_category: 'cross-hostname', 519 event_label: label || valuesArray.title, 520 link_text: label || valuesArray.title, 521 link_url: link, 522 link_domain: valuesArray.el_hostname, 523 link_classes: valuesArray.el_classes, 524 link_id: valuesArray.el_id, 525 }; 526 } 527 528 if (fieldsArray) { 529 __gtagTrackerSendDual('event', action || link, fieldsArray, valuesArray); 530 } else { 531 if (type && type != 'internal') { 532 fieldsArray = { 533 event_category: type, 534 event_label: label || valuesArray.title, 535 link_text: label || valuesArray.title, 536 link_url: link, 537 link_domain: valuesArray.el_hostname, 538 link_classes: valuesArray.el_classes, 539 link_id: valuesArray.el_id, 540 }; 541 542 __gtagTrackerSendDual('event', action || link, fieldsArray, valuesArray); 543 } else { 544 valuesArray.exit = 'type'; 545 __gtagTrackerNotSend(valuesArray); 546 } 547 } 548 } else { 549 /* Prevent standard click, track then open */ 550 if (type != 'cross-hostname' && type != 'external' && type != 'internal-as-outboun
550d') { 551 if (!event.defaultPrevented) { 552 if (event.preventDefault) { 553 event.preventDefault(); 554 } else { 555 event.returnValue = false; 556 } 557 } 558 } 559 560 if (type == 'download') { 561 fieldsArray = { 562 event_category: 'download', 563 event_label: label || valuesArray.title, 564 event_callback: __gtagTrackerHitBack, 565 file_extension: valuesArray.extension, 566 file_name: valuesArray.link.replace(/^.*\//g, ''), 567 link_text: label || valuesArray.title, 568 link_url: link, 569 link_domain: valuesArray.el_hostname, 570 link_classes: valuesArray.el_classes, 571 link_id: valuesArray.el_id, 572 }; 573 574 __gtagTrackerSendDual('event', action || link, fieldsArray, valuesArray); 575 } else if (type == 'internal-as-outbound') { 576 beforeUnloadChanged = true; 577 window.onbeforeunload = function (e) { 578 if (!event.defaultPrevented) { 579 if (event.preventDefault) { 580 event.preventDefault(); 581 } else { 582 event.returnValue = false; 583 } 584 } 585 586 fieldsArray = { 587 event_category: internalAsOutboundCategory, 588 event_label: label || valuesArray.title, 589 event_callback: __gtagTrackerHitBack, 590 is_affiliate_link: true, 591 affiliate_label: internalAsOutboundCategory.replace('outbound-link-', ''), 592 link_text: label || valuesArray.title, 593 link_url: link, 594 link_domain: valuesArray.el_hostname, 595 link_classes: valuesArray.el_classes, 596 link_id: valuesArray.el_id, 597 outbound: true, 598 }; 599 600 if (navigator.sendBeacon) { 601 fieldsArray.transport = 'beacon'; 602 } 603 604 __gtagTrackerSendDual('event', action || link, fieldsArray, valuesArray); 605 606 setTimeout(__gtagTrackerHitBack, 1000); 607 }; 608 } else if (type == 'external') { 609 beforeUnloadChanged = true; 610 window.onbeforeunload = function (e) { 611 612 fieldsArray = { 613 event_category: 'outbound-link', 614 event_label: label || valuesArray.title, 615 event_callback: __gtagTrackerHitBack, 616 is_affiliate_link: false, 617 link_text: label || valuesArray.title, 618 link_url: link, 619 link_domain: valuesArray.el_hostname, 620 link_classes: valuesArray.el_classes, 621 link_id: valuesArray.el_id, 622 outbound: true, 623 }; 624 625 if (navigator.sendBeacon) { 626 fieldsArray.transport = 'beacon'; 627 } 628 629 __gtagTrackerSendDual('event', action || link, fieldsArray, valuesArray); 630 }; 631 } else if (type == 'cross-hostname') { 632 beforeUnloadChanged = true; 633 window.onbeforeunload = function (e) { 634 if (!event.defaultPrevented) { 635 if (event.preventDefault) { 636 event.preventDefault(); 637 } else { 638 event.returnValue = false; 639 } 640 } 641 642 fieldsArray = { 643 event_category: 'cross-hostname', 644 event_label: label || valuesArray.title, 645 event_callback: __gtagTrackerHitBack, 646 link_text: label || valuesArray.title, 647 link_url: link, 648 link_domain: valuesArray.el_hostname, 649 link_classes: valuesArray.el_classes, 650 link_id: valuesArray.el_id, 651 }; 652 653 if (navigator.sendBeacon) { 654 fieldsArray.transport = 'beacon'; 655 } 656 657 __gtagTrackerSendDual('event', action || link, fieldsArray, valuesArray); 658 setTimeout(__gtagTrackerHitBack, 1000); 659 }; 660 } else {
661 if (type && type !== 'internal') { 662 fieldsArray = { 663 event_category: type, 664 event_label: label || valuesArray.title, 665 event_callback: __gtagTrackerHitBack, 666 link_text: label || valuesArray.title, 667 link_url: link, 668 link_domain: valuesArray.el_hostname, 669 link_classes: valuesArray.el_classes, 670 link_id: valuesArray.el_id, 671 }; 672 673 __gtagTrackerSendDual('event', action || link, fieldsArray, valuesArray); 674 } else { 675 valuesArray.exit = 'type'; 676 __gtagTrackerNotSend(valuesArray); 677 } 678 } 679 680 if (type != 'external' && type != 'cross-hostname' && type != 'internal-as-outbound') { 681 /* Run event_callback again if GA takes longer than 1 second */ 682 setTimeout(__gtagTrackerHitBack, 1000); 683 } else { 684 if (type == 'external') { 685 setTimeout(__gtagTrackerNoRedirectExternal, 1100); 686 } else if (type == 'cross-hostname') { 687 setTimeout(__gtagTrackerNoRedirectCrossHostname, 1100); 688 } else { 689 setTimeout(__gtagTrackerNoRedirectInboundAsExternal, 1100); 690 } 691 } 692 693 // Clear out the beforeunload event if it was set to avoid sending false events. 694 setTimeout(maybePreventBeforeUnload, 100); 695 } 696 } else { 697 maybePreventBeforeUnload(); 698 valuesArray.exit = 'internal'; 699 __gtagTrackerNotSend(valuesArray); 700 } 701 } else { 702 valuesArray.exit = 'notlink'; 703 __gtagTrackerNotSend(valuesArray); 704 } 705 } 706 707 var prevHash = window.location.hash; 708 709 function __gtagTrackerHashChangeEvent() { 710 /* Todo: Ready this section for JS unit testing */ 711 if (monsterinsights_frontend.hash_tracking === "true" && prevHash != window.location.hash && monsterinsights_frontend.v4_id) { 712 prevHash = window.location.hash; 713 714 __gtagTracker('config', monsterinsights_frontend.v4_id, { 715 page_path: location.pathname + location.search + location.hash, 716 }); 717 718 __gtagTrackerLog("Hash change to: " + location.pathname + location.search + location.hash); 719 } else { 720 __gtagTrackerLog("Hash change to (untracked): " + location.pathname + location.search + location.hash); 721 } 722 } 723 724 function maybePreventBeforeUnload() { 725 if (beforeUnloadChanged) { 726 window.onbeforeunload = null; 727 } 728 } 729 730 /* Attach the event to all clicks in the document after page has loaded */ 731 var __gtagTrackerWindow = window; 732 if (__gtagTrackerWindow.addEventListener) { 733 __gtagTrackerWindow.addEventListener( 734 "load", 735 function () { 736 document.body.addEventListener( 737 "click", 738 __gtagTrackerClickEvent, 739 false 740 ); 741 }, 742 false 743 ); 744 window.addEventListener("hashchange", __gtagTrackerHashChangeEvent, false); 745 } else { 746 if (__gtagTrackerWindow.attachEvent) { 747 __gtagTrackerWindow.attachEvent( 748 "onload", 749 function () { 750 document.body.attachEvent("onclick", __gtagTrackerClickEvent); 751 } 752 ); 753 window.attachEvent("onhashchange", __gtagTrackerHashChangeEvent); 754 } 755 } 756 757 if (typeof String.prototype.endsWith !== 'function') { 758 String.prototype.endsWith = function (suffix) { 759 return this.indexOf(suffix, this.length - suffix.length) !== -1; 760 }; 761 } 762 if (typeof String.prototype.startsWith !== 'function') { 763 String.prototype.startsWith = function (prefix) { 764 return this.indexOf(prefix) === 0; 765 }; 766 } 767 768 if (typeof Array.prototype.lastIndexOf !== 'function') { 769 Array.prototype.lastIndexOf = function (searchElement /*, fromIndex*/) { 770 'use strict'; 771 772 if (this === void 0 || this === null) { 773 throw new TypeError(); 774 } 775 776 var n, k, 777 t = Object(this), 778 len = t.length >>> 0; /* jshint ignore:line */ 779 if (len === 0) { 780 return -1; 781 } 782 783 n = len - 1; 784 if (arguments.length > 1) { 785 n = Number(arguments[1]); 786 if (n != n) { 787 n = 0; 788 } else if (n != 0 && n != ( 789 1 / 0 790 ) && n != -( 791 1 / 0 792 )) { /* jshint ignore:line */ 793 n = ( 794 n > 0 || -1 795 ) * Math.floor(Math.abs(n)); 796 } 797 } 798 799 for (k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n); k >= 0; k--) { 800 if (k in t && t[k] === searchElement) { 801 return k; 802 } 803 } 804 return -1; 805 }; 806 } 807}; 808var MonsterInsightsObject = new MonsterInsights();
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.