1/** 2 * Track banner views on the front end 3 * 4 * 5 */ 6if (!Array.prototype.indexOf) { 7 Array.prototype.indexOf = function (elem, startFrom) { 8 var startFrom = startFrom || 0; 9 if (startFrom > this.length) return -1; 10 11 for (var i = 0; i < this.length; i++) { 12 if (this[i] == elem && startFrom <= i) { 13 return i; 14 } else if (this[i] == elem && startFrom > i) { 15 return -1; 16 } 17 } 18 return -1; 19 } 20} 21"use strict"; 22jQuery(document).ready(function($) { 23 24 /** 25 * Hold all banner ids that have already been viewed on current page 26 * @type {Array} 27 */ 28 var viewedBanners = []; 29 30 // Track all banners on page load 31 $('.adv-banner').each(function(i, banner) { 32 trackView(banner); 33 }); 34 35 // Track view for dynamically reloaded banners 36 $('body').on('reloaded', '.adv-banner', function() { 37 trackView(this); 38 }); 39 40 /** 41 * Track single banner view 42 * @return {void} 43 */ 44 function trackView(banner) { 45 46 /** 47 * Hold banner attributes that will be passed to server 48 * @type {Object} 49 */ 50 var attributes = {}; 51 52 /** 53 * Hold attributes that should not be passed to server 54 * @type {Array} 55 */ 56 var excludeAttributes = ['style', 'class']; 57 58 // Check if we need to track current banner 59 if ( viewedBanners.indexOf( $(banner).data('id') ) > -1 ) 60 return; 61 viewedBanners.push( $(banner).data('id') ); 62 63 // Skip unnecessary attributes 64 for (var i = 0; i < banner.attributes.length; i++) { 65 if ( excludeAttributes.indexOf( banner.attributes[i].name ) == -1 ) 66 attributes[ banner.attributes[i].name ] = banner.attributes[i].value; 67 }; 68 69 incrementBannerView(attributes); 70 } 71 72 /** 73 * Tell server to incerement view of a single banner 74 * @param {array} attributes Array of banner attributes 75 * @return {void} 76 */ 77 function incrementBannerView(attributes) { 78 attributes['action'] = 'banner_increment_view'; 79 $.ajax({ 80 url: ajaxurl, 81 data: attributes, 82 }); 83 } 84 85}); // End of jQuery shell function
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.