1jQuery(document).ready(function( $ ) { 2 3 var utm_terms = [ 'utm_source', 'utm_media', 'utm_campaign', 'utm_term', 'utm_content' ]; 4 5 // load FB pixel 6 !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? 7 n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; 8 n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; 9 t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, 10 document,'script','https://connect.facebook.net/en_US/fbevents.js'); 11 12 manageCookies(); 13 14 regularEvents(); 15 dynamicEvents(); 16 customCodeEvents(); 17 18 // temporary disabled due CORS error (@see #55 issue) 19 // @since 4.0.5 20 //timeOnPageEvent(); 21 22 // AddToCart button 23 $(".ajax_add_to_cart").click(function(e){ 24 25 var attr = $(this).attr('data-pys-event-id'); 26 27 if( typeof attr == 'undefined' || typeof pys_woo_ajax_events == 'undefined' ) { 28 return; 29 } 30 31 evaluateEventByID( attr.toString(), pys_woo_ajax_events ); 32 33 }); 34 35 // Affiliate Product 36 $(".product_type_external").click(function(e){ 37 38 var attr = $(this).attr('data-pys-event-id'); 39 40 if( typeof attr == 'undefined' || typeof pys_woo_ajax_events == 'undefined' ) { 41 return; 42 } 43 44 evaluateEventByID( attr.toString(), pys_woo_ajax_events ); 45 46 }); 47 48 // PayPal Event 49 $(document).on('submit click', '#place_order', function(e){ 50 51 var method = $('input[name="payment_method"]:checked').val(); 52 if( method == false || method != 'paypal' ) { 53 return; 54 } 55 56 //@todo: wrap to try/catch block 57 var eventData = JSON.parse( $(this).attr('data-pys-code') ); 58 59 var params = addTrafficSourceParams( eventData.type, eventData.params ); 60 fbq( eventData.type, eventData.name, params ); 61 62 }); 63 64 /** 65 * Process Init, General, Search, Standard (except custom code), WooCommerce (except AJAX AddToCart, Affiliate and 66 * PayPal events. In case if delay param is present - event will be fired after desired timeout. 67 */ 68 function regularEvents() { 69 70 if( typeof pys_events == 'undefined' ) { 71 return; 72 } 73 74 for( var i = 0; i < pys_events.length; i++ ) { 75 76 var eventData = pys_events[i]; 77 var params = addTrafficSourceParams( eventData.type, eventData.params ); 78 79 if( eventData.hasOwnProperty('delay') == false || eventData.delay == 0 ) { 80 81 fbq( eventData.type, eventData.name, params ); 82 83 } else { 84 85 setTimeout( function( type, name, params ) { 86 fbq( type, name, params ); 87 }, eventData.delay * 1000, eventData.type, eventData.name, params ); 88 89 } 90 91 92 } 93 94 } 95 96 function timeOnPageEvent() { 97 98 if( typeof pys_timeOnPage == 'undefined' ) { 99 return; 100 } 101 102 var counter = 0; 103 setInterval(function(){ 104 counter += 1; 105 }, 1000 ); 106 107 // setup event listener 108 window.onbeforeunload = function(e) { 109 110 pys_timeOnPage.time = counter; 111 112 var params = addTrafficSourceParams( 'track', pys_timeOnPage ); 113 fbq( 'trackCustom', 'TimeOnPage', params ); 114 115 }; 116 117 } 118 119 /** 120 * Process only custom code Standard events. 121 */ 122 function customCodeEvents() { 123 124 if( typeof pys_customEvents == 'undefined' ) { 125 return; 126 } 127 128 $.each( pys_customEvents, function( index, code ) { 129 eval( code ); 130 } ); 131 132 } 133 134 /** 135 * Attach Dynamic events event handlers: 136 * - on click: for URL event type (url will has .pys-dynamic-event class); 137 * - on click or on mouseenter: for CSS event type; 138 */ 139 function dynamicEvents() { 140 141 // set dynamic event listeners on URL clicks 142 $(document).on('click', '.pys-dynamic-event', function (e) { 143 144 var attr = $(this).attr('data-pys-event-id'); 145 146 if( typeof attr == 'undefined' ) { 147 return; 148 } 149 150 evaluateEventByID( attr.toString(), pys_dynamic_events ); 151 152 }); 153 154 // on Click by css selector 155 if( typeof pys_css_selectors != 'undefined' ) { 156 157 // setup event listeners for each selector 158 $.each( pys_css_selectors, function( eventID, selector ) { 159 160 $(document).on('click', selector, function () { 161 evaluateEventByID( eventID, pys_dynamic_events ); 162 }); 163 164 }); 165 166 } 167 168 // on page Scroll by desired position 169 if( typeof pys_scroll_positions != 'undefined' ) { 170 171 var height = $(document).height() - $(window).height(); 172 173 // collect al thresholds values 174 var thresholds = {}; 175 $.each( pys_scroll_positions, function( eventID, pos ) { 176 177 // convert % to pixels 178 pos = parseInt( pos ); 179 pos = height * pos / 100; 180 pos = Math.round( pos ); 181 182 // convert to correct field name (numbers can't be used) 183 pos = '_' + pos.toString(); 184 185 // add new position 186 if( typeof thresholds[pos] == 'undefined' ) { 187 thresholds[pos] = []; 188 } 189 190 // array is used because many events can be attached to same scroll position 191 // so each position has an array with events ids 192 thresholds[pos].push( eventID ); 193 194 }); 195 196 $(document).scroll(function () { 197 198 var scroll = $(window).scrollTop(); 199 200 $.each( thresholds, function( pos, events ) { 201 202 var trigger = parseInt( pos.substring(1) ); // integer threshold value 203 204 // position is not reached 205 if( scroll <= trigger ) { 206 return true; 207 } 208 209 // fire all events in current threshold 210 $.map( events, function( eventID, index ) { 211 212 if( events[ index ] != null ) { 213 214 evaluateEventByID( eventID, pys_dynamic_events ); 215 events[ index ] = null; // do not fire event next time 216 217 } 218 219 }); 220 221 222 }); 223 224 }); 225 226 } 227 228 // on Mouse Over by css selectors 229 if( typeof pys_mouse_over_selectors != 'undefined' ) { 230 231 // setup event listeners for each selector 232 $.each( pys_mouse_over_selectors, function( eventID, selector ) { 233 234 $(document).on('mouseover', selector, function () { 235 236 evaluateEventByID( eventID, pys_dynamic_events ); 237 238 // event should be fired only once 239 delete pys_dynamic_events[ eventID ]; 240 241 }); 242 243 }); 244 245 } 246 247 } 248 249 /** 250 * Fire event with {eventID} from =events= events list. In case of event data have =custom= property, code will be 251 * evaluated without regular Facebook pixel call. 252 */ 253 function evaluateEventByID( eventID, events ) { 254 255 if( typeof events == 'undefined' || events.length == 0 ) { 256 return; 257 } 258 259 // try to find required event 260 if( events.hasOwnProperty( eventID ) == false ) { 261 return; 262 } 263 264 var eventData = events[ eventID ]; 265 266 if( eventData.hasOwnProperty( 'custom' ) ) { 267 268 eval( eventData.custom ); 269 270 } else { 271 272 var params = addTrafficSourceParams( eventData.type, eventData.params ); 273 fbq( eventData.type, eventData.name, params ); 274 275 } 276 277 } 278 279 function getTrafficSource() { 280 281 var referrer = document.referrer.toString(); 282 283 var direct = referrer.length == 0; 284 var internal = direct ? false : referrer.indexOf( pys_options.site_url ) === 0; 285 var external = !(direct || internal); 286 var cookie = typeof Cookies.get('pys_traffic_source') == 'undefined' ? false : Cookies.get('pys_traffic_source'); 287 288 if( external == false ) { 289 290 return cookie ? cookie : 'direct'; 291 292 } else { 293 294 return cookie && cookie == referrer ? cookie : referrer; 295 296 } 297 298 } 299 300 /** 301 * Return UTM terms from request query variables or from cookies. 302 */ 303 function getUTMs() { 304 305 var terms = {}; 306 var queryVars = getQueryVars(); 307 308 $.each( utm_terms, function( index, name ){ 309 310 if( Cookies.get( 'pys_'+name ) ) { 311 terms[ name ] = Cookies.get( 'pys_'+name ); 312 } else if( queryVars.hasOwnProperty( name ) ) { 313 terms[ name ] = queryVars[name]; 314 } 315 316 }); 317 318 return terms; 319 320 } 321 322 function manageCookies() { 323 324 var source = getTrafficSource(); 325 326 if( source != 'direct' ) { 327 Cookies.set( 'pys_traffic_source', source ); 328 } else { 329 Cookies.remove( 'pys_traffic_source' ); 330 } 331 332 var queryVars = getQueryVars(); 333 334 $.each( utm_terms, function( index, name ){ 335 336 if( Cookies.get( 'pys_'+name ) == undefined && queryVars.hasOwnProperty( name ) ) { 337 Cookies.set( 'pys_'+name, queryVars[name] ); 338 } 339 340 }); 341 342 } 343 344 /** 345 * Add =traffic_source= and =utm_***= params to existing event params for all events except =init= event and 346 * in case of option is enabled. Returns modified =params= object. 347 */ 348 function addTrafficSourceParams( type, params ) { 349 350 if( pys_options.traffic_source_enabled == false || type == 'init' ) { 351 return params; 352 } 353 354 params.traffic_source = getTrafficSource(); 355 356 $.each( getUTMs(), function( name, value ) { 357 358 if( $.inArray( name, utm_terms ) >= 0 ) { 359 params[ name ] = value; 360 } 361 362 }); 363 364 return params; 365 366 } 367 368 /** 369 * Return query variables object with where property name is query variable and property value is query variable value. 370 */ 371 function getQueryVars() { 372 373 var result = {}, tmp = []; 374 375 window.location.search 376 .substr(1) 377 .split("&") 378 .forEach(function (item) { 379 380 tmp = item.split( '=' ); 381 382 if( tmp.length > 1 ) { 383 result[ tmp[0] ] = tmp[1]; 384 } 385 386 }); 387 388 return result; 389 390 } 391 392});
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.