From 4faa60b5a5df17fd374959c1d17e956b515efd00 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Wed, 19 Apr 2017 15:03:13 +0300 Subject: [PATCH 01/22] trackLink functionality --- src/ddManager.js | 5 ++++ src/functions/eventListener.js | 35 ++++++++++++++++++++++ src/functions/isMeta.js | 22 ++++++++++++++ src/functions/preventDefault.js | 4 +++ src/trackers/index.js | 1 + src/trackers/trackLink.js | 51 +++++++++++++++++++++++++++++++++ 6 files changed, 118 insertions(+) create mode 100644 src/functions/eventListener.js create mode 100644 src/functions/isMeta.js create mode 100644 src/functions/preventDefault.js create mode 100644 src/trackers/index.js create mode 100644 src/trackers/trackLink.js diff --git a/src/ddManager.js b/src/ddManager.js index 0ad20ae..2a59db7 100644 --- a/src/ddManager.js +++ b/src/ddManager.js @@ -19,6 +19,7 @@ import { VIEWED_PAGE, mapEvent } from './events'; import { validateIntegrationEvent, trackValidationErrors } from './EventValidator'; import { enableErrorTracking } from './ErrorTracker'; import { warn, error as errorLog } from './functions/safeConsole'; +import { trackLink } from './trackers'; let ddManager; @@ -382,6 +383,10 @@ ddManager = { return _eventManager; }, + trackLink: (el, handler) => { + trackLink(el, handler); + }, + reset: () => { if (_ddStorage) { _ddStorage.clear(); diff --git a/src/functions/eventListener.js b/src/functions/eventListener.js new file mode 100644 index 0000000..2b57112 --- /dev/null +++ b/src/functions/eventListener.js @@ -0,0 +1,35 @@ +const bindName = window.addEventListener ? 'addEventListener' : 'attachEvent'; +const unbindName = window.removeEventListener ? 'removeEventListener' : 'detachEvent'; +const prefix = bindName !== 'addEventListener' ? 'on' : ''; + +/** + * Bind `el` event `type` to `fn`. + * + * @param {Element} el + * @param {String} type + * @param {Function} fn + * @param {Boolean} capture + * @return {Function} + * @api public + */ + +export function bind(el, type, fn, capture) { + el[bindName](prefix + type, fn, capture || false); + return fn; +} + +/** + * Unbind `el` event `type`'s callback `fn`. + * + * @param {Element} el + * @param {String} type + * @param {Function} fn + * @param {Boolean} capture + * @return {Function} + * @api public + */ + +export function undbin(el, type, fn, capture) { + el[unbindName](prefix + type, fn, capture || false); + return fn; +} diff --git a/src/functions/isMeta.js b/src/functions/isMeta.js new file mode 100644 index 0000000..fb6eae6 --- /dev/null +++ b/src/functions/isMeta.js @@ -0,0 +1,22 @@ +/** +* Checks whether a DOM click event should open a link in a new tab. +*/ + +export default function isMeta(e) { + if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) { + return true; + } + + // Logic that handles checks for the middle mouse button, based + // on [jQuery](https://github.com/jquery/jquery/blob/master/src/event.js#L466). + const which = e.which; + const button = e.button; + if (!which && button !== undefined) { + // eslint-disable-next-line no-bitwise, no-extra-parens + return (!button & 1) && (!button & 2) && (button & 4); + } else if (which === 2) { + return true; + } + + return false; +} diff --git a/src/functions/preventDefault.js b/src/functions/preventDefault.js new file mode 100644 index 0000000..87370f4 --- /dev/null +++ b/src/functions/preventDefault.js @@ -0,0 +1,4 @@ +export default function preventDefault(e) { + e = e || window.event; + return e.preventDefault ? e.preventDefault() : e.returnValue = false; +} diff --git a/src/trackers/index.js b/src/trackers/index.js new file mode 100644 index 0000000..5fb3edd --- /dev/null +++ b/src/trackers/index.js @@ -0,0 +1 @@ +export { default as trackLink } from './trackLink.js'; diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js new file mode 100644 index 0000000..8835a7e --- /dev/null +++ b/src/trackers/trackLink.js @@ -0,0 +1,51 @@ +import { bind } from './../functions/eventListener'; +import isMeta from './../functions/isMeta'; +import preventDefault from './../functions/preventDefault'; + +function isElement(el) { + return (el && el.nodeType === 1); +} + +function onClick(el, handler) { + return (e) => { + const href = el.getAttribute('href') + || el.getAttributeNS('http://www.w3.org/1999/xlink', 'href') + || el.getAttribute('xlink:href'); + + try { + handler(); + } catch (error) { + // TODO + } + + if (href && el.target !== '_blank' && !isMeta(e)) { + preventDefault(e); + setTimeout(() => { + window.location.href = href; + }, 500); + } + }; +} + +export default function trackLink(links, handler) { + if (!links) return this; + // always arrays, handles jquery + if (isElement(links)) { + links = [links]; + } else if (links.toArray) { + links = links.toArray(); + } + + if (typeof handler !== 'function') { + throw new TypeError('Must pass function handler to `ddManager.trackLink`.'); + } + + for (const el of links) { + if (!isElement(el)) { + throw new TypeError('Must pass HTMLElement to `ddManager.trackLink`.'); + } + bind(el, 'click', onClick(el, handler)); + } + + return this; +} From 545468cca689d5aecab7a0bfb574af2525d2cac4 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Wed, 19 Apr 2017 17:58:31 +0300 Subject: [PATCH 02/22] trackImpression helper --- src/ddManager.js | 10 ++- src/functions/getStyle.js | 8 ++ src/trackers/index.js | 1 + src/trackers/trackImpression.js | 150 ++++++++++++++++++++++++++++++++ src/trackers/trackLink.js | 2 +- 5 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 src/functions/getStyle.js create mode 100644 src/trackers/trackImpression.js diff --git a/src/ddManager.js b/src/ddManager.js index 2a59db7..74a21cc 100644 --- a/src/ddManager.js +++ b/src/ddManager.js @@ -19,7 +19,7 @@ import { VIEWED_PAGE, mapEvent } from './events'; import { validateIntegrationEvent, trackValidationErrors } from './EventValidator'; import { enableErrorTracking } from './ErrorTracker'; import { warn, error as errorLog } from './functions/safeConsole'; -import { trackLink } from './trackers'; +import { trackLink, trackImpression } from './trackers'; let ddManager; @@ -383,8 +383,12 @@ ddManager = { return _eventManager; }, - trackLink: (el, handler) => { - trackLink(el, handler); + trackLink: (elements, handler) => { + trackLink(elements, handler); + }, + + trackImpression: (elements, handler) => { + trackImpression(elements, handler); }, reset: () => { diff --git a/src/functions/getStyle.js b/src/functions/getStyle.js new file mode 100644 index 0000000..de630ad --- /dev/null +++ b/src/functions/getStyle.js @@ -0,0 +1,8 @@ +export default function getStyle(el, prop) { + if (window.getComputedStyle) { + return window.getComputedStyle(el)[prop]; + } else if (el.currentStyle) { + return el.currentStyle[prop]; + } + return undefined; +} diff --git a/src/trackers/index.js b/src/trackers/index.js index 5fb3edd..cb9c22c 100644 --- a/src/trackers/index.js +++ b/src/trackers/index.js @@ -1 +1,2 @@ export { default as trackLink } from './trackLink.js'; +export { default as trackImpression } from './trackImpression.js'; diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js new file mode 100644 index 0000000..4136c91 --- /dev/null +++ b/src/trackers/trackImpression.js @@ -0,0 +1,150 @@ +import { bind } from './../functions/eventListener'; +import getStyle from './../functions/getStyle'; + +class Batch { + constructor(blocks, handler) { + this.blocks = blocks; + this.handler = handler; + } +} + +/** +* array of Batch +*/ +let batches = []; + +let isStarted = false; + +let docViewTop; +let docViewBottom; +let docViewLeft; +let docViewRight; + +function isElement(el) { + return (el && el.nodeType === 1); +} + +function defineDocBoundaries(maxWebsiteWidth) { + const _defineDocBoundaries = () => { + docViewTop = window.pageYOffset; + docViewBottom = docViewTop + window.document.documentElement.clientHeight; + docViewLeft = window.pageXOffset; + docViewRight = docViewLeft + window.document.documentElement.clientWidth; + + if (maxWebsiteWidth && maxWebsiteWidth < this.docViewRight && this.docViewLeft === 0) { + docViewLeft = (docViewRight - maxWebsiteWidth) / 2; + docViewRight = docViewLeft + maxWebsiteWidth; + } + }; + + _defineDocBoundaries(); + bind(window, 'resize', () => { + _defineDocBoundaries(); + }); + bind(window, 'scroll', () => { + _defineDocBoundaries(); + }); +} + +/** + * Returns true if element is visible by css + * and at least 3/4 of the element fit user viewport + * + * @param el DOMElement + * @returns boolean + */ +function isVisible(el) { + const elemWidth = el.clientWidth; + const elemHeight = el.clientHeight; + + const elemTop = el.getBoundingClientRect().top; + const elemBottom = elemTop + elemHeight; + const elemLeft = el.getBoundingClientRect().left; + const elemRight = elemLeft + elemWidth; + + const visible = !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length) && Number(getStyle(el, 'opacity')) > 0 && getStyle(el, 'visibility') !== 'hidden'; + if (!visible) { + return false; + } + + const fitsVertical = ( + ((elemBottom - elemHeight / 4) <= docViewBottom) && + ((elemTop + elemHeight / 4) >= docViewTop) + ); + + const fitsHorizontal = ( + (elemLeft + elemWidth / 4 >= docViewLeft) && + (elemRight - elemWidth / 4 <= docViewRight) + ); + + if (!fitsVertical || !fitsHorizontal) { + return false; + } + + let elementFromPoint = document.elementFromPoint( + elemLeft - window.pageXOffset + elemWidth / 2, + elemTop - window.pageYOffset + elemHeight / 2 + ); + + while (elementFromPoint && elementFromPoint !== el && elementFromPoint.parentNode !== document) { + elementFromPoint = elementFromPoint.parentNode; + } + return (!!elementFromPoint && elementFromPoint === el); +} + +function trackViews() { + for (const batch of batches) { + const newViewedBlocks = []; + + const blocks = batch.blocks; + for (const block of blocks) { + if (isVisible(block)) { + newViewedBlocks.push(block); + } + } + + if (newViewedBlocks.length > 0) { + batch.blocks = blocks.filter((block) => { + return newViewedBlocks.indexOf(block) < 0; + }); + try { + batch.handler(newViewedBlocks); + } catch (error) { + // TODO + } + } + } + + batches = batches.filter((batch) => { + return batch.blocks.length > 0; + }); +} + +function startTracking() { + defineDocBoundaries(); + trackViews(); + setInterval(() => { + trackViews(); + }, 500); +} + +export default function trackImpression(blocks, handler) { + if (!blocks) return; + + if (typeof handler !== 'function') { + throw new TypeError('Must pass function handler to `ddManager.trackImpression`.'); + } + + // always arrays, handles jquery + if (isElement(blocks)) { + blocks = [blocks]; + } else if (blocks.toArray) { + blocks = blocks.toArray(); + } + + batches.push(new Batch(blocks, handler)); + if (!isStarted) { + isStarted = true; + startTracking(); + } +} diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js index 8835a7e..a1f239d 100644 --- a/src/trackers/trackLink.js +++ b/src/trackers/trackLink.js @@ -28,7 +28,7 @@ function onClick(el, handler) { } export default function trackLink(links, handler) { - if (!links) return this; + if (!links) return; // always arrays, handles jquery if (isElement(links)) { links = [links]; From 5a42e8154e9e035cbcdd96d19ba785ad06aacc9c Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 10:54:39 +0300 Subject: [PATCH 03/22] sauce tests --- src/trackers/trackLink.js | 9 ++++--- test/functions/fireEvent.js | 9 +++++++ test/index.test.js | 3 +++ test/trackers/trackLinkSpec.js | 49 ++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/functions/fireEvent.js create mode 100644 test/trackers/trackLinkSpec.js diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js index a1f239d..c2a4505 100644 --- a/src/trackers/trackLink.js +++ b/src/trackers/trackLink.js @@ -13,7 +13,7 @@ function onClick(el, handler) { || el.getAttribute('xlink:href'); try { - handler(); + handler(el); } catch (error) { // TODO } @@ -29,10 +29,11 @@ function onClick(el, handler) { export default function trackLink(links, handler) { if (!links) return; - // always arrays, handles jquery - if (isElement(links)) { + if (typeof links === 'string') { + links = window.document.querySelectorAll(links); + } else if (isElement(links)) { links = [links]; - } else if (links.toArray) { + } else if (links.toArray) { // handles jquery links = links.toArray(); } diff --git a/test/functions/fireEvent.js b/test/functions/fireEvent.js new file mode 100644 index 0000000..105ff2c --- /dev/null +++ b/test/functions/fireEvent.js @@ -0,0 +1,9 @@ +export default function fireEvent(el, etype){ + if (el.fireEvent) { + el.fireEvent('on' + etype); + } else { + const evObj = document.createEvent('Events'); + evObj.initEvent(etype, true, false); + el.dispatchEvent(evObj); + } +} diff --git a/test/index.test.js b/test/index.test.js index 8343183..a860260 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -12,6 +12,9 @@ import './EventDataEnricherSpec.js'; import './DigitalDataEnricherSpec.js'; import './EventValidatorSpec.js'; +// trackers +import './trackers/trackLinkSpec.js'; + // integrations import './integrations/GoogleAnalyticsSpec.js'; import './integrations/GoogleTagManagerSpec.js'; diff --git a/test/trackers/trackLinkSpec.js b/test/trackers/trackLinkSpec.js new file mode 100644 index 0000000..aa519b7 --- /dev/null +++ b/test/trackers/trackLinkSpec.js @@ -0,0 +1,49 @@ +import trackLink from './../../src/trackers/trackLink'; +import fireEvent from './../functions/fireEvent'; +import assert from 'assert'; + +describe('trackLink', () => { + + describe('#button', () => { + let btn; + let div; + + beforeEach(() => { + // create button + btn = document.createElement('button'); + const t = document.createTextNode('click me'); + btn.appendChild(t); + btn.className = 'test-btn'; + + // create div + div = document.createElement('div'); + div.appendChild(btn); + div.id = 'test-div'; + + document.body.appendChild(div); + }); + + afterEach(() => { + document.body.removeChild(div); + }); + + it.only('should track click by class name', (done) => { + trackLink('.test-btn', (link) => { + assert.equal(typeof link, 'object'); + done(); + }); + + fireEvent(btn, 'click'); + }); + + it.only('should track click by nested class name', (done) => { + trackLink('#test-div .test-btn', (link) => { + assert.equal(typeof link, 'object'); + done(); + }); + + fireEvent(btn, 'click'); + }); + }) + +}); From 0a0ff5f41fc1227c595c540fc62b73c308f1bdee Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 11:02:40 +0300 Subject: [PATCH 04/22] symbol polyfill --- src/polyfill.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/polyfill.js b/src/polyfill.js index e337f73..e115438 100644 --- a/src/polyfill.js +++ b/src/polyfill.js @@ -10,3 +10,4 @@ import 'core-js/modules/es6.string.trim'; import 'core-js/modules/es6.string.ends-with'; import 'core-js/modules/es6.date.to-iso-string'; import 'core-js/modules/es6.date.now'; +import 'core-js/modules/es6.symbol'; From 8b5a34c63076efbf0e406f649391746d2b0c846c Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 11:10:39 +0300 Subject: [PATCH 05/22] sauce test --- src/trackers/trackLink.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js index c2a4505..23e5088 100644 --- a/src/trackers/trackLink.js +++ b/src/trackers/trackLink.js @@ -30,7 +30,7 @@ function onClick(el, handler) { export default function trackLink(links, handler) { if (!links) return; if (typeof links === 'string') { - links = window.document.querySelectorAll(links); + links = Array.prototype.slice.call(window.document.querySelectorAll(links)); } else if (isElement(links)) { links = [links]; } else if (links.toArray) { // handles jquery From 7c94291c9fe145efb36b8df40d22eec05079ff85 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 11:27:59 +0300 Subject: [PATCH 06/22] domQuery optimization --- src/functions/domQuery.js | 22 ++++++++++++++++++++++ src/polyfill.js | 1 - src/trackers/trackImpression.js | 14 ++++++++++++++ src/trackers/trackLink.js | 3 ++- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/functions/domQuery.js diff --git a/src/functions/domQuery.js b/src/functions/domQuery.js new file mode 100644 index 0000000..0d96761 --- /dev/null +++ b/src/functions/domQuery.js @@ -0,0 +1,22 @@ +export default function domQuery(selector, context) { + context = context || window.document; + // Redirect simple selectors to the more performant function + if (/^(#?[\w-]+|\.[\w-.]+)$/.test(selector)) { + switch (selector.charAt(0)) { + case '#': + // Handle ID-based selectors + return [context.getElementById(selector.substr(1))]; + case '.': + // Handle class-based selectors + // Query by multiple classes by converting the selector + // string into single spaced class names + var classes = selector.substr(1).replace(/\./g, ' '); + return [].slice.call(context.getElementsByClassName(classes)); + default: + // Handle tag-based selectors + return [].slice.call(context.getElementsByTagName(selector)); + } + } + // Default to `querySelectorAll` + return [].slice.call(context.querySelectorAll(selector)); +} diff --git a/src/polyfill.js b/src/polyfill.js index e115438..e337f73 100644 --- a/src/polyfill.js +++ b/src/polyfill.js @@ -10,4 +10,3 @@ import 'core-js/modules/es6.string.trim'; import 'core-js/modules/es6.string.ends-with'; import 'core-js/modules/es6.date.to-iso-string'; import 'core-js/modules/es6.date.now'; -import 'core-js/modules/es6.symbol'; diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index 4136c91..6fc6b05 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -8,6 +8,20 @@ class Batch { } } +class BatchTable { + constructor() { + this.selectors = []; + this.batches = {}; + } + + add(selector, handler) { + if (this.selectors.indexOf(selector) < 0) { + this.selectors.push(selector); + this.batches[selector] = []; + } + } +} + /** * array of Batch */ diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js index 23e5088..1e97a64 100644 --- a/src/trackers/trackLink.js +++ b/src/trackers/trackLink.js @@ -1,6 +1,7 @@ import { bind } from './../functions/eventListener'; import isMeta from './../functions/isMeta'; import preventDefault from './../functions/preventDefault'; +import domQuery from './../functions/domQuery'; function isElement(el) { return (el && el.nodeType === 1); @@ -30,7 +31,7 @@ function onClick(el, handler) { export default function trackLink(links, handler) { if (!links) return; if (typeof links === 'string') { - links = Array.prototype.slice.call(window.document.querySelectorAll(links)); + links = domQuery(links); } else if (isElement(links)) { links = [links]; } else if (links.toArray) { // handles jquery From caf45c890e7b54aa5ddae48c4547847b41bc16cd Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 11:39:40 +0300 Subject: [PATCH 07/22] sauce test --- karma.conf.js | 120 ++++++++++++++++---------------- src/trackers/trackImpression.js | 27 +++++++ src/trackers/trackLink.js | 3 +- 3 files changed, 89 insertions(+), 61 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 847dfa8..1c12772 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -24,72 +24,72 @@ module.exports = function(config) { // platform: 'Windows 7', // version: '8' // }, - slChromeWinXp: { - base: 'SauceLabs', - browserName: 'chrome', - platform: 'Windows XP' - }, - slIe10Win7: { - base: 'SauceLabs', - browserName: 'internet explorer', - platform: 'Windows 7', - version: '10' - }, - slIe9Win7: { - base: 'SauceLabs', - browserName: 'internet explorer', - platform: 'Windows 7', - version: '9' - }, - slIe11Win10: { - base: 'SauceLabs', - browserName: 'internet explorer', - platform: 'Windows 10', - version: '11' - }, + // slChromeWinXp: { + // base: 'SauceLabs', + // browserName: 'chrome', + // platform: 'Windows XP' + // }, + // slIe10Win7: { + // base: 'SauceLabs', + // browserName: 'internet explorer', + // platform: 'Windows 7', + // version: '10' + // }, + // slIe9Win7: { + // base: 'SauceLabs', + // browserName: 'internet explorer', + // platform: 'Windows 7', + // version: '9' + // }, + // slIe11Win10: { + // base: 'SauceLabs', + // browserName: 'internet explorer', + // platform: 'Windows 10', + // version: '11' + // }, slME25Win10: { base: 'SauceLabs', browserName: 'microsoftedge', platform: 'Windows 10', version: '13' }, - slSafariOsx: { - base: 'SauceLabs', - browserName: 'safari', - platform: 'OS X 10.8' - }, - slSafariOsx11: { - base: 'SauceLabs', - browserName: 'safari', - platform: 'OS X 10.11' - }, - slOpera12Win7: { - base: 'SauceLabs', - browserName: 'opera', - platform: 'Windows 7', - version: '12.12' - }, - slIphone: { - base: 'SauceLabs', - browserName: 'iphone', - platform: 'OS X 10.10', - version: '8.2', - deviceName: 'iPhone Simulator' - }, - slIpad: { - base: 'SauceLabs', - browserName: 'iphone', - platform: 'OS X 10.10', - version: '8.2', - deviceName: 'iPad Simulator' - }, - slAndroid: { - base: 'SauceLabs', - browserName: 'android', - platform: 'Linux', - version: '5.1', - deviceName: 'Android Emulator' - } + // slSafariOsx: { + // base: 'SauceLabs', + // browserName: 'safari', + // platform: 'OS X 10.8' + // }, + // slSafariOsx11: { + // base: 'SauceLabs', + // browserName: 'safari', + // platform: 'OS X 10.11' + // }, + // slOpera12Win7: { + // base: 'SauceLabs', + // browserName: 'opera', + // platform: 'Windows 7', + // version: '12.12' + // }, + // slIphone: { + // base: 'SauceLabs', + // browserName: 'iphone', + // platform: 'OS X 10.10', + // version: '8.2', + // deviceName: 'iPhone Simulator' + // }, + // slIpad: { + // base: 'SauceLabs', + // browserName: 'iphone', + // platform: 'OS X 10.10', + // version: '8.2', + // deviceName: 'iPad Simulator' + // }, + // slAndroid: { + // base: 'SauceLabs', + // browserName: 'android', + // platform: 'Linux', + // version: '5.1', + // deviceName: 'Android Emulator' + // } }; browsers = Object.keys(customLaunchers); } else { diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index 6fc6b05..bb47557 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -1,11 +1,16 @@ import { bind } from './../functions/eventListener'; import getStyle from './../functions/getStyle'; +import domQuery from './../functions/domQuery'; class Batch { constructor(blocks, handler) { this.blocks = blocks; this.handler = handler; } + + updateBlocks(blocks) { + this.blocks = blocks; + } } class BatchTable { @@ -19,6 +24,20 @@ class BatchTable { this.selectors.push(selector); this.batches[selector] = []; } + + const blocks = domQuery(selector); + const batch = new Batch(blocks, handler); + this.batches[selector].push(batch) + } + + update() { + for (const selector of this.selectors) { + const batches = this.batches[selector]; + const blocks = domQuery(selector); + for (const batch of batches) { + batch.updateBlocks(blocks); + } + } } } @@ -106,7 +125,15 @@ function isVisible(el) { return (!!elementFromPoint && elementFromPoint === el); } +updateTrackedComponents() { + for (const selector of this.selectors) { + this.$trackedComponents[selector] = window.jQuery(selector); + } +} + function trackViews() { + updateTrackedComponents(); + for (const batch of batches) { const newViewedBlocks = []; diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js index 1e97a64..c2576da 100644 --- a/src/trackers/trackLink.js +++ b/src/trackers/trackLink.js @@ -9,6 +9,7 @@ function isElement(el) { function onClick(el, handler) { return (e) => { + console.log(el); const href = el.getAttribute('href') || el.getAttributeNS('http://www.w3.org/1999/xlink', 'href') || el.getAttribute('xlink:href'); @@ -34,7 +35,7 @@ export default function trackLink(links, handler) { links = domQuery(links); } else if (isElement(links)) { links = [links]; - } else if (links.toArray) { // handles jquery + } else if (links.toArray) { // handles jquery links = links.toArray(); } From a25a5b86d9623f53244b9160c1f1fb7d65cfb93b Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 11:44:05 +0300 Subject: [PATCH 08/22] suace tests --- src/trackers/trackImpression.js | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index bb47557..9bcded6 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -41,10 +41,7 @@ class BatchTable { } } -/** -* array of Batch -*/ -let batches = []; +const batchTable = new BatchTable(); let isStarted = false; @@ -125,14 +122,8 @@ function isVisible(el) { return (!!elementFromPoint && elementFromPoint === el); } -updateTrackedComponents() { - for (const selector of this.selectors) { - this.$trackedComponents[selector] = window.jQuery(selector); - } -} - function trackViews() { - updateTrackedComponents(); + batchTable.update(); for (const batch of batches) { const newViewedBlocks = []; @@ -169,23 +160,16 @@ function startTracking() { }, 500); } -export default function trackImpression(blocks, handler) { - if (!blocks) return; +export default function trackImpression(selector, handler) { + if (!selector) return; if (typeof handler !== 'function') { throw new TypeError('Must pass function handler to `ddManager.trackImpression`.'); } - // always arrays, handles jquery - if (isElement(blocks)) { - blocks = [blocks]; - } else if (blocks.toArray) { - blocks = blocks.toArray(); - } - - batches.push(new Batch(blocks, handler)); if (!isStarted) { isStarted = true; + batchTable.add(selector, handler); startTracking(); } } From f49f095b9086694b5123751fa5222f521d83eb2c Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 11:54:15 +0300 Subject: [PATCH 09/22] sauce tests --- karma.conf.js | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 1c12772..a47d3a0 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -24,29 +24,29 @@ module.exports = function(config) { // platform: 'Windows 7', // version: '8' // }, - // slChromeWinXp: { - // base: 'SauceLabs', - // browserName: 'chrome', - // platform: 'Windows XP' - // }, - // slIe10Win7: { - // base: 'SauceLabs', - // browserName: 'internet explorer', - // platform: 'Windows 7', - // version: '10' - // }, - // slIe9Win7: { - // base: 'SauceLabs', - // browserName: 'internet explorer', - // platform: 'Windows 7', - // version: '9' - // }, - // slIe11Win10: { - // base: 'SauceLabs', - // browserName: 'internet explorer', - // platform: 'Windows 10', - // version: '11' - // }, + slChromeWinXp: { + base: 'SauceLabs', + browserName: 'chrome', + platform: 'Windows XP' + }, + slIe10Win7: { + base: 'SauceLabs', + browserName: 'internet explorer', + platform: 'Windows 7', + version: '10' + }, + slIe9Win7: { + base: 'SauceLabs', + browserName: 'internet explorer', + platform: 'Windows 7', + version: '9' + }, + slIe11Win10: { + base: 'SauceLabs', + browserName: 'internet explorer', + platform: 'Windows 10', + version: '11' + }, slME25Win10: { base: 'SauceLabs', browserName: 'microsoftedge', From 04acf6a41385473c400892c428ce35c99b968394 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 12:04:47 +0300 Subject: [PATCH 10/22] suace test --- karma.conf.js | 22 +++++++++++----------- src/trackers/trackImpression.js | 27 ++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index a47d3a0..9910db8 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -24,11 +24,11 @@ module.exports = function(config) { // platform: 'Windows 7', // version: '8' // }, - slChromeWinXp: { - base: 'SauceLabs', - browserName: 'chrome', - platform: 'Windows XP' - }, + // slChromeWinXp: { + // base: 'SauceLabs', + // browserName: 'chrome', + // platform: 'Windows XP' + // }, slIe10Win7: { base: 'SauceLabs', browserName: 'internet explorer', @@ -41,12 +41,12 @@ module.exports = function(config) { platform: 'Windows 7', version: '9' }, - slIe11Win10: { - base: 'SauceLabs', - browserName: 'internet explorer', - platform: 'Windows 10', - version: '11' - }, + // slIe11Win10: { + // base: 'SauceLabs', + // browserName: 'internet explorer', + // platform: 'Windows 10', + // version: '11' + // }, slME25Win10: { base: 'SauceLabs', browserName: 'microsoftedge', diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index 9bcded6..c106026 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -5,9 +5,18 @@ import domQuery from './../functions/domQuery'; class Batch { constructor(blocks, handler) { this.blocks = blocks; + this.viewedBlocks = []; this.handler = handler; } + addViewedBlock(block) { + this.viewedBlocks.push(block); + } + + isViewedBlock(block) { + return !(this.viewedBlocks.indexOf(block) < 0); + } + updateBlocks(blocks) { this.blocks = blocks; } @@ -39,6 +48,15 @@ class BatchTable { } } } + + getAll() { + const allBatches = []; + for (const selector of this.selectors) { + const batches = this.batches[selector]; + allBatches = [...allBatches, ...batches]; + } + return allBatches; + } } const batchTable = new BatchTable(); @@ -125,13 +143,16 @@ function isVisible(el) { function trackViews() { batchTable.update(); + const batches = batchTable.getAll(); + for (const batch of batches) { const newViewedBlocks = []; const blocks = batch.blocks; for (const block of blocks) { - if (isVisible(block)) { + if (isVisible(block) && !batch.isViewedBlock(block)) { newViewedBlocks.push(block); + batch.addViewedBlock(block); } } @@ -146,10 +167,6 @@ function trackViews() { } } } - - batches = batches.filter((batch) => { - return batch.blocks.length > 0; - }); } function startTracking() { From 114c1f9654a4f0342c26adf93382324f7aba073b Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 12:07:13 +0300 Subject: [PATCH 11/22] sauce --- src/trackers/trackImpression.js | 2 +- src/trackers/trackLink.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index c106026..f48f18b 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -50,7 +50,7 @@ class BatchTable { } getAll() { - const allBatches = []; + let allBatches = []; for (const selector of this.selectors) { const batches = this.batches[selector]; allBatches = [...allBatches, ...batches]; diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js index c2576da..0b2f11a 100644 --- a/src/trackers/trackLink.js +++ b/src/trackers/trackLink.js @@ -9,7 +9,7 @@ function isElement(el) { function onClick(el, handler) { return (e) => { - console.log(el); + console.warn(el); const href = el.getAttribute('href') || el.getAttributeNS('http://www.w3.org/1999/xlink', 'href') || el.getAttribute('xlink:href'); From 02ed19b233998387fea99e3e58fb4effeaabceaa Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 12:11:23 +0300 Subject: [PATCH 12/22] sauce --- src/trackers/trackImpression.js | 4 ---- src/trackers/trackLink.js | 3 ++- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index f48f18b..f108503 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -68,10 +68,6 @@ let docViewBottom; let docViewLeft; let docViewRight; -function isElement(el) { - return (el && el.nodeType === 1); -} - function defineDocBoundaries(maxWebsiteWidth) { const _defineDocBoundaries = () => { docViewTop = window.pageYOffset; diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js index 0b2f11a..767d9c8 100644 --- a/src/trackers/trackLink.js +++ b/src/trackers/trackLink.js @@ -9,12 +9,13 @@ function isElement(el) { function onClick(el, handler) { return (e) => { - console.warn(el); const href = el.getAttribute('href') || el.getAttributeNS('http://www.w3.org/1999/xlink', 'href') || el.getAttribute('xlink:href'); try { + console.warn(el); + console.warn(handler); handler(el); } catch (error) { // TODO From 717e5756d0108c1c68985746ae3a8d5df5aa07a6 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 12:19:20 +0300 Subject: [PATCH 13/22] sauce --- src/trackers/trackImpression.js | 5 +---- test/trackers/trackLinkSpec.js | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index f108503..b32b3bc 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -153,9 +153,6 @@ function trackViews() { } if (newViewedBlocks.length > 0) { - batch.blocks = blocks.filter((block) => { - return newViewedBlocks.indexOf(block) < 0; - }); try { batch.handler(newViewedBlocks); } catch (error) { @@ -170,7 +167,7 @@ function startTracking() { trackViews(); setInterval(() => { trackViews(); - }, 500); + }, 2000); } export default function trackImpression(selector, handler) { diff --git a/test/trackers/trackLinkSpec.js b/test/trackers/trackLinkSpec.js index aa519b7..8bc19db 100644 --- a/test/trackers/trackLinkSpec.js +++ b/test/trackers/trackLinkSpec.js @@ -29,6 +29,7 @@ describe('trackLink', () => { it.only('should track click by class name', (done) => { trackLink('.test-btn', (link) => { + console.log(typeof link, link); assert.equal(typeof link, 'object'); done(); }); From f3d3472632bfa9f7889a7230a29f1a7440baeba2 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 12:27:10 +0300 Subject: [PATCH 14/22] sauce --- src/trackers/trackImpression.js | 1 + src/trackers/trackLink.js | 1 + test/trackers/trackLinkSpec.js | 1 - 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index b32b3bc..e19f0fd 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -146,6 +146,7 @@ function trackViews() { const blocks = batch.blocks; for (const block of blocks) { + console.log(block, isVisible(block), !batch.isViewedBlock(block)); if (isVisible(block) && !batch.isViewedBlock(block)) { newViewedBlocks.push(block); batch.addViewedBlock(block); diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js index 767d9c8..8dd4f1f 100644 --- a/src/trackers/trackLink.js +++ b/src/trackers/trackLink.js @@ -18,6 +18,7 @@ function onClick(el, handler) { console.warn(handler); handler(el); } catch (error) { + console.warn(error); // TODO } diff --git a/test/trackers/trackLinkSpec.js b/test/trackers/trackLinkSpec.js index 8bc19db..aa519b7 100644 --- a/test/trackers/trackLinkSpec.js +++ b/test/trackers/trackLinkSpec.js @@ -29,7 +29,6 @@ describe('trackLink', () => { it.only('should track click by class name', (done) => { trackLink('.test-btn', (link) => { - console.log(typeof link, link); assert.equal(typeof link, 'object'); done(); }); From ef4f7e918562c22a736a2c7b1498f7188fa325f6 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 12:35:32 +0300 Subject: [PATCH 15/22] sauce --- src/trackers/trackImpression.js | 1 + test/functions/fireEvent.js | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index e19f0fd..383f225 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -14,6 +14,7 @@ class Batch { } isViewedBlock(block) { + console.log('Viewed Blocks', this.viewedBlocks); return !(this.viewedBlocks.indexOf(block) < 0); } diff --git a/test/functions/fireEvent.js b/test/functions/fireEvent.js index 105ff2c..c66d43a 100644 --- a/test/functions/fireEvent.js +++ b/test/functions/fireEvent.js @@ -2,8 +2,20 @@ export default function fireEvent(el, etype){ if (el.fireEvent) { el.fireEvent('on' + etype); } else { - const evObj = document.createEvent('Events'); - evObj.initEvent(etype, true, false); - el.dispatchEvent(evObj); + let event; + if (document.createEvent) { + event = document.createEvent("HTMLEvents"); + event.initEvent(etype, true, true); + } else { + event = document.createEventObject(); + event.eventType = etype; + } + event.eventName = etype; + + if (el.dispatchEvent) { + el.dispatchEvent(event); + } else { + el.fireEvent(`on${etype}`, event); + } } } From ac9d7b088662170253c16a470b620b938315148b Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 12:43:46 +0300 Subject: [PATCH 16/22] sauce --- karma.conf.js | 12 ++++++------ src/trackers/trackImpression.js | 10 +++++++--- test/functions/fireEvent.js | 13 +++++++++---- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 9910db8..c8543da 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -47,12 +47,12 @@ module.exports = function(config) { // platform: 'Windows 10', // version: '11' // }, - slME25Win10: { - base: 'SauceLabs', - browserName: 'microsoftedge', - platform: 'Windows 10', - version: '13' - }, + // slME25Win10: { + // base: 'SauceLabs', + // browserName: 'microsoftedge', + // platform: 'Windows 10', + // version: '13' + // }, // slSafariOsx: { // base: 'SauceLabs', // browserName: 'safari', diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index 383f225..f966f2b 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -11,10 +11,10 @@ class Batch { addViewedBlock(block) { this.viewedBlocks.push(block); + console.log(this.viewedBlocks); } isViewedBlock(block) { - console.log('Viewed Blocks', this.viewedBlocks); return !(this.viewedBlocks.indexOf(block) < 0); } @@ -54,7 +54,10 @@ class BatchTable { let allBatches = []; for (const selector of this.selectors) { const batches = this.batches[selector]; - allBatches = [...allBatches, ...batches]; + for (const batch of batches) { + allBatches.push(batch); + } + // allBatches = [...allBatches, ...batches]; } return allBatches; } @@ -147,9 +150,10 @@ function trackViews() { const blocks = batch.blocks; for (const block of blocks) { - console.log(block, isVisible(block), !batch.isViewedBlock(block)); + // console.log(block, isVisible(block), !batch.isViewedBlock(block)); if (isVisible(block) && !batch.isViewedBlock(block)) { newViewedBlocks.push(block); + console.log('batch.addViewedBlock(block)'); batch.addViewedBlock(block); } } diff --git a/test/functions/fireEvent.js b/test/functions/fireEvent.js index c66d43a..b042b20 100644 --- a/test/functions/fireEvent.js +++ b/test/functions/fireEvent.js @@ -1,20 +1,25 @@ export default function fireEvent(el, etype){ if (el.fireEvent) { + console.warn('1'); el.fireEvent('on' + etype); } else { let event; if (document.createEvent) { - event = document.createEvent("HTMLEvents"); - event.initEvent(etype, true, true); + console.warn('2'); + event = document.createEvent("HTMLEvents"); + event.initEvent(etype, true, true); } else { - event = document.createEventObject(); - event.eventType = etype; + console.warn('3'); + event = document.createEventObject(); + event.eventType = etype; } event.eventName = etype; if (el.dispatchEvent) { + console.warn('4'); el.dispatchEvent(event); } else { + console.warn('5'); el.fireEvent(`on${etype}`, event); } } From a04a5a1d18fd03cd734f7eeec5a8023748028455 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 13:58:55 +0300 Subject: [PATCH 17/22] sauce --- src/trackers/trackImpression.js | 34 ++++++++++++++---------------- test/functions/fireEvent.js | 37 ++++++++++++++------------------- 2 files changed, 32 insertions(+), 39 deletions(-) diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index f966f2b..763cec4 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -11,7 +11,6 @@ class Batch { addViewedBlock(block) { this.viewedBlocks.push(block); - console.log(this.viewedBlocks); } isViewedBlock(block) { @@ -54,10 +53,7 @@ class BatchTable { let allBatches = []; for (const selector of this.selectors) { const batches = this.batches[selector]; - for (const batch of batches) { - allBatches.push(batch); - } - // allBatches = [...allBatches, ...batches]; + allBatches = [...allBatches, ...batches]; } return allBatches; } @@ -74,10 +70,13 @@ let docViewRight; function defineDocBoundaries(maxWebsiteWidth) { const _defineDocBoundaries = () => { - docViewTop = window.pageYOffset; - docViewBottom = docViewTop + window.document.documentElement.clientHeight; - docViewLeft = window.pageXOffset; - docViewRight = docViewLeft + window.document.documentElement.clientWidth; + const body = window.document.body; + const docEl = window.document.documentElement; + + docViewTop = window.pageYOffset || docEl.scrollTop || body.scrollTop;; + docViewBottom = docViewTop + docEl.clientHeight; + docViewLeft = window.pageXOffset || docEl.scrollLeft || body.scrollLeft; + docViewRight = docViewLeft + docEl.clientWidth; if (maxWebsiteWidth && maxWebsiteWidth < this.docViewRight && this.docViewLeft === 0) { docViewLeft = (docViewRight - maxWebsiteWidth) / 2; @@ -102,6 +101,8 @@ function defineDocBoundaries(maxWebsiteWidth) { * @returns boolean */ function isVisible(el) { + const docEl = window.document.documentElement; + const elemWidth = el.clientWidth; const elemHeight = el.clientHeight; @@ -116,13 +117,13 @@ function isVisible(el) { } const fitsVertical = ( - ((elemBottom - elemHeight / 4) <= docViewBottom) && - ((elemTop + elemHeight / 4) >= docViewTop) + ((elemBottom - elemHeight / 4) <= docEl.clientHeight) && + ((elemTop + elemHeight / 4) >= 0) ); const fitsHorizontal = ( - (elemLeft + elemWidth / 4 >= docViewLeft) && - (elemRight - elemWidth / 4 <= docViewRight) + (elemLeft + elemWidth / 4 >= 0) && + (elemRight - elemWidth / 4 <= docEl.clientWidth) ); if (!fitsVertical || !fitsHorizontal) { @@ -130,8 +131,8 @@ function isVisible(el) { } let elementFromPoint = document.elementFromPoint( - elemLeft - window.pageXOffset + elemWidth / 2, - elemTop - window.pageYOffset + elemHeight / 2 + elemLeft + elemWidth / 2, + elemTop + elemHeight / 2 ); while (elementFromPoint && elementFromPoint !== el && elementFromPoint.parentNode !== document) { @@ -144,16 +145,13 @@ function trackViews() { batchTable.update(); const batches = batchTable.getAll(); - for (const batch of batches) { const newViewedBlocks = []; const blocks = batch.blocks; for (const block of blocks) { - // console.log(block, isVisible(block), !batch.isViewedBlock(block)); if (isVisible(block) && !batch.isViewedBlock(block)) { newViewedBlocks.push(block); - console.log('batch.addViewedBlock(block)'); batch.addViewedBlock(block); } } diff --git a/test/functions/fireEvent.js b/test/functions/fireEvent.js index b042b20..7ae8456 100644 --- a/test/functions/fireEvent.js +++ b/test/functions/fireEvent.js @@ -1,26 +1,21 @@ export default function fireEvent(el, etype){ - if (el.fireEvent) { - console.warn('1'); - el.fireEvent('on' + etype); + let event; + if (document.createEvent) { + console.warn('2'); + event = document.createEvent("HTMLEvents"); + event.initEvent(etype, true, true); } else { - let event; - if (document.createEvent) { - console.warn('2'); - event = document.createEvent("HTMLEvents"); - event.initEvent(etype, true, true); - } else { - console.warn('3'); - event = document.createEventObject(); - event.eventType = etype; - } - event.eventName = etype; + console.warn('3'); + event = document.createEventObject(); + event.eventType = etype; + } + event.eventName = etype; - if (el.dispatchEvent) { - console.warn('4'); - el.dispatchEvent(event); - } else { - console.warn('5'); - el.fireEvent(`on${etype}`, event); - } + if (el.dispatchEvent) { + console.warn('4'); + el.dispatchEvent(event); + } else { + console.warn('5'); + el.fireEvent(`on${etype}`, event); } } From 0e6e456d686f9c918a1c066cf025ee81eb6a280e Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 14:36:58 +0300 Subject: [PATCH 18/22] sauce --- karma.conf.js | 120 ++++++++++++++++++------------------ test/functions/fireEvent.js | 4 -- 2 files changed, 60 insertions(+), 64 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index c8543da..0f94c2c 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -18,17 +18,17 @@ module.exports = function(config) { if (process.env.SAUCE_ENABLED == "true") { customLaunchers = { - // slIe8Win7: { - // base: 'SauceLabs', - // browserName: 'internet explorer', - // platform: 'Windows 7', - // version: '8' - // }, - // slChromeWinXp: { - // base: 'SauceLabs', - // browserName: 'chrome', - // platform: 'Windows XP' - // }, + slIe8Win7: { + base: 'SauceLabs', + browserName: 'internet explorer', + platform: 'Windows 7', + version: '8' + }, + slChromeWinXp: { + base: 'SauceLabs', + browserName: 'chrome', + platform: 'Windows XP' + }, slIe10Win7: { base: 'SauceLabs', browserName: 'internet explorer', @@ -41,55 +41,55 @@ module.exports = function(config) { platform: 'Windows 7', version: '9' }, - // slIe11Win10: { - // base: 'SauceLabs', - // browserName: 'internet explorer', - // platform: 'Windows 10', - // version: '11' - // }, - // slME25Win10: { - // base: 'SauceLabs', - // browserName: 'microsoftedge', - // platform: 'Windows 10', - // version: '13' - // }, - // slSafariOsx: { - // base: 'SauceLabs', - // browserName: 'safari', - // platform: 'OS X 10.8' - // }, - // slSafariOsx11: { - // base: 'SauceLabs', - // browserName: 'safari', - // platform: 'OS X 10.11' - // }, - // slOpera12Win7: { - // base: 'SauceLabs', - // browserName: 'opera', - // platform: 'Windows 7', - // version: '12.12' - // }, - // slIphone: { - // base: 'SauceLabs', - // browserName: 'iphone', - // platform: 'OS X 10.10', - // version: '8.2', - // deviceName: 'iPhone Simulator' - // }, - // slIpad: { - // base: 'SauceLabs', - // browserName: 'iphone', - // platform: 'OS X 10.10', - // version: '8.2', - // deviceName: 'iPad Simulator' - // }, - // slAndroid: { - // base: 'SauceLabs', - // browserName: 'android', - // platform: 'Linux', - // version: '5.1', - // deviceName: 'Android Emulator' - // } + slIe11Win10: { + base: 'SauceLabs', + browserName: 'internet explorer', + platform: 'Windows 10', + version: '11' + }, + slME25Win10: { + base: 'SauceLabs', + browserName: 'microsoftedge', + platform: 'Windows 10', + version: '13' + }, + slSafariOsx: { + base: 'SauceLabs', + browserName: 'safari', + platform: 'OS X 10.8' + }, + slSafariOsx11: { + base: 'SauceLabs', + browserName: 'safari', + platform: 'OS X 10.11' + }, + slOpera12Win7: { + base: 'SauceLabs', + browserName: 'opera', + platform: 'Windows 7', + version: '12.12' + }, + slIphone: { + base: 'SauceLabs', + browserName: 'iphone', + platform: 'OS X 10.10', + version: '8.2', + deviceName: 'iPhone Simulator' + }, + slIpad: { + base: 'SauceLabs', + browserName: 'iphone', + platform: 'OS X 10.10', + version: '8.2', + deviceName: 'iPad Simulator' + }, + slAndroid: { + base: 'SauceLabs', + browserName: 'android', + platform: 'Linux', + version: '5.1', + deviceName: 'Android Emulator' + } }; browsers = Object.keys(customLaunchers); } else { diff --git a/test/functions/fireEvent.js b/test/functions/fireEvent.js index 7ae8456..dfc1af3 100644 --- a/test/functions/fireEvent.js +++ b/test/functions/fireEvent.js @@ -1,21 +1,17 @@ export default function fireEvent(el, etype){ let event; if (document.createEvent) { - console.warn('2'); event = document.createEvent("HTMLEvents"); event.initEvent(etype, true, true); } else { - console.warn('3'); event = document.createEventObject(); event.eventType = etype; } event.eventName = etype; if (el.dispatchEvent) { - console.warn('4'); el.dispatchEvent(event); } else { - console.warn('5'); el.fireEvent(`on${etype}`, event); } } From 733e18bd5a9f7ce7f279f66f9a8c27e151406cab Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 14:51:08 +0300 Subject: [PATCH 19/22] trackImpressions implementation in pure JS --- src/trackers/trackImpression.js | 12 ++++++------ test/trackers/trackLinkSpec.js | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index 763cec4..ab3622d 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -17,7 +17,7 @@ class Batch { return !(this.viewedBlocks.indexOf(block) < 0); } - updateBlocks(blocks) { + setBlocks(blocks) { this.blocks = blocks; } } @@ -34,8 +34,7 @@ class BatchTable { this.batches[selector] = []; } - const blocks = domQuery(selector); - const batch = new Batch(blocks, handler); + const batch = new Batch(handler); this.batches[selector].push(batch) } @@ -44,7 +43,7 @@ class BatchTable { const batches = this.batches[selector]; const blocks = domQuery(selector); for (const batch of batches) { - batch.updateBlocks(blocks); + batch.setBlocks(blocks); } } } @@ -171,7 +170,7 @@ function startTracking() { trackViews(); setInterval(() => { trackViews(); - }, 2000); + }, 500); } export default function trackImpression(selector, handler) { @@ -181,9 +180,10 @@ export default function trackImpression(selector, handler) { throw new TypeError('Must pass function handler to `ddManager.trackImpression`.'); } + batchTable.add(selector, handler); + if (!isStarted) { isStarted = true; - batchTable.add(selector, handler); startTracking(); } } diff --git a/test/trackers/trackLinkSpec.js b/test/trackers/trackLinkSpec.js index aa519b7..563d3d6 100644 --- a/test/trackers/trackLinkSpec.js +++ b/test/trackers/trackLinkSpec.js @@ -27,7 +27,7 @@ describe('trackLink', () => { document.body.removeChild(div); }); - it.only('should track click by class name', (done) => { + it('should track click by class name', (done) => { trackLink('.test-btn', (link) => { assert.equal(typeof link, 'object'); done(); @@ -36,7 +36,7 @@ describe('trackLink', () => { fireEvent(btn, 'click'); }); - it.only('should track click by nested class name', (done) => { + it('should track click by nested class name', (done) => { trackLink('#test-div .test-btn', (link) => { assert.equal(typeof link, 'object'); done(); From 056efde73dc3b6e4960d427a258db9e71fc648f3 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 14:51:25 +0300 Subject: [PATCH 20/22] disable ie8 --- karma.conf.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 0f94c2c..847dfa8 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -18,12 +18,12 @@ module.exports = function(config) { if (process.env.SAUCE_ENABLED == "true") { customLaunchers = { - slIe8Win7: { - base: 'SauceLabs', - browserName: 'internet explorer', - platform: 'Windows 7', - version: '8' - }, + // slIe8Win7: { + // base: 'SauceLabs', + // browserName: 'internet explorer', + // platform: 'Windows 7', + // version: '8' + // }, slChromeWinXp: { base: 'SauceLabs', browserName: 'chrome', From a34f4db4e34e2440622c5ac392f4f7db20516c5d Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 15:15:24 +0300 Subject: [PATCH 21/22] version bump --- package.json | 2 +- src/ddManager.js | 2 +- src/trackers/trackImpression.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 007bb8e..76f0611 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "digital-data-manager", "description": "The hassle-free way to integrate Digital Data Layer on your website.", "author": "Driveback LLC ", - "version": "1.2.31", + "version": "1.2.32", "license": "MIT", "main": "dist/dd-manager.js", "scripts": { diff --git a/src/ddManager.js b/src/ddManager.js index 74a21cc..da4f282 100644 --- a/src/ddManager.js +++ b/src/ddManager.js @@ -241,7 +241,7 @@ function _initializeIntegrations(settings) { ddManager = { - VERSION: '1.2.31', + VERSION: '1.2.32', setAvailableIntegrations: (availableIntegrations) => { _availableIntegrations = availableIntegrations; diff --git a/src/trackers/trackImpression.js b/src/trackers/trackImpression.js index ab3622d..93cc9a2 100644 --- a/src/trackers/trackImpression.js +++ b/src/trackers/trackImpression.js @@ -3,8 +3,8 @@ import getStyle from './../functions/getStyle'; import domQuery from './../functions/domQuery'; class Batch { - constructor(blocks, handler) { - this.blocks = blocks; + constructor(handler) { + this.blocks = []; this.viewedBlocks = []; this.handler = handler; } From 067848870d1705baaebbe9d84c01472d9e8fc705 Mon Sep 17 00:00:00 2001 From: Constantine Yurevich Date: Thu, 20 Apr 2017 15:33:32 +0300 Subject: [PATCH 22/22] removed console --- src/trackers/trackLink.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/trackers/trackLink.js b/src/trackers/trackLink.js index 8dd4f1f..bf0d71a 100644 --- a/src/trackers/trackLink.js +++ b/src/trackers/trackLink.js @@ -14,11 +14,8 @@ function onClick(el, handler) { || el.getAttribute('xlink:href'); try { - console.warn(el); - console.warn(handler); handler(el); } catch (error) { - console.warn(error); // TODO }