Commit 6f772644 by Chris

update dist to latest version

parent 1c89f3aa
......@@ -100,7 +100,7 @@ return /******/ (function(modules) { // webpackBootstrap
"use strict";
/*!
/**!
* Foresight
*
* @author James Ooi <james.ooi@forefront.com.my>
......@@ -128,11 +128,11 @@ var Foresight = /** @class */ (function () {
* @constructor
*/
function Foresight(config) {
if (config === void 0) { config = {}; }
var _this = this;
if (config === void 0) { config = {}; }
/**
* Stores a mapping of elements with is respective functions to de-register
* listeners.
* Stores a mapping of elements with its respective functions to de-register
* the listeners.
* @private
*/
this._untrackFns = new Map();
......@@ -141,7 +141,17 @@ var Foresight = /** @class */ (function () {
* @private
*/
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
this._observer = new IntersectionObserver(function (entries, observer) {
entries.forEach(function (entry) {
......@@ -172,6 +182,13 @@ var Foresight = /** @class */ (function () {
Utils
.toArray(root.querySelectorAll('[data-track], [data-track-view]'))
.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.
......@@ -219,7 +236,7 @@ var Foresight = /** @class */ (function () {
return this.options.sendEventFn(data);
}
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') {
return window['ga']('send', {
......@@ -228,7 +245,7 @@ var Foresight = /** @class */ (function () {
eventAction: data.action,
eventLabel: data.label,
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?';
};
......@@ -255,7 +272,7 @@ var Foresight = /** @class */ (function () {
*/
Foresight.prototype._trackClicks = function (element) {
var _this = this;
// Define listen fucntion
// Define listen function
var listener = function (e) { return _this._onTrackedClick(element, e); };
element.addEventListener('click', listener);
element.addEventListener('auxclick', listener);
......@@ -355,9 +372,26 @@ var Foresight = /** @class */ (function () {
this.send(data);
};
/**
* Default Options
* @static
* Handles a scroll event
*/
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 = {
defer: false,
observerOptions: {},
......@@ -409,7 +443,7 @@ if ('IntersectionObserver' in window &&
/**
* 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
* garbage collected.
*/
......@@ -438,7 +472,9 @@ function IntersectionObserverEntry(entry) {
// Sets intersection ratio.
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 {
// If area is zero and is intersecting, sets to 1, otherwise to 0
this.intersectionRatio = this.isIntersecting ? 1 : 0;
......@@ -626,7 +662,7 @@ IntersectionObserver.prototype._parseRootMargin = function(opt_rootMargin) {
/**
* 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
*/
IntersectionObserver.prototype._monitorIntersections = function() {
......@@ -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.
* @param {Function} fn The function to throttle.
* @param {number} timeout The amount of time that must pass before the
......@@ -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).
* @param {Node} parent The parent element.
* @param {Node} child The child element.
......@@ -1109,6 +1145,8 @@ window.IntersectionObserverEntry = IntersectionObserverEntry;
Object.defineProperty(exports, "__esModule", { value: true });
var toArray_1 = __webpack_require__(3);
exports.toArray = toArray_1.default;
var debounce_1 = __webpack_require__(4);
exports.debounce = debounce_1.default;
/***/ }),
......@@ -1127,6 +1165,34 @@ function toArray(arrayLike) {
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 @@
"webpack-cli": "^3.1.0"
},
"dependencies": {
"intersection-observer": "^0.5.0"
"intersection-observer": "^0.5.0",
"uglifyjs-webpack-plugin": "^2.2.0"
},
"scripts": {
"start": "webpack --watch & browser-sync start -s . -f .",
......
/*!
/**!
* Foresight
*
* @author James Ooi <james.ooi@forefront.com.my>
......@@ -29,6 +29,7 @@ interface EventData {
action: string;
label: string;
interaction: boolean;
value?: string | number | null | undefined;
metrics?: {
[key: string]: any;
};
......@@ -40,10 +41,6 @@ interface EventData {
* Foresight is an analytics library that allows for declarative event tracking.
*/
declare class Foresight {
/**
* Default Options
* @static
*/
static defaultOptions: Partial<ForesightConfig>;
/**
* Stores the options of the current Foresight instance.
......@@ -51,8 +48,8 @@ declare class Foresight {
*/
options: ForesightConfig;
/**
* Stores a mapping of elements with is respective functions to de-register
* listeners.
* Stores a mapping of elements with its respective functions to de-register
* the listeners.
* @private
*/
private _untrackFns;
......@@ -62,6 +59,16 @@ declare class Foresight {
*/
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(config?: ForesightConfig);
......@@ -113,5 +120,10 @@ declare class Foresight {
* @private
*/
private _onTrackedView;
/**
* Handles a scroll event
*/
private _onScroll;
private _onUnload;
}
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 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