1/* global pysOptions */ 2 3// https://bitbucket.org/pixelyoursite/pys_pro_7/issues/7/possible-ie-11-error 4// https://tc39.github.io/ecma262/#sec-array.prototype.includes 5if (!Array.prototype.includes) { 6 Object.defineProperty(Array.prototype, 'includes', { 7 value: function (searchElement, fromIndex) { 8 9 if (this == null) { 10 throw new TypeError('"this" is null or not defined'); 11 } 12 13 // 1. Let O be ? ToObject(this value). 14 var o = Object(this); 15 16 // 2. Let len be ? ToLength(? Get(O, "length")). 17 var len = o.length >>> 0; 18 19 // 3. If len is 0, return false. 20 if (len === 0) { 21 return false; 22 } 23 24 // 4. Let n be ? ToInteger(fromIndex). 25 // (If fromIndex is undefined, this step produces the value 0.) 26 var n = fromIndex | 0; 27 28 // 5. If n ⥠0, then 29 // a. Let k be n. 30 // 6. Else n < 0, 31 // a. Let k be len + n. 32 // b. If k < 0, let k be 0. 33 var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); 34 35 function sameValueZero(x, y) { 36 return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); 37 } 38 39 // 7. Repeat, while k < len 40 while (k < len) { 41 // a. Let elementK be the result of ? Get(O, ! ToString(k)). 42 // b. If SameValueZero(searchElement, elementK) is true, return true. 43 if (sameValueZero(o[k], searchElement)) { 44 return true; 45 } 46 // c. Increase k by 1. 47 k++; 48 } 49 50 // 8. Return false 51 return false; 52 } 53 }); 54} 55 56!function ($, options) { 57 58 if (options.debug) { 59 console.log('PYS:', options); 60 } 61 62 var dummyPinterest = function () { 63 64 /** 65 * Public API 66 */ 67 return { 68 69 isEnabled: function () { 70 }, 71 72 disable: function () { 73 }, 74 75 loadPixel: function () { 76 }, 77 78 fireEvent: function (name, data) { 79 return false; 80 }, 81 82 onCommentEvent: function () { 83 }, 84 85 onDownloadEvent: function (params) { 86 }, 87 88 onFormEvent: function (params) { 89 }, 90 91 onWooAddToCartOnButtonEvent: function (product_id) { 92 }, 93 94 onWooAddToCartOnSingleEvent: function (product_id, qty, is_variable, is_external, $form) { 95 }, 96 97 onWooRemoveFromCartEvent: function (cart_item_hash) { 98 }, 99 100 onEddAddToCartOnButtonEvent: function (download_id, price_index, qty) { 101 }, 102 103 onEddRemoveFromCartEvent: function (item) { 104 }, 105 onPageScroll: function (event) {}, 106 onTime: function (event) { 107 108 }, 109 110 } 111 112 }(); 113 114 var dummyBing = function () { 115 116 /** 117 * Public API 118 */ 119 return { 120 121 isEnabled: function () { 122 }, 123 124 disable: function () { 125 }, 126 127 loadPixel: function () { 128 }, 129 130 fireEvent: function (name, data) { 131 return false; 132 }, 133 134 onAdSenseEvent: function () { 135 }, 136 137 onClickEvent: function (params) { 138 }, 139 140 onWatchVideo: function (params) { 141 }, 142 143 onCommentEvent: function () { 144 }, 145 146 onFormEvent: function (params) { 147 }, 148 149 onDownloadEvent: function (params) { 150 }, 151 152 onWooAddToCartOnButtonEvent: function (product_id) { 153 }, 154 155 onWooAddToCartOnSingleEvent: function (product_id, qty, is_variable, is_external, $form) { 156 }, 157 158 onWooRemoveFromCartEvent: function (cart_item_hash) { 159 }, 160 161 onWooAffiliateEvent: function (product_id) { 162 }, 163 164 onWooPayPalEvent: function () { 165 }, 166 167 onEddAddToCartOnButtonEvent: function (download_id, price_index, qty) { 168 }, 169 170 onEddRemoveFromCartEvent: function (item) { 171 }, 172 173 onPageScroll: function (event) { 174 }, 175 176 onTime: function (event) { 177 }, 178 179 } 180 181 }(); 182 183 var Utils = function (options) { 184 185 var Pinterest = dummyPinterest; 186 187 var Bing = dummyBing; 188 189 var gtag_loaded = false;
190 191 let isNewSession = checkSession(); 192 193 var utmTerms = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']; 194 195 var utmId = ['fbadid', 'gadid', 'padid', 'bingid']; 196 197 function validateEmail(email) { 198 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 199 return re.test(email); 200 } 201 function getDomain(url) { 202 203 url = url.replace(/(https?:\/\/)?(www.)?/i, ''); 204 205 if (url.indexOf('/') !== -1) { 206 return url.split('/')[0]; 207 } 208 209 return url; 210 } 211 function loadPixels() { 212 213 if (!options.gdpr.all_disabled_by_api) { 214 215 if (!options.gdpr.facebook_disabled_by_api) { 216 Facebook.loadPixel(); 217 } 218 219 if (!options.gdpr.analytics_disabled_by_api) { 220 Analytics.loadPixel(); 221 } 222 223 if (!options.gdpr.pinterest_disabled_by_api) { 224 Pinterest.loadPixel(); 225 } 226 227 if (!options.gdpr.bing_disabled_by_api) { 228 Bing.loadPixel(); 229 } 230 } 231 if (options.gdpr.consent_magic_integration_enabled && typeof CS_Data !== "undefined") { 232 if (typeof CS_Data.cs_google_analytics_consent_mode !== "undefined" && CS_Data.cs_google_analytics_consent_mode == 1) { 233 Analytics.loadPixel(); 234 } 235 } 236 237 } 238 239 function checkSession() { 240 let duration = options.last_visit_duration * 60000 241 if(!options.cookie.disabled_all_cookie && !options.cookie.disabled_start_session_cookie) { 242 if (Cookies.get('pys_start_session') === undefined || 243 Cookies.get('pys_session_limit') === undefined) { 244 var now = new Date(); 245 now.setTime(now.getTime() + duration); 246 Cookies.set('pys_session_limit', true, {expires: now}) 247 Cookies.set('pys_start_session', true) 248 return true 249 } 250 } 251 return false 252 253 } 254 255 function getTrafficSource() { 256 257 try { 258 259 let referrer = document.referrer.toString(), 260 source; 261 262 let direct = referrer.length === 0; 263 let internal = direct ? false : referrer.indexOf(options.siteUrl) === 0; 264 let external = !direct && !internal; 265 266 if (external === false) { 267 source = 'direct'; 268 } else { 269 source = referrer; 270 } 271 272 if (source !== 'direct') { 273 // leave only domain (Issue #70) 274 return getDomain(source); 275 } else { 276 return source; 277 } 278 279 } catch (e) { 280 console.error(e); 281 return 'direct'; 282 } 283 284 } 285 286 /** 287 * Return query variables object with where property name is query variable 288 * and property value is query variable value. 289 */ 290 function getQueryVars() { 291 292 try { 293 294 var result = {}, 295 tmp = []; 296 297 window.location.search 298 .substr(1) 299 .split("&") 300 .forEach(function (item) { 301 302 tmp = item.split('='); 303 304 if (tmp.length > 1) { 305 result[tmp[0]] = tmp[1]; 306 } 307 308 }); 309 310 return result; 311 312 } catch (e) { 313 console.error(e); 314 return {}; 315 } 316 317 } 318 319 320 function getLandingPageValue() { 321 let name = "pys_landing_page" 322 if(options.visit_data_model === "last_visit") { 323 name = "last_pys_landing_page" 324 } 325 if(Cookies.get(name) && Cookies.get(name) !== "undefined") { 326 return Cookies.get(name); 327 } 328 else if(options.hasOwnProperty("tracking_analytics") && options.tracking_analytics.TrafficLanding){ 329 return options.tracking_analytics.TrafficLanding; 330 } else{ 331 return ""; 332 } 333 } 334 function getTrafficSourceValue() { 335 let name = "pysTrafficSource" 336 if(options.visit_data_model === "last_visit") { 337 name = "last_pysTrafficSource" 338 } 339 if(Cookies.get(name) && Cookies.get(name) !== "undefined") { 340 return Cookies.get(name); 341 } 342 else if(options.hasOwnProperty("tracking_analytics") && options.tracking_analytics.TrafficSource){ 343 return options.tracking_analytics.TrafficSource; 344 } else{ 345 return ""; 346 } 347 } 348 349 function getUTMId(useLast = false) { 350 try { 351 let cookiePrefix = 'pys_' 352 let terms = []; 353 if (useLast) { 354 cookiePrefix = 'last_pys_' 355 } 356 $.each(utmId, function (index, name) { 357 if (Cookies.get(cookiePrefix + name)) { 358 terms[name] = Cookies.get(cookiePrefix + name) 359 } 360 else if(options.hasOwnProperty("tracking_analytics") && options.tracking_analytics.TrafficUtmsId[name]) { 361 terms[name] = filterEmails(options.tracking_analytics.TrafficUtmsId[name]) 362 } 363 }); 364 return terms; 365 } catch (e) { 366 console.error(e); 367 return []; 368 } 369 } 370 /** 371 * Return UTM terms from request query variables or from cookies. 372 */ 373 function getUTMs(useLast = false) { 374 375 try { 376 let cookiePrefix = 'pys_' 377 if(useLast) { 378 cookiePrefix = 'last_pys_' 379 } 380 let terms = []; 381 $.each(utmTerms, function (index, name) { 382 if (Cookies.get(cookiePrefix + name)) { 383 let value = Cookies.get(cookiePrefix + name); 384 terms[name] = filterEmails(value); // do not allow email in request params (Issue #70) 385 } 386 else if(options.hasOwnProperty("tracking_analytics") && options.tracking_analytics.TrafficUtms[name]) { 387 terms[name] = filterEmails(options.tracking_analytics.TrafficUtms[name]) 388 } 389 }); 390 391 return terms; 392 393 } catch (e) { 394 console.error(e); 395 return []; 396 } 397 398 } 399 400 function getDateTime() { 401 var dateTime = new Array(); 402 var date = new Date(), 403 days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], 404 months = ['January', 'February', 'March', 'April', 'May', 'June', 405 'July', 'August', 'September', 'October', 'November', 'December' 406 ], 407 hours = ['00-01', '01-02', '02-03', '03-04', '04-05', '05-06', '06-07', '07-08', 408 '08-09', '09-10', '10-11', '11-12', '12-13', '13-14', '14-15', '15-16', '16-17', 409 '17-18', '18-19', '19-20', '20-21', '21-22', '22-23', '23-24' 410 ]; 411 dateTime.push(hours[date.getHours()]); 412 dateTime.push(days[date.getDay()]); 413 dateTime.push(months[date.getMonth()]); 414 return dateTime; 415 } 416 417 function filterEmails(value) { 418 return validateEmail(value) ? undefined : value; 419 } 420 421 /** 422 * PUBLIC API 423 */ 424 return { 425 PRODUCT_SIMPLE : 0, 426 PRODUCT_VARIABLE : 1, 427 PRODUCT_BUNDLE : 2, 428 PRODUCT_GROUPED : 3, 429 utmTerms : utmTerms, 430 utmId : utmId, 431 fireEventForAllPixel:function(functionName,events){ 432 if (events.hasOwnProperty(Facebook.tag())) 433 Facebook[functionName](events[Facebook.tag()]); 434 if (events.hasOwnProperty(Analytics.tag())) 435 Analytics[functionName](events[Analytics.tag()]); 436 if (events.hasOwnProperty(Pinterest.tag())) 437 Pinterest[functionName](events[Pinterest.tag()]); 438 if (events.hasOwnProperty(Bing.tag())) 439 Bing[functionName](events[Bing.tag()]); 440 }, 441 442 setupPinterestObject: function () { 443 Pinterest = window.pys.Pinterest || Pinterest; 444 return Pinterest; 445 }, 446 447 setupBingObject: function () { 448 Bing = window.pys.Bing || Bing; 449 return Bing; 450 }, 451 452 // Clone all object members to another and return it 453 copyProperties: function (from, to) { 454 for (var key in from) { 455 if("function" == typeof from[key]) { 456 continue; 457 } 458 to[key] = from[key]; 459 } 460 return to; 461 }, 462 463 manageCookies: function () { 464 let expires = parseInt(options.cookie_duration); // days 465 let queryVars = getQueryVars(); 466 let landing = window.location.href.split('?')[0]; 467 try { 468 // save data for first visit 469 if(Cookies.get('pys_first_visit') === undefined && (!options.cookie.disabled_all_cookie)) { 470 471 if(!options.cookie.disabled_first_visit_cookie) 472 { 473 Cookies.set('pys_first_visit', true, { expires: expires }); 474 } 475 else { 476 Cookies.remove('pys_first_visit') 477 } 478 479 if(!options.cookie.disabled_trafficsource_cookie) 480 { 481 Cookies.set('pysTrafficSource', getTrafficSource(), { expires: expires }); 482 } 483 else { 484 Cookies.remove('pysTrafficSource') 485 } 486 487 if(!options.cookie.disabled_landing_page_cookie) 488 { 489 Cookies.set('pys_landing_page',landing,{ expires: expires }); 490 } 491 else { 492 Cookies.remove('pys_landing_page') 493 } 494 495 if(!options.cookie.disabled_utmTerms_cookie) 496 { 497 $.each(utmTerms, function (index, name) { 498 if (queryVars.hasOwnProperty(name)) { 499 Cookies.set('pys_' + name, queryVars[name], { expires: expires }); 500 } else { 501 Cookies.remove('pys_' + name) 502 } 503 }); 504 } 505 else { 506 $.each(utmTerms, function (index, name) { 507 Cookies.remove('pys_' + name) 508 }); 509 } 510 511 if(!options.cookie.disabled_utmId_cookie) 512 { 513 $.each(utmId,function(index,name) { 514 if (queryVars.hasOwnProperty(name)) { 515 Cookies.set('pys_' + name, queryVars[name], { expires: expires }); 516 } else { 517 Cookies.remove('pys_' + name) 518 } 519 }) 520 } 521 else { 522 $.each(utmId, function (index, name) { 523 Cookies.remove('pys_' + name) 524 }); 525 } 526 } 527 528 // save data for last visit if it new session 529 if(isNewSession && (!options.cookie.disabled_all_cookie)) { 530 if(!options.cookie.disabled_trafficsource_cookie) 531 { 532 Cookies.set('last_pysTrafficSource', getTrafficSource(), { expires: expires }); 533 } 534 else { 535 Cookies.remove('last_pysTrafficSource') 536 } 537 538 if(!options.cookie.disabled_landing_page_cookie) 539 { 540 Cookies.set('last_pys_landing_page',landing,{ expires: expires }); 541 } 542 else { 543 Cookies.remove('last_pys_landing_page') 544 } 545 546 if(!options.cookie.disabled_utmTerms_cookie) 547 { 548 $.each(utmTerms, function (index, name) { 549 if (queryVars.hasOwnProperty(name)) { 550 Cookies.set('last_pys_' + name, queryVars[name], { expires: expires }); 551 } else { 552 Cookies.remove('last_pys_' + name) 553 } 554 }); 555 } 556 else { 557 $.each(utmTerms, function (index, name) { 558 Cookies.remove('last_pys_' + name) 559 }); 560 } 561 562 if(!options.cookie.disabled_utmId_cookie) 563 { 564 $.each(utmId,function(index,name) { 565 if (queryVars.hasOwnProperty(name)) { 566 Cookies.set('last_pys_' + name, queryVars[name], { expires: expires }); 567 } else { 568 Cookies.remove('last_pys_' + name) 569 } 570 }) 571 } 572 else { 573 $.each(utmId, function (index, name) { 574 Cookies.remove('last_pys_' + name) 575 }); 576 } 577 578 } 579 if(options.cookie.disabled_start_session_cookie) { 580 Cookies.remove('pys_start_session') 581 Cookies.remove('pys_session_limit') 582 } 583 if(options.cookie.disabled_all_cookie) 584 { 585 Cookies.remove('pys_first_visit') 586 Cookies.remove('pysTrafficSource') 587 Cookies.remove('pys_landing_page') 588 Cookies.remove('last_pys_landing_page') 589 Cookies.remove('last_pysTrafficSource') 590 Cookies.remove('pys_start_session') 591 Cookies.remove('pys_session_limit') 592 $.each(Utils.utmTerms, function (index, name) { 593 Cookies.remove('pys_' + name) 594 }); 595 $.each(Utils.utmId,function(index,name) { 596 Cookies.remove('pys_' + name) 597 }) 598 $.each(Utils.utmTerms, function (index, name) { 599 Cookies.remove('last_pys_' + name) 600 }); 601 $.each(Utils.utmId,function(index,name) { 602 Cookies.remove('last_pys_' + name) 603 }); 604 } 605 } catch (e) { 606 console.error(e); 607 } 608 }, 609 // clone object 610 clone: function(obj) { 611
611 var copy; 612 613 // Handle the 3 simple types, and null or undefined 614 if (null == obj || "object" != typeof obj) return obj; 615 616 // Handle Date 617 if (obj instanceof Date) { 618 copy = new Date(); 619 copy.setTime(obj.getTime()); 620 return copy; 621 } 622 623 // Handle Array 624 if (obj instanceof Array) { 625 copy = []; 626 for (var i = 0, len = obj.length; i < len; i++) { 627 if("function" == typeof obj[i]) { 628 continue; 629 } 630 copy[i] = Utils.clone(obj[i]); 631 } 632 return copy; 633 } 634 635 // Handle Object 636 if (obj instanceof Object) { 637 copy = {}; 638 for (var attr in obj) { 639 if (obj.hasOwnProperty(attr)) { 640 if("function" == typeof obj[attr]) { 641 continue; 642 } 643 copy[attr] = Utils.clone(obj[attr]); 644 } 645 } 646 return copy; 647 } 648 649 return obj; 650 }, 651 652 // Returns array of elements with given tag name 653 getTagsAsArray: function (tag) { 654 return [].slice.call(document.getElementsByTagName(tag)); 655 }, 656
657 getRequestParams: function () { 658 return []; 659 }, 660 661 /** 662 * CUSTOM EVENTS 663 */ 664 665 setupMouseOverClickEvents: function (eventId, triggers) { 666 667 // Non-default binding used to avoid situations when some code in external js 668 // stopping events propagation, eg. returns false, and our handler will never called 669 document.addEventListener('mouseover', function(event) { 670 var matchedElements = Array.from(document.querySelectorAll(triggers)); 671 if (matchedElements.includes(event.target)) { 672 if (event.target.classList.contains('pys-mouse-over-' + eventId)) { 673 return true; 674 } else { 675 event.target.classList.add('pys-mouse-over-' + eventId); 676 } 677 678 Utils.fireTriggerEvent(eventId); 679 } 680 }); 681 682 }, 683 684 setupCSSClickEvents: function (eventId, triggers) { 685 // Non-default binding used to avoid situations when some code in external js 686 // stopping events propagation, eg. returns false, and our handler will never called 687 document.addEventListener('click', function(event) { 688 var matchedElements = Array.from(document.querySelectorAll(triggers)); 689 if (matchedElements.includes(event.target)) { 690 console.log(event.target) 691 Utils.fireTriggerEvent(eventId); 692 } 693 }, true); 694 }, 695 696 setupURLClickEvents: function () { 697 698 // Non-default binding used to avoid situations when some code in external js 699 // stopping events propagation, eg. returns false, and our handler will never called 700 $('a[data-pys-event-id]').onFirst('click', function (evt) { 701 702 $(this).attr('data-pys-event-id').split(',').forEach(function (eventId) { 703 704 eventId = parseInt(eventId); 705 706 if (isNaN(eventId) === false) { 707 Utils.fireTriggerEvent(eventId); 708 } 709 710 }); 711 712 }); 713 714 715 }, 716 717 setupScrollPosEvents: function (eventId, triggers) { 718 719 var scrollPosThresholds = {}, 720 docHeight = $(document).height() - $(window).height(); 721 722 // convert % to absolute positions 723 $.each(triggers, function (index, scrollPos) { 724 725 // convert % to pixels 726 scrollPos = docHeight * scrollPos / 100; 727 scrollPos = Math.round(scrollPos); 728 729 scrollPosThresholds[scrollPos] = eventId; 730 731 }); 732 733 $(document).on("scroll",function () { 734 735 var scrollPos = $(window).scrollTop(); 736 737 $.each(scrollPosThresholds, function (threshold, eventId) { 738 739 // position has not reached yes 740 if (scrollPos <= threshold) { 741 return true; 742 } 743 744 // fire event only once 745 if (eventId === null) { 746 return true; 747 } else { 748 scrollPosThresholds[threshold] = null; 749 } 750 751 Utils.fireTriggerEvent(eventId); 752 753 }); 754 755 }); 756 757 758 }, 759 setupCommentEvents : function (eventId,triggers) { 760 $('form.comment-form').on("submit",function () { 761 Utils.fireTriggerEvent(eventId); 762 }); 763 }, 764 765 /** 766 * Events 767 */ 768 769 fireTriggerEvent: function (eventId) { 770 771 if (!options.triggerEvents.hasOwnProperty(eventId)) { 772 return; 773 } 774 775 var event = {}; 776 var events = options.triggerEvents[eventId]; 777 778 if (events.hasOwnProperty('facebook')) { 779 event = events.facebook; 780 Facebook.fireEvent(event.name, event); 781 } 782 783 if (events.hasOwnProperty('ga')) { 784 event = events.ga; 785 Analytics.fireEvent(event.name, event); 786 } 787 788 if (events.hasOwnProperty('pinterest')) { 789 event = events.pinterest; 790 Pinterest.fireEvent(event.name, event); 791 } 792 793 if (events.hasOwnProperty('bing')) { 794 event = events.bing; 795 Bing.fireEvent(event.name, event); 796 } 797 }, 798 799 fireStaticEvents: function (pixel) { 800 801 if (options.staticEvents.hasOwnProperty(pixel)) { 802 803 $.each(options.staticEvents[pixel], function (eventName, events) { 804 $.each(events, function (index, eventData) { 805 806 eventData.fired = eventData.fired || false; 807 808 if (!eventData.fired) { 809 810 var fired = false;
811 812 // fire event 813 if ('facebook' === pixel) { 814 fired = Facebook.fireEvent(eventData.name, eventData); 815 } else if ('ga' === pixel) { 816 fired = Analytics.fireEvent(eventData.name, eventData); 817 } else if ('pinterest' === pixel) { 818 fired = Pinterest.fireEvent(eventData.name, eventData); 819 } else if ('bing' === pixel) { 820 fired = Bing.fireEvent(eventData.name, eventData); 821 } 822 823 // prevent event double event firing 824 eventData.fired = fired; 825 826 } 827 828 }); 829 }); 830 831 } 832 833 }, 834 835 /** 836 * Load tag's JS 837 * 838 * @link: https://developers.google.com/analytics/devguides/collection/gtagjs/ 839 * @link: https://developers.google.com/analytics/devguides/collection/gtagjs/custom-dims-mets 840 */ 841 loadGoogleTag: function (id) { 842 843 if (!gtag_loaded) { 844 845 (function (window, document, src) { 846 var a = document.createElement('script'), 847 m = document.getElementsByTagName('script')[0]; 848 a.async = 1; 849 a.src = src; 850 m.parentNode.insertBefore(a, m); 851 })(window, document, '//www.googletagmanager.com/gtag/js?id=' + id); 852 853 window.dataLayer = window.dataLayer || []; 854 window.gtag = window.gtag || function gtag() { 855 dataLayer.push(arguments); 856 }; 857 858 gtag('js', new Date()); 859 860 if ( options.google_consent_mode ) { 861 let data = {}; 862 data[ 'analytics_storage' ] = options.gdpr.analytics_storage.enabled ? options.gdpr.analytics_storage.value : 'granted'; 863 data[ 'ad_storage' ] = options.gdpr.ad_storage.enabled ? options.gdpr.ad_storage.value : 'granted'; 864 data[ 'ad_user_data' ] = options.gdpr.ad_user_data.enabled ? options.gdpr.ad_user_data.value : 'granted'; 865 data[ 'ad_personalization' ] = options.gdpr.ad_personalization.enabled ? options.gdpr.ad_personalization.value : 'granted'; 866 867 gtag( 'consent', 'default', data ); 868 } 869 870 gtag_loaded = true; 871 872 } 873 874 }, 875 876 /** 877 * GDPR 878 */ 879 880 loadPixels: function () { 881 882 if (options.gdpr.ajax_enabled && !options.gdpr.consent_magic_integration_enabled) { 883 884 // retrieves actual PYS GDPR filters values which allow to avoid cache issues 885 $.get({ 886 url: options.ajaxUrl, 887 dataType: 'json', 888 data: { 889 action: 'pys_get_gdpr_filters_values' 890 }, 891 success: function (res) { 892 893 if (res.success) { 894 895 options.gdpr.all_disabled_by_api = res.data.all_disabled_by_api; 896 options.gdpr.facebook_disabled_by_api = res.data.facebook_disabled_by_api; 897 options.gdpr.analytics_disabled_by_api = res.data.analytics_disabled_by_api; 898 options.gdpr.google_ads_disabled_by_api = res.data.google_ads_disabled_by_api; 899 options.gdpr.pinterest_disabled_by_api = res.data.pinterest_disabled_by_api; 900 options.gdpr.bing_disabled_by_api = res.data.bing_disabled_by_api; 901 902 options.cookie.externalID_disabled_by_api = res.data.externalID_disabled_by_api; 903 options.cookie.disabled_all_cookie = res.data.disabled_all_cookie; 904 options.cookie.disabled_advanced_form_data_cookie = res.data.disabled_advanced_form_data_cookie; 905 options.cookie.disabled_landing_page_cookie = res.data.disabled_landing_page_cookie; 906 options.cookie.disabled_first_visit_cookie = res.data.disabled_first_visit_cookie; 907 options.cookie.disabled_trafficsource_cookie = res.data.disabled_trafficsource_cookie; 908 options.cookie.disabled_utmTerms_cookie = res.data.disabled_utmTerms_cookie; 909 options.cookie.disabled_utmId_cookie = res.data.disabled_utmId_cookie; 910 } 911 912 loadPixels(); 913 914 } 915 }); 916 917 } else { 918 loadPixels(); 919 } 920 921 }, 922 923 consentGiven: function (pixel) { 924 925 /** 926 * ConsentMagic 927 */ 928 if (options.gdpr.consent_magic_integration_enabled && typeof CS_Data !== "undefined" ) { 929 930 var test_prefix = CS_Data.test_prefix; 931 if ((typeof CS_Data.cs_google_consent_mode_enabled !== "undefined" && CS_Data.cs_google_consent_mode_enabled == 1) && ( pixel == 'analytics' || pixel == 'google_ads' ) ) { 932 return true; 933 } 934 935 if ( CS_Data.cs_cache_enabled == 1 ) { 936 var substring = "cs_enabled_cookie_term"; 937 var theCookies = document.cookie.split(';'); 938 939 for (var i = 1 ; i <= theCookies.length; i++) { 940 if ( theCookies[ i - 1 ].indexOf( substring ) !== -1 ) { 941 var categoryCookie = theCookies[ i - 1 ].replace( 'cs_enabled_cookie_term' + test_prefix + '_', '' ); 942 categoryCookie = Number( categoryCookie.replace( /\D+/g, "" ) ); 943 var cs_cookie_val = Cookies.get( 'cs_enabled_cookie_term' + test_prefix + '_' + categoryCookie ); 944 945 if ( categoryCookie === CS_Data.cs_script_cat.facebook && pixel == 'facebook' ) { 946 if ( cs_cookie_val == 'yes' ) { 947 return true; 948 } else { 949 return false;
950 } 951 } else if ( categoryCookie === CS_Data.cs_script_cat.bing && pixel == 'bing' ) { 952 if ( cs_cookie_val == 'yes' ) { 953 return true; 954 } else { 955 return false; 956 } 957 } else if ( categoryCookie === CS_Data.cs_script_cat.analytics && pixel == 'analytics' ) { 958 if ( cs_cookie_val == 'yes' ) { 959 return true; 960 } else { 961 return false; 962 } 963 } else if ( categoryCookie === CS_Data.cs_script_cat.gads && pixel == 'google_ads' ) { 964 if ( cs_cookie_val == 'yes' ) { 965 return true; 966 } else { 967 return false; 968 } 969 } else if ( categoryCookie === CS_Data.cs_script_cat.pinterest && pixel == 'pinterest' ) { 970 if ( cs_cookie_val == 'yes' ) { 971 return true; 972 } else { 973 return false; 974 } 975 } else if ( categoryCookie === CS_Data.cs_script_cat.tiktok && pixel == 'tiktok' ) { 976 if ( cs_cookie_val == 'yes' ) { 977 return true; 978 } else { 979 return false; 980 } 981 } 982 } 983 } 984 } else { 985 var cs_cookie = Cookies.get('cs_viewed_cookie_policy'+test_prefix); 986 if (typeof cs_cookie === 'undefined' || cs_cookie === 'yes') { 987 return true; 988 } 989 } 990 991 return false; 992 993 } 994 995 /** 996 * Real Cookie Banner 997 */ 998 if(options.gdpr.real_cookie_banner_integration_enabled) { 999 var consentApi = window.consentApi; 1000 if (consentApi) { 1001 switch (pixel) { 1002 case "analytics": 1003 return consentApi.consentSync("http", "_ga", "*").cookieOptIn; 1004 case "facebook": 1005 return consentApi.consentSync("http", "_fbp", "*").cookieOptIn; 1006 case "pinterest": 1007 return consentApi.consentSync("http", "_pinterest_sess", ".pinterest.com").cookieOptIn; 1008 default: 1009 return true; 1010 } 1011 } 1012 } 1013 1014 /** 1015 * Cookiebot 1016 */ 1017 if (options.gdpr.cookiebot_integration_enabled && typeof Cookiebot !== 'undefined') { 1018 1019 var cookiebot_consent_category = options.gdpr['cookiebot_' + pixel + '_consent_category']; 1020 1021 if (options.gdpr[pixel + '_prior_consent_enabled']) { 1022 if (Cookiebot.consented === false || Cookiebot.consent[cookiebot_consent_category]) { 1023 return true; 1024 } 1025 } else { 1026 if (Cookiebot.consent[cookiebot_consent_category]) { 1027 return true; 1028 } 1029 } 1030 1031 return false; 1032 1033 } 1034 1035 /** 1036 * Cookie Notice 1037 */ 1038 if (options.gdpr.cookie_notice_integration_enabled && typeof cnArgs !== 'undefined') { 1039 1040 var cn_cookie = Cookies.get(cnArgs.cookieName); 1041 1042 if (options.gdpr[pixel + '_prior_consent_enabled']) { 1043 if (typeof cn_cookie === 'undefined' || cn_cookie === 'true') { 1044 return true; 1045 } 1046 } else { 1047 if (cn_cookie === 'true') { 1048 return true; 1049 } 1050 } 1051 1052 return false;
1053 1054 } 1055 1056 /** 1057 * Cookie Law Info 1058 */ 1059 if (options.gdpr.cookie_law_info_integration_enabled) { 1060 1061 var cli_cookie = Cookies.get('viewed_cookie_policy'); 1062 1063 if (options.gdpr[pixel + '_prior_consent_enabled']) { 1064 if (typeof cli_cookie === 'undefined' || cli_cookie === 'yes') { 1065 return true; 1066 } 1067 } else { 1068 if (cli_cookie === 'yes') { 1069 return true; 1070 } 1071 } 1072 1073 return false; 1074 1075 } 1076 1077 return true; 1078 1079 }, 1080 1081 setupGdprCallbacks: function () { 1082 /** 1083 * ConsentMagic 1084 */ 1085 if (options.gdpr.consent_magic_integration_enabled && typeof CS_Data !== "undefined") { 1086 var test_prefix = CS_Data.test_prefix, 1087 cs_refresh_after_consent = false, 1088 substring = "cs_enabled_cookie_term"; 1089 1090 if (CS_Data.cs_refresh_after_consent == 1) { 1091 cs_refresh_after_consent = CS_Data.cs_refresh_after_consent; 1092 } 1093 1094 if (!cs_refresh_after_consent) { 1095 var theCookies = document.cookie.split(';'); 1096 for (var i = 1 ; i <= theCookies.length; i++) { 1097 if (theCookies[i-1].indexOf(substring) !== -1) { 1098 var categoryCookie = theCookies[i-1].replace('cs_enabled_cookie_term'+test_prefix+'_',''); 1099 categoryCookie = Number(categoryCookie.replace(/\D+/g,"")); 1100 var cs_cookie_val = Cookies.get('cs_enabled_cookie_term'+test_prefix+'_'+categoryCookie); 1101 if(cs_cookie_val == 'yes') { 1102 if (categoryCookie === CS_Data.cs_script_cat.facebook) { 1103 Facebook.loadPixel(); 1104 } 1105 1106 if (categoryCookie === CS_Data.cs_script_cat.bing) { 1107 Bing.loadPixel(); 1108 } 1109 1110 if (categoryCookie === CS_Data.cs_script_cat.analytics || (typeof CS_Data.cs_google_analytics_consent_mode !== "undefined" && CS_Data.cs_google_analytics_consent_mode == 1)) { 1111 1112 Analytics.loadPixel(); 1113 } 1114 1115 if (categoryCookie === CS_Data.cs_script_cat.pinterest) { 1116 Pinterest.loadPixel(); 1117 } 1118 } else { 1119 if (categoryCookie === CS_Data.cs_script_cat.facebook) { 1120 Facebook.disable(); 1121 } 1122 1123 if (categoryCookie === CS_Data.cs_script_cat.bing) { 1124 Bing.disable(); 1125 } 1126 if (categoryCookie === CS_Data.cs_script_cat.analytics && (typeof CS_Data.cs_google_analytics_consent_mode == "undefined" || CS_Data.cs_google_analytics_consent_mode == 0)) { 1127 Analytics.disable(); 1128 } 1129 1130 if (categoryCookie === CS_Data.cs_script_cat.pinterest) { 1131 Pinterest.disable(); 1132 } 1133 } 1134 if (Cookies.get('cs_enabled_advanced_matching') == 'yes') { 1135 Facebook.loadPixel(); 1136 } 1137 } 1138 } 1139 1140 $(document).on('click','.cs_action_btn',function(e) { 1141 e.preventDefault(); 1142 var elm = $(this), 1143 button_action = elm.attr('data-cs_action'); 1144 1145 if(button_action === 'allow_all') { 1146 Facebook.loadPixel(); 1147 Bing.loadPixel(); 1148 Analytics.loadPixel(); 1149 Pinterest.loadPixel(); 1150 } else if(button_action === 'disable_all') { 1151 Facebook.disable(); 1152 Bing.disable(); 1153 if(CS_Data.cs_google_analytics_consent_mode == 0 || typeof CS_Data.cs_google_analytics_consent_mode == "undefined") 1154 { 1155 Analytics.disable(); 1156 } 1157 Pinterest.disable(); 1158 } 1159 }); 1160 } 1161 } 1162 /** 1163 * Real Cookie Banner 1164 */ 1165 if(options.gdpr.real_cookie_banner_integration_enabled) { 1166 var consentApi = window.consentApi; 1167 if (consentApi) { 1168 consentApi.consent("http", "_ga", "*") 1169 .then(Analytics.loadPixel.bind(Analytics), Analytics.disable.bind(Analytics)); 1170 consentApi.consent("http", "_fbp", "*") 1171 .then(Facebook.loadPixel.bind(Facebook), Facebook.disable.bind(Facebook)); 1172 consentApi.consent("http", "_pinterest_sess", ".pinterest.com") 1173 .then(Pinterest.loadPixel.bind(Pinterest), Pinterest.disable.bind(Pinterest)); 1174 consentApi.consent("http", "_uetsid", "*") 1175 .then(Bing.loadPixel.bind(Bing), Bing.disable.bind(Bing)); 1176 } 1177 } 1178 /** 1179 * Cookiebot 1180 */ 1181 if (options.gdpr.cookiebot_integration_enabled && typeof Cookiebot !== 'undefined') { 1182 1183 window.addEventListener("CookiebotOnConsentReady", function() { 1184 if (Cookiebot.consent.marketing) { 1185 Facebook.loadPixel(); 1186 Bing.loadPixel(); 1187 Pinterest.loadPixel(); 1188 1189 } 1190 if (Cookiebot.consent.statistics) { 1191 Analytics.loadPixel(); 1192 } 1193 if (!Cookiebot.consent.marketing) { 1194 Facebook.disable(); 1195 Pinterest.disable(); 1196 Bing.disable() 1197 } 1198 if (!Cookiebot.consent.statistics) { 1199 Analytics.disable(); 1200 } 1201 }); 1202 1203 } 1204 1205 /** 1206 * Cookie Notice 1207 */ 1208 if (options.gdpr.cookie_notice_integration_enabled) { 1209 1210 $(document).onFirst('click', '.cn-set-cookie', function () { 1211 1212 if ($(this).data('cookie-set') === 'accept') { 1213 Facebook.loadPixel(); 1214 Analytics.loadPixel(); 1215 Pinterest.loadPixel(); 1216 Bing.loadPixel(); 1217 } else { 1218 Facebook.disable(); 1219 Analytics.disable(); 1220 Pinterest.disable(); 1221 Bing.disable(); 1222 } 1223 1224 }); 1225 1226 $(document).onFirst('click', '.cn-revoke-cookie', function () { 1227 Facebook.disable(); 1228 Analytics.disable(); 1229 Pinterest.disable(); 1230 Bing.disable(); 1231 }); 1232 1233 } 1234 1235 /** 1236 * Cookie Law Info 1237 */ 1238 if (options.gdpr.cookie_law_info_integration_enabled) { 1239 1240 $(document).onFirst('click', '#cookie_action_close_header', function () { 1241 Facebook.loadPixel(); 1242 Analytics.loadPixel(); 1243 Pinterest.loadPixel(); 1244 Bing.loadPixel(); 1245 }); 1246 1247 $(document).onFirst('click', '#cookie_action_close_header_reject', function () { 1248 Facebook.disable(); 1249 Analytics.disable(); 1250 Pinterest.disable(); 1251 Bing.disable(); 1252 }); 1253 1254 } 1255 1256 }, 1257 1258 /** 1259 * DOWNLOAD DOCS 1260 */ 1261 1262 getLinkExtension: function (link) { 1263 1264 // Remove anchor, query string and everything before last slash 1265 link = link.substring(0, (link.indexOf("#") === -1) ? link.length : link.indexOf("#")); 1266 link = link.substring(0, (link.indexOf("?") === -1) ? link.length : link.indexOf("?")); 1267 link = link.substring(link.lastIndexOf("/") + 1, link.length); 1268 1269 // If there's a period left in the URL, then there's a extension 1270 if (link.length > 0 && link.indexOf('.') !== -1) { 1271 link = link.substring(link.indexOf(".") + 1); // Remove everything but what's after the first period 1272 return link; 1273 } else { 1274 return ""; 1275 } 1276 }, 1277 1278 getLinkFilename: function (link) { 1279 1280 // Remove anchor, query string and everything before last slash 1281 link = link.substring(0, (link.indexOf("#") === -1) ? link.length : link.indexOf("#")); 1282 link = link.substring(0, (link.indexOf("?") === -1) ? link.length : link.indexOf("?")); 1283 link = link.substring(link.lastIndexOf("/") + 1, link.length); 1284 1285 // If there's a period left in the URL, then there's a extension 1286 if (link.length > 0 && link.indexOf('.') !== -1) { 1287 return link; 1288 } else { 1289 return ""; 1290 } 1291 }, 1292 /** 1293 * Enrich 1294 */ 1295 isCheckoutPage: function () { 1296 return $('body').hasClass('woocommerce-checkout') || document.querySelector('.woocommerce-checkout') || 1297 $('body').hasClass('edd-checkout'); 1298 }, 1299 addCheckoutFields : function() { 1300 var utm = ""; 1301 var utms = getUTMs() 1302 1303 $.each(utmTerms, function (index, name) { 1304 if(index > 0) { 1305 utm+="|"; 1306 } 1307 utm+=name+":"+utms[name]; 1308 }); 1309 var utmIdList = ""; 1310 var utmsIds = getUTMId() 1311 $.each(utmId, function (index, name) { 1312 if(index > 0) { 1313 utmIdList+="|"; 1314 } 1315 utmIdList+=name+":"+utmsIds[name]; 1316 }); 1317 var utmIdListLast = ""; 1318 var utmsIdsLast = getUTMId(true) 1319 $.each(utmId, function (index, name) { 1320 if(index > 0) { 1321 utmIdListLast+="|"; 1322 } 1323 utmIdListLast+=name+":"+utmsIdsLast[name]; 1324 }); 1325 1326 1327 var utmLast = ""; 1328 var utmsLast = getUTMs(true) 1329 $.each(utmTerms, function (index, name) { 1330 if(index > 0) { 1331 utmLast+="|"; 1332 }
1333 utmLast+=name+":"+utmsLast[name]; 1334 }); 1335 1336 var dateTime = getDateTime(); 1337 var landing = getLandingPageValue(); 1338 var lastLanding = getLandingPageValue(); 1339 var trafic = getTrafficSourceValue(); 1340 var lastTrafic = getTrafficSourceValue(); 1341 1342 var $form = null; 1343 if($('body').hasClass('woocommerce-checkout')) { 1344 $form = $("form.woocommerce-checkout"); 1345 } else { 1346 $form = $("#edd_purchase_form"); 1347 } 1348 var inputs = {'pys_utm':utm, 1349 'pys_utm_id':utmIdList, 1350 'pys_browser_time':dateTime.join("|"), 1351 'pys_landing':landing, 1352 'pys_source':trafic, 1353 'pys_order_type': $(".wcf-optin-form").length > 0 ? "wcf-optin" : "normal", 1354 1355 'last_pys_landing':lastLanding, 1356 'last_pys_source':lastTrafic, 1357 'last_pys_utm':utmLast, 1358 'last_pys_utm_id':utmIdListLast, 1359 } 1360 1361 Object.keys(inputs).forEach(function(key,index) { 1362 $form.append("<input type='hidden' name='"+key+"' value='"+inputs[key]+"' /> "); 1363 }); 1364 1365 1366 }, 1367 getAdvancedFormData: function () { 1368 let dataStr = Cookies.get("pys_advanced_form_data"); 1369 if(dataStr === undefined) { 1370 return {'first_name':"",'last_name':"",'email':"",'phone':""}; 1371 } else { 1372 return JSON.parse(dataStr); 1373 } 1374 } 1375 }; 1376 1377 }(options); 1378 1379 var Facebook = function (options) { 1380 1381 1382 var defaultEventTypes = [ 1383 'PageView', 1384 'ViewContent', 1385 'Search', 1386 'AddToCart', 1387 'AddToWishlist', 1388 'InitiateCheckout', 1389 'AddPaymentInfo', 1390 'Purchase', 1391 'Lead', 1392 1393 'Subscribe', 1394 'CustomizeProduct', 1395 'FindLocation', 1396 'StartTrial', 1397 'SubmitApplication', 1398 'Schedule', 1399 'Contact', 1400 'Donate' 1401 ]; 1402 1403 var initialized = false; 1404 1405 // fire server side event gdpr plugin installed 1406 var isApiDisabled = options.gdpr.all_disabled_by_api || 1407 options.gdpr.facebook_disabled_by_api || 1408 options.gdpr.cookiebot_integration_enabled || 1409 options.gdpr.consent_magic_integration_enabled || 1410 options.gdpr.cookie_notice_integration_enabled || 1411 options.gdpr.cookie_law_info_integration_enabled; 1412 1413 /** 1414 * 1415 * @param allData 1416 * @param params 1417 * @returns {string | null} 1418 */ 1419 function sendFbServerEvent(allData,name,params) { 1420 let eventId = null; 1421 if(options.facebook.serverApiEnabled) { 1422 1423 if(allData.e_id === "woo_remove_from_cart") {// server event will sended from hook 1424 Facebook.updateEventId(allData.name); 1425 allData.eventID = Facebook.getEventId(allData.name); 1426 } else { 1427 if( options.ajaxForServerEvent 1428 || isApiDisabled 1429 || allData.delay > 0 1430 || allData.type !== "static") 1431 { // send event from server if they was bloc by gdpr or need send with delay 1432 allData.eventID = pys_generate_token(36); 1433 var json = { 1434 action: 'pys_api_event', 1435 pixel: 'facebook', 1436 event: name, 1437 data:params, 1438 ids:options.facebook.pixelIds, 1439 eventID:allData.eventID, 1440 url:window.location.href, 1441 ajax_event:options.ajax_event 1442 }; 1443 if(allData.hasOwnProperty('woo_order')) { 1444 json['woo_order'] = allData.woo_order; 1445 } 1446 if(allData.hasOwnProperty('edd_order')) { 1447 json['edd_order'] = allData.edd_order; 1448 } 1449 1450 if(name == 'PageView') { 1451 let expires = parseInt(options.cookie_duration); 1452 var currentTimeInSeconds=Date.now(); 1453 var randomNum = Math.floor(1000000000 + Math.random() * 9000000000); 1454 timeoutDelay = 0; 1455 if(allData.delay > 0) 1456 { 1457 timeoutDelay = allData.delay; 1458 } 1459 if(!Cookies.get('_fbp')) 1460 { 1461 timeoutDelay = 100; 1462 } 1463 if(getUrlParameter('fbclid') && !Cookies.get('_fbc')) 1464 { 1465 timeoutDelay = 100; 1466 } 1467 setTimeout(function(){ 1468 if(!Cookies.get('_fbp')) 1469 { 1470 Cookies.set('_fbp','fb.1.'+currentTimeInSeconds+'.'+randomNum, { expires: expires }) 1471 } 1472 if(getUrlParameter('fbclid') && !Cookies.get('_fbc')) 1473 { 1474 Cookies.set('_fbc', 'fb.1.'+currentTimeInSeconds+'.'+getUrlParameter('fbclid'), { expires: expires }) 1475 } 1476 jQuery.ajax( { 1477 type: 'POST', 1478 url: options.ajaxUrl, 1479 data: json, 1480 headers: { 1481 'Cache-Control': 'no-cache' 1482 }, 1483 success: function(){}, 1484 }); 1485 },timeoutDelay) 1486 } 1487 else 1488 { 1489 jQuery.ajax( { 1490 type: 'POST', 1491 url: options.ajaxUrl, 1492 data: json, 1493 headers: { 1494 'Cache-Control': 'no-cache' 1495 }, 1496 success: function(){}, 1497 }); 1498 } 1499 } 1500 } 1501 eventId = allData.eventID 1502 } 1503 1504 return eventId; 1505 } 1506 1507 function fireEvent(name, allData) { 1508 1509 if(typeof window.pys_event_data_filter === "function" && window.pys_disable_event_filter(name,'facebook')) { 1510 return; 1511 } 1512 1513 var actionType = defaultEventTypes.includes(name) ? 'track' : 'trackCustom'; 1514 var data = allData.params; 1515 var params = {}; 1516 var arg = {}; 1517 Utils.copyProperties(data, params); 1518 1519 let eventId = sendFbServerEvent(allData,name,params) 1520 1521 1522 if("hCR" === name) { 1523 return; 1524 } 1525 1526 if (options.debug) { 1527 console.log('[Facebook] ' + name, params,"eventID",eventId); 1528 } 1529 1530 if(eventId != null) { 1531 arg.eventID = eventId; 1532 } 1533 1534 fbq(actionType, name, params,arg); 1535 } 1536 1537 /** 1538 * Public API 1539 */ 1540 return { 1541 tag: function() { 1542 return "facebook"; 1543 }, 1544 isEnabled: function () { 1545 return options.hasOwnProperty('facebook'); 1546 }, 1547 1548 disable: function () { 1549 initialized = false;
1550 }, 1551 advancedMatching: function () { 1552 if(options.facebook.advancedMatchingEnabled) { 1553 let advancedMatchingForm = Utils.getAdvancedFormData(); 1554 let advancedMatching = {}; 1555 if(Object.keys(options.facebook.advancedMatching).length > 0) { 1556 advancedMatching = options.facebook.advancedMatching; 1557 } 1558 1559 if(!advancedMatching.hasOwnProperty("em") 1560 && advancedMatchingForm.hasOwnProperty("email") && advancedMatchingForm["email"].length > 0) { 1561 advancedMatching["em"] = advancedMatchingForm["email"]; 1562 } 1563 if(!advancedMatching.hasOwnProperty("ph") 1564 && advancedMatchingForm.hasOwnProperty("phone") && advancedMatchingForm["phone"].length > 0) { 1565 advancedMatching["ph"] = advancedMatchingForm["phone"]; 1566 } 1567 if(!advancedMatching.hasOwnProperty("fn") 1568 && advancedMatchingForm.hasOwnProperty("first_name") && advancedMatchingForm["first_name"].length > 0) { 1569 advancedMatching["fn"] = advancedMatchingForm["first_name"]; 1570 } 1571 if(!advancedMatching.hasOwnProperty("ln") 1572 && advancedMatchingForm.hasOwnProperty("last_name") && advancedMatchingForm["last_name"].length > 0) { 1573 advancedMatching["ln"] = advancedMatchingForm["last_name"]; 1574 } 1575 if(!advancedMatching.hasOwnProperty("external_id")){ 1576 if (Cookies.get('pbid') || (options.hasOwnProperty('pbid') && options.pbid)) { 1577 advancedMatching["external_id"] = Cookies.get('pbid') ? Cookies.get('pbid') : options.pbid; 1578 } 1579 } 1580 else if(advancedMatching.external_id != Cookies.get('pbid')) 1581 { 1582 advancedMatching["external_id"] = Cookies.get('pbid') ? Cookies.get('pbid') : advancedMatching.external_id; 1583 } 1584 if(Object.keys(advancedMatching).length > 0) { 1585 return advancedMatching; 1586 } 1587 } 1588 return false 1589 }, 1590 /** 1591 * Load pixel's JS 1592 */ 1593 loadPixel: function () { 1594 1595 if (initialized || !this.isEnabled() || !Utils.consentGiven('facebook')) { 1596 return; 1597 } 1598 1599 !function (f, b, e, v, n, t, s) { 1600 if (f.fbq) return; 1601 n = f.fbq = function () { 1602 n.callMethod ? 1603 n.callMethod.apply(n, arguments) : n.queue.push(arguments) 1604 }; 1605 if (!f._fbq) f._fbq = n; 1606 n.push = n; 1607 n.loaded = !0; 1608 n.version = '2.0'; 1609 n.agent = 'dvpixelyoursite'; 1610 n.queue = []; 1611 t = b.createElement(e); 1612 t.async = !0; 1613 t.src = v; 1614 s = b.getElementsByTagName(e)[0]; 1615 s.parentNode.insertBefore(t, s) 1616 }(window, 1617 document, 'script', 'https://connect.facebook.net/en_US/fbevents.js'); 1618 1619 // initialize pixel 1620 options.facebook.pixelIds.forEach(function (pixelId) { 1621 if (options.facebook.removeMetadata) { 1622 fbq('set', 'autoConfig', false, pixelId); 1623 } 1624 let advancedMatching = Facebook.advancedMatching(); 1625 if (options.gdpr.consent_magic_integration_enabled && typeof CS_Data !== "undefined") { 1626 if(!advancedMatching) { 1627 fbq('init', pixelId); 1628 } else { 1629 var test_prefix = CS_Data.test_prefix; 1630 var cs_advanced_matching = Cookies.get('cs_enabled_advanced_matching'+test_prefix); 1631 if (jQuery('#cs_enabled_advanced_matching'+test_prefix).length > 0) { 1632 if (cs_advanced_matching == 'yes') { 1633 fbq('init', pixelId, advancedMatching); 1634 } else { 1635 fbq('init', pixelId); 1636 } 1637 } else { 1638 fbq('init', pixelId, advancedMatching); 1639 } 1640 } 1641 } else { 1642 if(!advancedMatching) { 1643 fbq('init', pixelId); 1644 } else { 1645 fbq('init', pixelId, advancedMatching); 1646 } 1647 } 1648 }); 1649 1650 initialized = true; 1651 1652 Utils.fireStaticEvents('facebook'); 1653 1654 }, 1655 1656 fireEvent: function (name, data) { 1657 1658 if (!initialized || !this.isEnabled()) { 1659 return false;
1660 } 1661 1662 data.delay = data.delay || 0; 1663 data.params = data.params || {}; 1664 1665 if (data.delay === 0) { 1666 1667 fireEvent(name, data); 1668 1669 } else { 1670 1671 setTimeout(function (name, params) { 1672 fireEvent(name, params); 1673 }, data.delay * 1000, name, data); 1674 1675 } 1676 1677 return true; 1678 1679 }, 1680 1681 onCommentEvent: function (event) { 1682 this.fireEvent(event.name, event); 1683 }, 1684 1685 onDownloadEvent: function (event) { 1686 this.fireEvent(event.name, event); 1687 }, 1688 1689 onFormEvent: function (event) { 1690 1691 this.fireEvent(event.name, event); 1692 1693 }, 1694 1695 onWooAddToCartOnButtonEvent: function (product_id) { 1696 1697 if(!options.dynamicEvents.woo_add_to_cart_on_button_click.hasOwnProperty(this.tag())) 1698 return; 1699 var event = options.dynamicEvents.woo_add_to_cart_on_button_click[this.tag()]; 1700 1701 if (window.pysWooProductData.hasOwnProperty(product_id)) { 1702 if (window.pysWooProductData[product_id].hasOwnProperty('facebook')) { 1703 event = Utils.copyProperties(event, {}) 1704 Utils.copyProperties(window.pysWooProductData[product_id]['facebook'].params, event.params) 1705 this.fireEvent(event.name, event); 1706 } 1707 } 1708 }, 1709 1710 onWooAddToCartOnSingleEvent: function (product_id, qty, product_type, $form) { 1711 1712 window.pysWooProductData = window.pysWooProductData || []; 1713 if(!options.dynamicEvents.woo_add_to_cart_on_button_click.hasOwnProperty(this.tag())) 1714 return; 1715 var event = Utils.clone(options.dynamicEvents.woo_add_to_cart_on_button_click[this.tag()]); 1716 1717 if (product_type === Utils.PRODUCT_VARIABLE && !options.facebook.wooVariableAsSimple) { 1718 product_id = parseInt($form.find('input[name="variation_id"]').val()); 1719 } 1720 1721 if (window.pysWooProductData.hasOwnProperty(product_id)) { 1722 if (window.pysWooProductData[product_id].hasOwnProperty('facebook')) { 1723 1724 1725 Utils.copyProperties(window.pysWooProductData[product_id]['facebook'].params, event.params); 1726 1727 var groupValue = 0; 1728 if(product_type === Utils.PRODUCT_GROUPED ) { 1729 $form.find(".woocommerce-grouped-product-list .qty").each(function(index){ 1730 var childId = $(this).attr('name').replaceAll("quantity[","").replaceAll("]",""); 1731 var quantity = parseInt($(this).val()); 1732 if(isNaN(quantity)) { 1733 quantity = 0; 1734 } 1735 var childItem = window.pysWooProductData[product_id]['facebook'].grouped[childId]; 1736 1737 if(quantity == 0) { 1738 event.params.content_ids.forEach(function(el,index,array) { 1739 if(el == childItem.content_id) { 1740 array.splice(index, 1); 1741 } 1742 }); 1743 } 1744 1745 if(event.params.hasOwnProperty('contents')) { 1746 event.params.contents.forEach(function(el,index,array) { 1747 if(el.id == childItem.content_id) { 1748 if(quantity > 0){ 1749 el.quantity = quantity; 1750 } else { 1751 array.splice(index, 1); 1752 } 1753 } 1754 }); 1755 } 1756 1757 1758 groupValue += childItem.price * quantity; 1759 }); 1760 if(groupValue == 0) return;
1760 // skip if no items selected 1761 } 1762 1763 // maybe customize value option 1764 if (options.woo.addToCartOnButtonValueEnabled && options.woo.addToCartOnButtonValueOption !== 'global') { 1765 1766 if(product_type === Utils.PRODUCT_GROUPED) { 1767 event.params.value = groupValue; 1768 } else if(product_type === Utils.PRODUCT_BUNDLE) { 1769 var data = $(".bundle_form .bundle_data").data("bundle_form_data"); 1770 var items_sum = getBundlePriceOnSingleProduct(data); 1771 event.params.value = (parseInt(data.base_price) + items_sum )* qty; 1772 } else { 1773 event.params.value = event.params.value * qty; 1774 } 1775 } 1776 1777 // only when non Facebook for WooCommerce logic used 1778 if (event.params.hasOwnProperty('contents') && product_type !== Utils.PRODUCT_GROUPED) { 1779 event.params.contents[0].quantity = qty; 1780 } 1781 1782 this.fireEvent(event.name, event); 1783 1784 } 1785 } 1786 }, 1787 1788 onWooRemoveFromCartEvent: function (event) { 1789 this.fireEvent(event.name, event); 1790 }, 1791 1792 onEddAddToCartOnButtonEvent: function (download_id, price_index, qty) { 1793 1794 if(!options.dynamicEvents.edd_add_to_cart_on_button_click.hasOwnProperty(this.tag())) 1795 return; 1796 var event = Utils.clone(options.dynamicEvents.edd_add_to_cart_on_button_click[this.tag()]); 1797 1798 if (window.pysEddProductData.hasOwnProperty(download_id)) { 1799 1800 var index; 1801 1802 if (price_index) { 1803 index = download_id + '_' + price_index; 1804 } else { 1805 index = download_id; 1806 } 1807 1808 if (window.pysEddProductData[download_id].hasOwnProperty(index)) { 1809 if (window.pysEddProductData[download_id][index].hasOwnProperty('facebook')) { 1810 1811 1812 Utils.copyProperties(window.pysEddProductData[download_id][index]['facebook']["params"], event.params) 1813 1814 // maybe customize value option 1815 if (options.edd.addToCartOnButtonValueEnabled && options.edd.addToCartOnButtonValueOption !== 'global') { 1816 event.params.value = event.params.value * qty; 1817 } 1818 1819 // update contents qty param 1820 var contents = event.params.contents; 1821 contents[0].quantity = qty; 1822 event.params.contents = contents; 1823 1824 this.fireEvent(event.name,event); 1825 1826 } 1827 } 1828 1829 } 1830 1831 }, 1832 1833 onEddRemoveFromCartEvent: function (event) { 1834 this.fireEvent(event.name, event); 1835 }, 1836 onPageScroll: function (event) { 1837 this.fireEvent(event.name, event); 1838 }, 1839 onTime: function (event) { 1840 this.fireEvent(event.name, event); 1841 }, 1842 initEventIdCookies: function (key) { 1843 var ids = {}; 1844 ids[key] = pys_generate_token(36) 1845 Cookies.set('pys_fb_event_id', JSON.stringify(ids)); 1846 }, 1847 updateEventId:function(key) { 1848 var cooData = Cookies.get("pys_fb_event_id") 1849 if(cooData === undefined) { 1850 this.initEventIdCookies(key); 1851 } else { 1852 var data = JSON.parse(cooData); 1853 data[key] = pys_generate_token(36); 1854 Cookies.set('pys_fb_event_id', JSON.stringify(data) ); 1855 } 1856 }, 1857 1858 getEventId:function (key) { 1859 var data = Cookies.get("pys_fb_event_id"); 1860 if(data === undefined) {
1861 this.initEventIdCookies(key); 1862 data = Cookies.get("pys_fb_event_id"); 1863 } 1864 return JSON.parse(data)[key]; 1865 }, 1866 }; 1867 1868 }(options); 1869 1870 var Analytics = function (options) { 1871 1872 var initialized = false; 1873 1874 /** 1875 * Fires event 1876 * 1877 * @link: https://developers.google.com/analytics/devguides/collection/gtagjs/sending-data 1878 * @link: https://developers.google.com/analytics/devguides/collection/gtagjs/events 1879 * @link: https://developers.google.com/gtagjs/reference/event 1880 * @link: https://developers.google.com/gtagjs/reference/parameter 1881 * 1882 * @link: https://developers.google.com/analytics/devguides/collection/gtagjs/custom-dims-mets 1883 * 1884 * @param name 1885 * @param data 1886 */ 1887 function fireEvent(name, data) { 1888 1889 if(typeof window.pys_event_data_filter === "function" && window.pys_disable_event_filter(name,'ga')) { 1890 return; 1891 } 1892 1893 var eventParams = Utils.copyProperties(data, {}); 1894 1895 var _fireEvent = function (tracking_id,name,params) { 1896 1897 params['send_to'] = tracking_id; 1898 1899 if (options.debug) { 1900 console.log('[Google Analytics #' + tracking_id + '] ' + name, params); 1901 } 1902 1903 gtag('event', name, params); 1904 1905 }; 1906 1907 options.ga.trackingIds.forEach(function (tracking_id) { 1908 var copyParams = Utils.copyProperties(eventParams, {}); // copy params because mapParamsTov4 can modify it 1909 var params = mapParamsTov4(tracking_id,name,copyParams) 1910 _fireEvent(tracking_id,name,params); 1911 }); 1912 1913 } 1914 function mapParamsTov4(tag,name,param) { 1915 //GA4 automatically collects a number of parameters for all events 1916 delete param.landing_page; 1917 // end 1918 if(isv4(tag)) { 1919 delete param.traffic_source; 1920 delete param.event_category; 1921 delete param.event_label; 1922 delete param.ecomm_prodid; 1923 delete param.ecomm_pagetype; 1924 delete param.ecomm_totalvalue; 1925 if(name === 'search') { 1926 param['search'] = param.search_term; 1927 delete param.search_term; 1928 delete param.dynx_itemid; 1929 delete param.dynx_pagetype; 1930 delete param.dynx_totalvalue; 1931 } 1932 } 1933 return param; 1934 } 1935 1936 function isv4(tag) { 1937 return tag.indexOf('G') === 0; 1938 } 1939 1940 /** 1941 * Public API 1942 */ 1943 return { 1944 tag: function() { 1945 return "ga"; 1946 }, 1947 isEnabled: function () { 1948 return options.hasOwnProperty('ga'); 1949 }, 1950 1951 disable: function () { 1952 initialized = false; 1953 }, 1954 1955 loadPixel: function () { 1956 1957 if (initialized || !this.isEnabled() || !Utils.consentGiven('analytics')) { 1958 return; 1959 } 1960 1961 Utils.loadGoogleTag(options.ga.trackingIds[0]); 1962 var config = {}; 1963 // Cross-Domain tracking 1964 if (options.ga.crossDomainEnabled) { 1965 config.linker = { 1966 accept_incoming: options.ga.crossDomainAcceptIncoming, 1967 domains: options.ga.crossDomainDomains 1968 }; 1969 } 1970 1971 1972 1973 // configure tracking ids 1974 options.ga.trackingIds.forEach(function (trackingId,index) { 1975 var obj = options.ga.isDebugEnabled; 1976 var searchValue = "index_"+index; 1977 config.debug_mode = false;
1978 1979 for (var key in obj) { 1980 if (obj.hasOwnProperty(key) && obj[key] === searchValue) { 1981 config.debug_mode = true; 1982 break; 1983 } 1984 } 1985 if(!config.debug_mode) 1986 { 1987 delete config.debug_mode; 1988 } 1989 if(isv4(trackingId)) { 1990 if(options.ga.disableAdvertisingFeatures) { 1991 config.allow_google_signals = false 1992 } 1993 if(options.ga.disableAdvertisingPersonalization) { 1994 config.allow_ad_personalization_signals = false 1995 } 1996 } 1997 if (options.gdpr.cookiebot_integration_enabled && typeof Cookiebot !== 'undefined') { 1998 1999 var cookiebot_consent_category = options.gdpr['cookiebot_analytics_consent_category']; 2000 if (options.gdpr['analytics_prior_consent_enabled']) { 2001 if (Cookiebot.consented === true && Cookiebot.consent[cookiebot_consent_category]) { 2002 gtag('config', trackingId, config); 2003 } 2004 } else { 2005 if (Cookiebot.consent[cookiebot_consent_category]) { 2006 gtag('config', trackingId, config); 2007 } 2008 } 2009 2010 } 2011 else 2012 { 2013 gtag('config', trackingId, config); 2014 } 2015 }); 2016 2017 initialized = true; 2018 2019 Utils.fireStaticEvents('ga'); 2020 2021 }, 2022 2023 fireEvent: function (name, data) { 2024 2025 if (!initialized || !this.isEnabled()) { 2026 return false; 2027 } 2028 2029 data.delay = data.delay || 0; 2030 data.params = data.params || {}; 2031 2032 if (data.delay === 0) { 2033 2034 fireEvent(name, data.params); 2035 2036 } else { 2037 2038 setTimeout(function (name, params) { 2039 fireEvent(name, params); 2040 }, data.delay * 1000, name, data.params); 2041 2042 } 2043 2044 return true; 2045 2046 }, 2047 2048 onCommentEvent: function (event) { 2049 this.fireEvent(event.name, event); 2050 }, 2051 2052 onDownloadEvent: function (event) { 2053 this.fireEvent(event.name, event); 2054 }, 2055 2056 onFormEvent: function (event) { 2057 this.fireEvent(event.name, event); 2058 }, 2059 2060 onWooAddToCartOnButtonEvent: function (product_id) { 2061 2062 if(!options.dynamicEvents.woo_add_to_cart_on_button_click.hasOwnProperty(this.tag())) 2063 return; 2064 var event = Utils.clone(options.dynamicEvents.woo_add_to_cart_on_button_click[this.tag()]); 2065 2066 if (window.pysWooProductData.hasOwnProperty(product_id)) { 2067 if (window.pysWooProductData[product_id].hasOwnProperty('ga')) { 2068 Utils.copyProperties(window.pysWooProductData[product_id]['ga'].params, event.params) 2069 this.fireEvent(event.name, event); 2070 } 2071 } 2072 2073 2074 }, 2075 2076 onWooAddToCartOnSingleEvent: function (product_id, qty, product_type, $form) { 2077 2078 window.pysWooProductData = window.pysWooProductData || []; 2079 2080 if(!options.dynamicEvents.woo_add_to_cart_on_button_click.hasOwnProperty(this.tag())) 2081 return; 2082 var event = Utils.clone(options.dynamicEvents.woo_add_to_cart_on_button_click[this.tag()]); 2083 2084 if (product_type === Utils.PRODUCT_VARIABLE && !options.ga.wooVariableAsSimple) { 2085 product_id = parseInt($form.find('input[name="variation_id"]').val()); 2086 } 2087 2088 if (window.pysWooProductData.hasOwnProperty(product_id)) { 2089 if (window.pysWooProductData[product_id].hasOwnProperty('ga')) { 2090 2091 Utils.copyProperties(window.pysWooProductData[product_id]['ga'].params, event.params); 2092 if(product_type === Utils.PRODUCT_GROUPED ) { 2093 var groupValue = 0; 2094 $form.find(".woocommerce-grouped-product-list .qty").each(function(index){ 2095 var childId = $(this).attr('name').replaceAll("quantity[","").replaceAll("]",""); 2096 var quantity = parseInt($(this).val()); 2097 if(isNaN(quantity)) { 2098 quantity = 0; 2099 } 2100 var childItem = window.pysWooProductData[product_id]['ga'].grouped[childId]; 2101 // update quantity
2102 event.params.items.forEach(function(el,index,array) { 2103 if(el.id == childItem.content_id) { 2104 if(quantity > 0){ 2105 el.quantity = quantity; 2106 } else { 2107 array.splice(index, 1); 2108 } 2109 } 2110 }); 2111 groupValue += childItem.price * quantity; 2112 }); 2113 if(options.woo.addToCartOnButtonValueEnabled && 2114 options.woo.addToCartOnButtonValueOption !== 'global' && 2115 event.params.hasOwnProperty('ecomm_totalvalue')) { 2116 event.params.ecomm_totalvalue = groupValue; 2117 } 2118 2119 if(groupValue == 0) return; // skip if no items selected 2120 } else { 2121 // update items qty param 2122 event.params.items[0].quantity = qty; 2123 } 2124 2125 // maybe customize value option 2126 if (options.woo.addToCartOnButtonValueEnabled && 2127 options.woo.addToCartOnButtonValueOption !== 'global' && 2128 product_type !== Utils.PRODUCT_GROUPED) 2129 { 2130 if(event.params.hasOwnProperty('ecomm_totalvalue')) { 2131 event.params.ecomm_totalvalue = event.params.items[0].price * qty; 2132 } 2133 2134 } 2135 2136 2137 this.fireEvent(event.name, event); 2138 } 2139 } 2140 2141 }, 2142 2143 onWooRemoveFromCartEvent: function (event) { 2144 2145 this.fireEvent(event.name, event); 2146 2147 }, 2148 2149 onEddAddToCartOnButtonEvent: function (download_id, price_index, qty) { 2150 2151 if(!options.dynamicEvents.edd_add_to_cart_on_button_click.hasOwnProperty(this.tag())) 2152 return; 2153 var event = Utils.clone(options.dynamicEvents.edd_add_to_cart_on_button_click[this.tag()]); 2154 2155 2156 if (window.pysEddProductData.hasOwnProperty(download_id)) { 2157 2158 var index; 2159 2160 if (price_index) { 2161 index = download_id + '_' + price_index; 2162 } else { 2163 index = download_id; 2164 } 2165 2166 if (window.pysEddProductData[download_id].hasOwnProperty(index)) { 2167 if (window.pysEddProductData[download_id][index].hasOwnProperty('ga')) { 2168 2169 Utils.copyProperties(window.pysEddProductData[download_id][index]['ga'].params, event.params); 2170 2171 // update items qty param 2172 event.params.items[0].quantity = qty; 2173 2174 this.fireEvent(event.name,event); 2175 2176 } 2177 } 2178 2179 } 2180 2181 }, 2182 2183 onEddRemoveFromCartEvent: function (event) { 2184 this.fireEvent(event.name, event); 2185 }, 2186 onPageScroll: function (event) { 2187 this.fireEvent(event.name, event); 2188 }, 2189 onTime: function (event) { 2190 this.fireEvent(event.name, event); 2191 }, 2192 2193 }; 2194 2195 }(options); 2196 2197 window.pys = window.pys || {}; 2198 window.pys.Facebook = Facebook; 2199 window.pys.Analytics = Analytics; 2200 window.pys.Utils = Utils; 2201 2202 2203 2204 2205 $(document).ready(function () { 2206 2207 if($("#pys_late_event").length > 0) { 2208 var events = JSON.parse($("#pys_late_event").attr("dir")); 2209 for(var key in events) { 2210 var event = {}; 2211 event[events[key].e_id] = [events[key]]; 2212 if(options.staticEvents.hasOwnProperty(key)) { 2213 Object.assign(options.staticEvents[key], event); 2214 } else { 2215 options.staticEvents[key] = event; 2216 } 2217 2218 } 2219 } 2220 2221 var Pinterest = Utils.setupPinterestObject(); 2222 var Bing = Utils.setupBingObject(); 2223 2224 if(options.hasOwnProperty('cookie')) 2225 { 2226 if(options.cookie.externalID_disabled_by_api || options.cookie.disabled_all_cookie) 2227 { 2228 Cookies.remove('pbid') 2229 } 2230 if(options.cookie.disabled_advanced_form_data_cookie || options.cookie.disabled_all_cookie) 2231 { 2232 Cookies.remove('pys_advanced_form_data') 2233 } 2234 if(options.cookie.disabled_landing_page_cookie || options.cookie.disabled_all_cookie) 2235 { 2236 Cookies.remove('pys_landing_page') 2237 Cookies.remove('last_pys_landing_page') 2238 } 2239 if(options.cookie.disabled_trafficsource_cookie || options.cookie.disabled_all_cookie) 2240 { 2241 Cookies.remove('pysTrafficSource') 2242 Cookies.remove('last_pysTrafficSource') 2243 } 2244 if(options.cookie.disabled_first_visit_cookie || options.cookie.disabled_all_cookie) 2245 { 2246 Cookies.remove('pys_first_visit') 2247 2248 } 2249 if(options.cookie.disabled_utmTerms_cookie || options.cookie.disabled_all_cookie) 2250 { 2251 $.each(Utils.utmTerms, function (index, name) { 2252 Cookies.remove('pys_' + name) 2253 }); 2254 $.each(Utils.utmTerms, function (index, name) { 2255 Cookies.remove('last_pys_' + name) 2256 }); 2257 } 2258 if(options.cookie.disabled_utmId_cookie || options.cookie.disabled_all_cookie) 2259 { 2260 $.each(Utils.utmId,function(index,name) { 2261 Cookies.remove('pys_' + name) 2262 }) 2263 $.each(Utils.utmId,function(index,name) { 2264 Cookies.remove('last_pys_' + name) 2265 }); 2266 } 2267 } 2268 2269 2270 Utils.manageCookies(); 2271 Utils.setupGdprCallbacks(); 2272 // page scroll event 2273 if ( options.dynamicEvents.hasOwnProperty("automatic_event_scroll") 2274 ) { 2275 2276 var singlePageScroll = function () { 2277 2278 2279 var docHeight = $(document).height() - $(window).height(); 2280 var isFired = false;
2281 2282 if (options.dynamicEvents.hasOwnProperty("automatic_event_scroll")) { 2283 var pixels = Object.keys(options.dynamicEvents.automatic_event_scroll); 2284 for(var i = 0;i<pixels.length;i++) { 2285 var event = Utils.clone(options.dynamicEvents.automatic_event_scroll[pixels[i]]); 2286 var scroll = Math.round(docHeight * event.scroll_percent / 100)// convert % to absolute positions 2287 2288 if(scroll < $(window).scrollTop()) { 2289 Utils.copyProperties(Utils.getRequestParams(), event.params); 2290 getPixelBySlag(pixels[i]).onPageScroll(event); 2291 isFired = true 2292 } 2293 } 2294 } 2295 if(isFired) { 2296 $(document).off("scroll",singlePageScroll); 2297 } 2298 } 2299 $(document).on("scroll",singlePageScroll); 2300 } 2301 2302 2303 if (options.dynamicEvents.hasOwnProperty("automatic_event_time_on_page")) { 2304 var pixels = Object.keys(options.dynamicEvents.automatic_event_time_on_page); 2305 var time = options.dynamicEvents.automatic_event_time_on_page[pixels[0]].time_on_page; // the same for all pixel 2306 setTimeout(function(){ 2307 for(var i = 0;i<pixels.length;i++) { 2308 var event = Utils.clone(options.dynamicEvents.automatic_event_time_on_page[pixels[i]]);
2309 Utils.copyProperties(Utils.getRequestParams(), event.params); 2310 getPixelBySlag(pixels[i]).onTime(event); 2311 } 2312 },time*1000); 2313 } 2314 2315 // setup Click Event 2316 if (options.dynamicEvents.hasOwnProperty("automatic_event_download")) { 2317 2318 $(document).onFirst('click', 'a, button, input[type="button"], input[type="submit"]', function (e) { 2319 2320 var $elem = $(this); 2321 2322 // Download 2323 if(options.dynamicEvents.hasOwnProperty("automatic_event_download") 2324 ) { 2325 var isFired = false; 2326 if ($elem.is('a')) { 2327 var href = $elem.attr('href'); 2328 if (typeof href !== "string") { 2329 return; 2330 } 2331 href = href.trim(); 2332 var extension = Utils.getLinkExtension(href); 2333 var track_download = false; 2334 2335 if (extension.length > 0) { 2336 2337 if(options.dynamicEvents.hasOwnProperty("automatic_event_download") ) { 2338 var pixels = Object.keys(options.dynamicEvents.automatic_event_download); 2339 for (var i = 0; i < pixels.length; i++) { 2340 var event = Utils.clone(options.dynamicEvents.automatic_event_download[pixels[i]]); 2341 var extensions = event.extensions; 2342 if (extensions.includes(extension)) { 2343 2344 if(pixels[i] == "tiktok") { 2345 getPixelBySlag(pixels[i]).fireEvent(tikEvent.name, event); 2346 } else { 2347 if (options.enable_remove_download_url_param) { 2348 href = href.split('?')[0]; 2349 } 2350 event.params.download_url = href; 2351 event.params.download_type = extension; 2352 event.params.download_name = Utils.getLinkFilename(href); 2353 getPixelBySlag(pixels[i]).onDownloadEvent(event); 2354 } 2355 2356 isFired = true; 2357 } 2358 } 2359 } 2360 } 2361 } 2362 if(isFired) { // prevent duplicate events on the same element 2363 return; 2364 } 2365 } 2366 }); 2367 } 2368 2369 2370 // setup Dynamic events 2371 $.each(options.triggerEventTypes, function (triggerType, events) { 2372 2373 $.each(events, function (eventId, triggers) { 2374 2375 switch (triggerType) { 2376 case 'url_click': 2377 //@see: Utils.setupURLClickEvents() 2378 break; 2379 2380 case 'css_click': 2381 Utils.setupCSSClickEvents(eventId, triggers); 2382 break; 2383 2384 case 'css_mouseover': 2385 Utils.setupMouseOverClickEvents(eventId, triggers); 2386 break; 2387 2388 case 'scroll_pos': 2389 Utils.setupScrollPosEvents(eventId, triggers); 2390 break; 2391 case 'comment': 2392 Utils.setupCommentEvents(eventId, triggers); 2393 break; 2394 } 2395 2396 }); 2397 2398 }); 2399 // setup WooCommerce events 2400 if (options.woo.enabled) { 2401 2402 // WooCommerce AddToCart 2403 if (options.dynamicEvents.hasOwnProperty("woo_add_to_cart_on_button_click") 2404 && options.woo.hasOwnProperty("addToCartCatchMethod") 2405 && options.woo.addToCartCatchMethod === "add_cart_js" 2406 ) { 2407 2408 // Loop, any kind of "simple" product, except external 2409 $('.add_to_cart_button:not(.product_type_variable,.product_type_bundle,.single_add_to_cart_button)').on("click",function (e) { 2410 2411 var product_id = $(this).data('product_id'); 2412 2413 if (typeof product_id !== 'undefined') { 2414 Facebook.onWooAddToCartOnButtonEvent(product_id); 2415 Analytics.onWooAddToCartOnButtonEvent(product_id); 2416 Pinterest.onWooAddToCartOnButtonEvent(product_id); 2417 Bing.onWooAddToCartOnButtonEvent(product_id); 2418 } 2419 2420 }); 2421 2422 // Single Product 2423 // tap try to https://stackoverflow.com/questions/30990967/on-tap-click-event-firing-twice-how-to-avoid-it 2424 // $(document) not work 2425 $('body').onFirst('click','button.single_add_to_cart_button,.single_add_to_cart_button',function (e) { 2426 2427 var $button = $(this); 2428 2429 if ($button.hasClass('disabled')) { 2430 return; 2431 } 2432 2433 var $form = $button.closest('form'); 2434 2435 var product_type = Utils.PRODUCT_SIMPLE; 2436 2437 if ($form.length === 0) { 2438 return ; 2439 } else if ($form.hasClass('variations_form')) { 2440 product_type = Utils.PRODUCT_VARIABLE; 2441 } else if($form.hasClass('bundle_form')) { 2442 product_type = Utils.PRODUCT_BUNDLE; 2443 } else if($form.hasClass('grouped_form')) { 2444 product_type = Utils.PRODUCT_GROUPED; 2445 } 2446 2447 2448 2449 2450 var product_id; 2451 var qty; 2452 2453 if (product_type === Utils.PRODUCT_GROUPED) { 2454 qty = 1; 2455 product_id = parseInt($form.find('*[name="add-to-cart"]').val()); 2456 } else if (product_type === Utils.PRODUCT_VARIABLE) { 2457 product_id = parseInt($form.find('*[name="add-to-cart"]').val()); 2458 var qtyTag = $form.find('input[name="quantity"]'); 2459 if(qtyTag.length <= 0) { 2460 qtyTag = $form.find('select[name="quantity"]'); 2461 } 2462 qty = parseInt(qtyTag.val()); 2463 } else { 2464 product_id = parseInt($form.find('*[name="add-to-cart"]').val()); 2465 var qtyTag = $form.find('input[name="quantity"]'); 2466 if(qtyTag.length <= 0) { 2467 qtyTag = $form.find('select[name="quantity"]'); 2468 } 2469 qty = parseInt(qtyTag.val()); 2470 } 2471 2472 Facebook.onWooAddToCartOnSingleEvent(product_id, qty, product_type, $form); 2473 Analytics.onWooAddToCartOnSingleEvent(product_id, qty, product_type, $form); 2474 2475 Pinterest.onWooAddToCartOnSingleEvent(product_id, qty, product_type, false, $form); 2476 Bing.onWooAddToCartOnSingleEvent(product_id, qty, product_type, false, $form); 2477 2478 }); 2479 2480 } 2481 2482 // WooCommerce RemoveFromCart 2483 if (options.dynamicEvents.hasOwnProperty("woo_remove_from_cart")) { 2484 2485 $('body').on('click', options.woo.removeFromCartSelector, function (e) { 2486 2487 var $a = $(e.currentTarget), 2488 href = $a.attr('href'); 2489 2490 // extract cart item hash from remove button URL 2491 var regex = new RegExp("[\\?&]remove_item=([^&#]*)"), 2492 results = regex.exec(href); 2493 2494 if (results !== null) { 2495 2496 var item_hash = results[1]; 2497 2498 if (options.dynamicEvents["woo_remove_from_cart"].hasOwnProperty(item_hash)) { 2499 var events = options.dynamicEvents["woo_remove_from_cart"][item_hash]; 2500 Utils.fireEventForAllPixel("onWooRemoveFromCartEvent",events) 2501 } 2502 2503 } 2504 2505 }); 2506 } 2507 } 2508 2509 // setup EDD events 2510 if (options.edd.enabled) { 2511 2512 // EDD AddToCart 2513 if (options.dynamicEvents.hasOwnProperty("edd_add_to_cart_on_button_click")) { 2514 2515 $('form.edd_download_purchase_form .edd-add-to-cart').on("click",function (e) { 2516 2517 var $button = $(this); 2518 var $form = $button.closest('form'); 2519 var variable_price = $button.data('variablePrice'); // yes/no 2520 var price_mode = $button.data('priceMode'); // single/multi 2521 var ids = []; 2522 var quantities = []; 2523 var qty; 2524 var id; 2525 2526 if (variable_price === 'yes' && price_mode === 'multi') { 2527 2528 id = $form.find('input[name="download_id"]').val(); 2529 2530 // get selected variants 2531 $.each($form.find('input[name="edd_options[price_id][]"]:checked'), function (i, el) { 2532 ids.push(id + '_' + $(el).val()); 2533 }); 2534 2535 // get qty for selected variants 2536 $.each(ids, function (i, variant_id) { 2537 2538 var variant_index = variant_id.split('_', 2); 2539 qty = $form.find('input[name="edd_download_quantity_' + variant_index[1] + '"]').val(); 2540 2541 if (typeof qty !== 'undefined') { 2542 quantities.push(qty); 2543 } else { 2544 quantities.push(1); 2545 } 2546 2547 }); 2548 2549 } else if (variable_price === 'yes' && price_mode === 'single') { 2550 2551 id = $form.find('input[name="download_id"]').val(); 2552 ids.push(id + '_' + $form.find('input[name="edd_options[price_id][]"]:checked').val()); 2553 2554 qty = $form.find('input[name="edd_download_quantity"]').val(); 2555 2556 if (typeof qty !== 'undefined') { 2557 quantities.push(qty); 2558 } else { 2559 quantities.push(1); 2560 } 2561 2562 } else { 2563 2564 ids.push($button.data('downloadId')); 2565 2566 qty = $form.find('input[name="edd_download_quantity"]').val(); 2567 2568 if (typeof qty !== 'undefined') { 2569 quantities.push(qty); 2570 } else { 2571 quantities.push(1); 2572 } 2573 } 2574 2575 // fire event for each download/variant 2576 $.each(ids, function (i, download_id) { 2577 2578 var q = parseInt(quantities[i]); 2579 var variant_index = download_id.toString().split('_', 2); 2580 var price_index; 2581 2582 if (variant_index.length === 2) { 2583 download_id = variant_index[0]; 2584 price_index = variant_index[1]; 2585 } 2586 2587 Facebook.onEddAddToCartOnButtonEvent(download_id, price_index, q); 2588 Analytics.onEddAddToCartOnButtonEvent(download_id, price_index, q); 2589 Pinterest.onEddAddToCartOnButtonEvent(download_id, price_index, q); 2590 Bing.onEddAddToCartOnButtonEvent(download_id, price_index, q); 2591 2592 }); 2593 2594 }); 2595 2596 } 2597 2598 2599 // EDD RemoveFromCart 2600 if (options.dynamicEvents.hasOwnProperty("edd_remove_from_cart") ) { 2601 2602 $('form#edd_checkout_cart_form .edd_cart_remove_item_btn').on("click",function (e) { 2603 2604 var href = $(this).attr('href'); 2605 if(href) { 2606 var key = href.substring(href.indexOf('=') + 1).charAt(0); 2607 if (options.dynamicEvents.edd_remove_from_cart.hasOwnProperty(key)) { 2608 var events = options.dynamicEvents.edd_remove_from_cart[key]; 2609 Utils.fireEventForAllPixel("onEddRemoveFromCartEvent",events) 2610 } 2611 } 2612 }); 2613 2614 } 2615 2616 } 2617 2618 // setup Comment Event 2619 if (options.dynamicEvents.hasOwnProperty("automatic_event_c
2619omment") 2620 ) { 2621 2622 $('form.comment-form').on("submit",function () { 2623 if (options.dynamicEvents.hasOwnProperty("automatic_event_comment")) { 2624 var pixels = Object.keys(options.dynamicEvents.automatic_event_comment); 2625 for (var i = 0; i < pixels.length; i++) { 2626 var event = Utils.clone(options.dynamicEvents.automatic_event_comment[pixels[i]]); 2627 Utils.copyProperties(Utils.getRequestParams(), event.params); 2628 getPixelBySlag(pixels[i]).onCommentEvent(event); 2629 } 2630 } 2631 }); 2632 2633 } 2634 2635 2636 // setup Form Event 2637 if ( options.dynamicEvents.hasOwnProperty("automatic_event_form")) { 2638 2639 $(document).onFirst('submit', 'form', function (e) { 2640 2641 var $form = $(this); 2642 2643 // exclude WP forms 2644 if ($form.hasClass('comment-form') || $form.hasClass('search-form') || $form.attr('id') === 'adminbarsearch') { 2645 return; 2646 } 2647 2648 // exclude Woo forms 2649 if ($form.hasClass('woocommerce-product-search') || $form.hasClass('cart') || $form.hasClass('woocommerce-cart-form') || 2650 $form.hasClass('woocommerce-shipping-calculator') || $form.hasClass('checkout') || $form.hasClass('checkout_coupon')) { 2651 return; 2652 } 2653 2654 // exclude EDD forms 2655 if ($form.hasClass('edd_form') || $form.hasClass('edd_download_purchase_form')) { 2656 return; 2657 } 2658// exclude CF7 forms 2659 if ($form.hasClass('wpcf7-form')) { 2660 return; 2661 } 2662 // exclude Forminator forms 2663 if ($form.hasClass('forminator-custom-form') || $form.hasClass('forminator_ajax')) { 2664 return; 2665 } 2666 // exclude WPforms forms 2667 if ($form.hasClass('wpforms-form') || $form.hasClass('wpforms-ajax-form')) { 2668 return; 2669 } 2670 // exclude Formidable forms 2671 /*if ($form.hasClass('frm-show-form')) { 2672 return; 2673 }*/ 2674 // exclude Ninja Forms forms 2675 if ($form.parent().hasClass('nf-form-layout')) { 2676 return; 2677 } 2678 // exclude Fluent forms 2679 if ($form.hasClass('frm-fluent-form')) { 2680 return; 2681 } 2682 if(!options.enable_success_send_form) { 2683 var params = { 2684 form_id: $form.attr('id'), 2685 form_class: $form.attr('class'), 2686 text: $form.find('[type="submit"]').is('input') ? 2687 $form.find('[type="submit"]').val() : $form.find('[type="submit"]').text() 2688 }; 2689 2690 if (options.dynamicEvents.hasOwnProperty("automatic_event_form")) { 2691 var pixels = Object.keys(options.dynamicEvents.automatic_event_form); 2692 for (var i = 0; i < pixels.length; i++) { 2693 var event = Utils.clone(options.dynamicEvents.automatic_event_form[pixels[i]]); 2694 2695 if (pixels[i] === "tiktok") { 2696 getPixelBySlag(pixels[i]).fireEvent(event.name, event); 2697 } else { 2698 Utils.copyProperties(params, event.params,) 2699 Utils.copyProperties(Utils.getRequestParam
2699s(), event.params); 2700 getPixelBySlag(pixels[i]).onFormEvent(event); 2701 } 2702 } 2703 } 2704 } 2705 }); 2706 2707 document.addEventListener( 'wpcf7mailsent', function( event ) { 2708 var form_id = event.detail.contactFormId; 2709 sendFormAction($(event.target), form_id); 2710 }, false ); 2711 //Forminator 2712 $(document).on( 'forminator:form:submit:success', function( event ){ 2713 var form_id = $(event.target).find('input[name="form_id"]').val(); 2714 sendFormAction($(event.target), form_id); 2715 }); 2716 2717 //WPForm 2718 $('form.wpforms-form').on('wpformsAjaxSubmitSuccess', (event) => { 2719 var form_id = $(event.target).attr('data-formid'); 2720 sendFormAction($(event.target), form_id); 2721 }) 2722 $(document).on( 'frmFormComplete', function( event, form, response ) { 2723 const form_id = $(form).find('input[name="form_id"]').val(); 2724 sendFormAction($(event.target), form_id); 2725 }); 2726 // Ninja Forms 2727 $(document).onFirst('nfFormSubmitResponse', function (event, data) { 2728 const form_id = data.response.data.form_id; 2729 sendFormAction($(event.target), form_id); 2730 }); 2731 2732 var fluentForms = $('form.frm-fluent-form'); 2733 fluentForms.each(function() { 2734 var $form = $(this); 2735 $form.on('fluentform_submission_success', function(event) { 2736 var $formItem = $(this); 2737 var form_id = $formItem.attr('data-form_id'); 2738 sendFormAction($(event.target), form_id); 2739 }); 2740 }); 2741 2742 } 2743 2744 2745 // load pixel APIs 2746 Utils.loadPixels(); 2747 // setup Enrich content 2748 if(Utils.isCheckoutPage()) { 2749 Utils.addCheckoutFields(); 2750 } 2751 }); 2752 2753 var sendFormAction = function (form_target, formId){ 2754 var params = { 2755 form_id: formId, 2756 text: form_target.find('[type="submit"]').is('input') ? form_target.find('[type="submit"]').val() : 2757 form_target.find('.forminator-button-submit').text() != '' ? form_target.find('.forminator-button-submit').text() : 2758 form_target.find('[type="submit"]').text() 2759 }; 2760 2761 if (options.dynamicEvents.hasOwnProperty("automatic_event_form")) { 2762 var pixels = Object.keys(options.dynamicEvents.automatic_event_form); 2763 for (var i = 0; i < pixels.length; i++) { 2764 var event = options.dynamicEvents.automatic_event_form[pixels[i]]; 2765 if (pixels[i] === "tiktok") { 2766 getPixelBySlag(pixels[i]).fireEvent(event.name, event); 2767 } else { 2768 Utils.copyProperties(params, event.params)
2769 Utils.copyProperties(Utils.getRequestParams(), event.params); 2770 getPixelBySlag(pixels[i]).onFormEvent(event); 2771 } 2772 } 2773 } 2774 } 2775 2776}(jQuery, pysOptions); 2777if (pysOptions.ajaxForServerEvent && !Cookies.get('pbid') && !(pysOptions.cookie.disabled_all_cookie || pysOptions.cookie.externalID_disabled_by_api)) { 2778 jQuery.ajax({ 2779 url: pysOptions.ajaxUrl, 2780 dataType: 'json', 2781 data: { 2782 action: 'pys_get_pbid' 2783 }, 2784 success: function (res) { 2785 if (res.data && res.data.pbid != false && pysOptions.send_external_id) { 2786 var expires = parseInt(pysOptions.external_id_expire || 180); 2787 Cookies.set('pbid', res.data.pbid, { expires: expires, path: '/' }); 2788 if(pysOptions.hasOwnProperty('facebook')) { 2789 pysOptions.facebook.advancedMatching.external_id = res.data.pbid; 2790 } 2791 } 2792 } 2793 }); 2794} 2795function pys_generate_token(length){ 2796 //edit the token allowed characters 2797 var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".split(""); 2798 var b = []; 2799 for (var i=0; i<length; i++) { 2800 var j = (Math.random() * (a.length-1)).toFixed(0); 2801 b[i] = a[j]; 2802 } 2803 return b.join(""); 2804} 2805 2806function getBundlePriceOnSingleProduct(data) { 2807 var items_sum = 0; 2808 jQuery(".bundle_form .bundled_product").each(function(index){ 2809 var id = jQuery(this).find(".cart").data("bundled_item_id"); 2810 var item_price = data.prices[id]; 2811 var item_quantity = jQuery(this).find(".bundled_qty").val(); 2812 if(!jQuery(this).hasClass("bundled_item_optional") || 2813 jQuery(this).find(".bundled_product_optional_checkbox input").prop('checked')) { 2814 items_sum += item_price*item_quantity; 2815 } 2816 }); 2817 return items_sum; 2818} 2819 2820function getPixelBySlag(slug) { 2821 switch (slug) { 2822 case "facebook": return window.pys.Facebook; 2823 case "ga": return window.pys.Analytics; 2824 case "bing": return window.pys.Bing; 2825 case "pinterest": return window.pys.Pinterest; 2826 } 2827} 2828var getUrlParameter = function getUrlParameter(sParam) { 2829 var sPageURL = window.location.search.substring(1), 2830 sURLVariables = sPageURL.split('&'), 2831 sParameterName, 2832 i; 2833 2834 for (i = 0; i < sURLVariables.length; i++) { 2835 sParameterName = sURLVariables[i].split('='); 2836 2837 if (sParameterName[0] === sParam) { 2838 return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]); 2839 } 2840 } 2841 return false; 2842};
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.