Commit 6f772644 by Chris

update dist to latest version

parent 1c89f3aa
...@@ -100,7 +100,7 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -100,7 +100,7 @@ return /******/ (function(modules) { // webpackBootstrap
"use strict"; "use strict";
/*! /**!
* Foresight * Foresight
* *
* @author James Ooi <james.ooi@forefront.com.my> * @author James Ooi <james.ooi@forefront.com.my>
...@@ -128,11 +128,11 @@ var Foresight = /** @class */ (function () { ...@@ -128,11 +128,11 @@ var Foresight = /** @class */ (function () {
* @constructor * @constructor
*/ */
function Foresight(config) { function Foresight(config) {
if (config === void 0) { config = {}; }
var _this = this; var _this = this;
if (config === void 0) { config = {}; }
/** /**
* Stores a mapping of elements with is respective functions to de-register * Stores a mapping of elements with its respective functions to de-register
* listeners. * the listeners.
* @private * @private
*/ */
this._untrackFns = new Map(); this._untrackFns = new Map();
...@@ -141,7 +141,17 @@ var Foresight = /** @class */ (function () { ...@@ -141,7 +141,17 @@ var Foresight = /** @class */ (function () {
* @private * @private
*/ */
this._observer = null; this._observer = null;
this.options = __assign({}, Foresight.defaultOptions, config); /**
* Store the current maximum page scroll amount.
* @private
*/
this._maxScrollY = 0;
/**
* Indicates that the scroll tracker has already been initialised
* @private
*/
this._scrollTrackerInitialised = false;
this.options = __assign(__assign({}, Foresight.defaultOptions), config);
// Initialise IntersectionObserver // Initialise IntersectionObserver
this._observer = new IntersectionObserver(function (entries, observer) { this._observer = new IntersectionObserver(function (entries, observer) {
entries.forEach(function (entry) { entries.forEach(function (entry) {
...@@ -172,6 +182,13 @@ var Foresight = /** @class */ (function () { ...@@ -172,6 +182,13 @@ var Foresight = /** @class */ (function () {
Utils Utils
.toArray(root.querySelectorAll('[data-track], [data-track-view]')) .toArray(root.querySelectorAll('[data-track], [data-track-view]'))
.map(function (element) { return _this.track(element); }); .map(function (element) { return _this.track(element); });
if (!this._scrollTrackerInitialised) {
var maxScrollListener = Utils.debounce(function () { return _this._onScroll(); });
window.addEventListener('scroll', maxScrollListener, { passive: true });
this._onScroll(); // call once
this._scrollTrackerInitialised = true;
}
window.addEventListener('beforeunload', function (e) { return _this._onUnload(e); });
}; };
/** /**
* Start tracking an element for events. * Start tracking an element for events.
...@@ -219,7 +236,7 @@ var Foresight = /** @class */ (function () { ...@@ -219,7 +236,7 @@ var Foresight = /** @class */ (function () {
return this.options.sendEventFn(data); return this.options.sendEventFn(data);
} }
if (typeof window['gtag'] === 'function') { if (typeof window['gtag'] === 'function') {
return window['gtag']('event', data.action, __assign({ 'event_label': data.label, 'event_category': data.category, 'non_interaction': !data.interaction }, data.metrics, data.dimensions)); return window['gtag']('event', data.action, __assign(__assign({ 'event_label': data.label, 'event_category': data.category, 'non_interaction': !data.interaction }, data.metrics), data.dimensions));
} }
if (typeof window['ga'] === 'function') { if (typeof window['ga'] === 'function') {
return window['ga']('send', { return window['ga']('send', {
...@@ -228,7 +245,7 @@ var Foresight = /** @class */ (function () { ...@@ -228,7 +245,7 @@ var Foresight = /** @class */ (function () {
eventAction: data.action, eventAction: data.action,
eventLabel: data.label, eventLabel: data.label,
nonInteraction: !data.interaction, nonInteraction: !data.interaction,
}, __assign({}, data.metrics, data.dimensions)); }, __assign(__assign({}, data.metrics), data.dimensions));
} }
throw 'No available analytics backend found. Has Google Analytics been loaded yet?'; throw 'No available analytics backend found. Has Google Analytics been loaded yet?';
}; };
...@@ -255,7 +272,7 @@ var Foresight = /** @class */ (function () { ...@@ -255,7 +272,7 @@ var Foresight = /** @class */ (function () {
*/ */
Foresight.prototype._trackClicks = function (element) { Foresight.prototype._trackClicks = function (element) {
var _this = this; var _this = this;
// Define listen fucntion // Define listen function
var listener = function (e) { return _this._onTrackedClick(element, e); }; var listener = function (e) { return _this._onTrackedClick(element, e); };
element.addEventListener('click', listener); element.addEventListener('click', listener);
element.addEventListener('auxclick', listener); element.addEventListener('auxclick', listener);
...@@ -355,9 +372,26 @@ var Foresight = /** @class */ (function () { ...@@ -355,9 +372,26 @@ var Foresight = /** @class */ (function () {
this.send(data); this.send(data);
}; };
/** /**
* Default Options * Handles a scroll event
* @static
*/ */
Foresight.prototype._onScroll = function (root) {
if (root === void 0) { root = document.documentElement; }
var vh = Math.max(root.clientHeight, window.innerHeight || 0);
var scrollY = root.scrollTop + vh;
if (scrollY > this._maxScrollY) {
this._maxScrollY = scrollY;
}
};
Foresight.prototype._onUnload = function (e) {
var scrollData = {
category: 'general',
action: 'scroll y',
label: window.location.pathname,
interaction: false,
value: Math.floor((this._maxScrollY / document.body.clientHeight) * 100)
};
this.send(scrollData);
};
Foresight.defaultOptions = { Foresight.defaultOptions = {
defer: false, defer: false,
observerOptions: {}, observerOptions: {},
...@@ -409,7 +443,7 @@ if ('IntersectionObserver' in window && ...@@ -409,7 +443,7 @@ if ('IntersectionObserver' in window &&
/** /**
* An IntersectionObserver registry. This registry exists to hold a strong * An IntersectionObserver registry. This registry exists to hold a strong
* reference to IntersectionObserver instances currently observering a target * reference to IntersectionObserver instances currently observing a target
* element. Without this registry, instances without another reference may be * element. Without this registry, instances without another reference may be
* garbage collected. * garbage collected.
*/ */
...@@ -438,7 +472,9 @@ function IntersectionObserverEntry(entry) { ...@@ -438,7 +472,9 @@ function IntersectionObserverEntry(entry) {
// Sets intersection ratio. // Sets intersection ratio.
if (targetArea) { if (targetArea) {
this.intersectionRatio = intersectionArea / targetArea; // Round the intersection ratio to avoid floating point math issues:
// https://github.com/w3c/IntersectionObserver/issues/324
this.intersectionRatio = Number((intersectionArea / targetArea).toFixed(4));
} else { } else {
// If area is zero and is intersecting, sets to 1, otherwise to 0 // If area is zero and is intersecting, sets to 1, otherwise to 0
this.intersectionRatio = this.isIntersecting ? 1 : 0; this.intersectionRatio = this.isIntersecting ? 1 : 0;
...@@ -626,7 +662,7 @@ IntersectionObserver.prototype._parseRootMargin = function(opt_rootMargin) { ...@@ -626,7 +662,7 @@ IntersectionObserver.prototype._parseRootMargin = function(opt_rootMargin) {
/** /**
* Starts polling for intersection changes if the polling is not already * Starts polling for intersection changes if the polling is not already
* happening, and if the page's visibilty state is visible. * happening, and if the page's visibility state is visible.
* @private * @private
*/ */
IntersectionObserver.prototype._monitorIntersections = function() { IntersectionObserver.prototype._monitorIntersections = function() {
...@@ -928,7 +964,7 @@ function now() { ...@@ -928,7 +964,7 @@ function now() {
/** /**
* Throttles a function and delays its executiong, so it's only called at most * Throttles a function and delays its execution, so it's only called at most
* once within a given time period. * once within a given time period.
* @param {Function} fn The function to throttle. * @param {Function} fn The function to throttle.
* @param {number} timeout The amount of time that must pass before the * @param {number} timeout The amount of time that must pass before the
...@@ -1059,7 +1095,7 @@ function getEmptyRect() { ...@@ -1059,7 +1095,7 @@ function getEmptyRect() {
} }
/** /**
* Checks to see if a parent element contains a child elemnt (including inside * Checks to see if a parent element contains a child element (including inside
* shadow DOM). * shadow DOM).
* @param {Node} parent The parent element. * @param {Node} parent The parent element.
* @param {Node} child The child element. * @param {Node} child The child element.
...@@ -1109,6 +1145,8 @@ window.IntersectionObserverEntry = IntersectionObserverEntry; ...@@ -1109,6 +1145,8 @@ window.IntersectionObserverEntry = IntersectionObserverEntry;
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
var toArray_1 = __webpack_require__(3); var toArray_1 = __webpack_require__(3);
exports.toArray = toArray_1.default; exports.toArray = toArray_1.default;
var debounce_1 = __webpack_require__(4);
exports.debounce = debounce_1.default;
/***/ }), /***/ }),
...@@ -1127,6 +1165,34 @@ function toArray(arrayLike) { ...@@ -1127,6 +1165,34 @@ function toArray(arrayLike) {
exports.default = toArray; exports.default = toArray;
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Returns a debounced function that as long as it continues to be invoked,
* will not be triggered until it stops being called for N milliseconds.
*/
function debounce(func, wait) {
if (wait === void 0) { wait = 300; }
var timeout;
return function () {
var context = this;
var args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = null;
func.apply(context, args);
}, wait);
};
}
exports.debounce = debounce;
exports.default = debounce;
/***/ }) /***/ })
/******/ ]); /******/ ]);
}); });
\ No newline at end of file
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
"webpack-cli": "^3.1.0" "webpack-cli": "^3.1.0"
}, },
"dependencies": { "dependencies": {
"intersection-observer": "^0.5.0" "intersection-observer": "^0.5.0",
"uglifyjs-webpack-plugin": "^2.2.0"
}, },
"scripts": { "scripts": {
"start": "webpack --watch & browser-sync start -s . -f .", "start": "webpack --watch & browser-sync start -s . -f .",
......
/*! /**!
* Foresight * Foresight
* *
* @author James Ooi <james.ooi@forefront.com.my> * @author James Ooi <james.ooi@forefront.com.my>
...@@ -29,6 +29,7 @@ interface EventData { ...@@ -29,6 +29,7 @@ interface EventData {
action: string; action: string;
label: string; label: string;
interaction: boolean; interaction: boolean;
value?: string | number | null | undefined;
metrics?: { metrics?: {
[key: string]: any; [key: string]: any;
}; };
...@@ -40,10 +41,6 @@ interface EventData { ...@@ -40,10 +41,6 @@ interface EventData {
* Foresight is an analytics library that allows for declarative event tracking. * Foresight is an analytics library that allows for declarative event tracking.
*/ */
declare class Foresight { declare class Foresight {
/**
* Default Options
* @static
*/
static defaultOptions: Partial<ForesightConfig>; static defaultOptions: Partial<ForesightConfig>;
/** /**
* Stores the options of the current Foresight instance. * Stores the options of the current Foresight instance.
...@@ -51,8 +48,8 @@ declare class Foresight { ...@@ -51,8 +48,8 @@ declare class Foresight {
*/ */
options: ForesightConfig; options: ForesightConfig;
/** /**
* Stores a mapping of elements with is respective functions to de-register * Stores a mapping of elements with its respective functions to de-register
* listeners. * the listeners.
* @private * @private
*/ */
private _untrackFns; private _untrackFns;
...@@ -62,6 +59,16 @@ declare class Foresight { ...@@ -62,6 +59,16 @@ declare class Foresight {
*/ */
private _observer; private _observer;
/** /**
* Store the current maximum page scroll amount.
* @private
*/
private _maxScrollY;
/**
* Indicates that the scroll tracker has already been initialised
* @private
*/
private _scrollTrackerInitialised;
/**
* @constructor * @constructor
*/ */
constructor(config?: ForesightConfig); constructor(config?: ForesightConfig);
...@@ -113,5 +120,10 @@ declare class Foresight { ...@@ -113,5 +120,10 @@ declare class Foresight {
* @private * @private
*/ */
private _onTrackedView; private _onTrackedView;
/**
* Handles a scroll event
*/
private _onScroll;
private _onUnload;
} }
export = Foresight; export = Foresight;
/**
* Returns a debounced function that as long as it continues to be invoked,
* will not be triggered until it stops being called for N milliseconds.
*/
declare function debounce(func: Function, wait?: number): () => void;
export default debounce;
export { debounce };
export { default as toArray } from './toArray'; export { default as toArray } from './toArray';
export { default as debounce } from './debounce';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment