Commit 41505fa1 by James Ooi

Build latest production builds

parent 48b6bcaa
...@@ -100,6 +100,13 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -100,6 +100,13 @@ return /******/ (function(modules) { // webpackBootstrap
"use strict"; "use strict";
/*!
* Foresight
*
* @author James Ooi <james.ooi@forefront.com.my>
* @license MIT
* @copyright 2018 (c) FOREFRONT International Sdn Bhd
*/
var __assign = (this && this.__assign) || function () { var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) { __assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) { for (var s, i = 1, n = arguments.length; i < n; i++) {
...@@ -113,12 +120,30 @@ var __assign = (this && this.__assign) || function () { ...@@ -113,12 +120,30 @@ var __assign = (this && this.__assign) || function () {
}; };
__webpack_require__(1); __webpack_require__(1);
var Utils = __webpack_require__(2); var Utils = __webpack_require__(2);
var Foresight = (function () { /**
* Foresight is an analytics library that allows for declarative event tracking
* in your websites.
* @class
*/
var Foresight = /** @class */ (function () {
/**
* @constructor
*/
function Foresight(config) { function Foresight(config) {
var _this = this; var _this = this;
/**
* Stores a mapping of elements with is respective functions to de-register
* listeners.
* @private
*/
this._untrackFns = new Map(); this._untrackFns = new Map();
/**
* Stores an instance of an IntersectionObserver.
* @private
*/
this._observer = null; this._observer = null;
this.options = __assign({}, Foresight.defaultOptions, config); this.options = __assign({}, Foresight.defaultOptions, config);
// Initialise IntersectionObserver
this._observer = new IntersectionObserver(function (entries, observer) { this._observer = new IntersectionObserver(function (entries, observer) {
entries.forEach(function (entry) { entries.forEach(function (entry) {
if (entry.isIntersecting) { if (entry.isIntersecting) {
...@@ -127,7 +152,14 @@ var Foresight = (function () { ...@@ -127,7 +152,14 @@ var Foresight = (function () {
} }
}); });
}, this.options.observerOptions); }, this.options.observerOptions);
// Start tracking
if (!this.options.defer) {
this.start();
}
} }
/**
* Start event tracking for all DOM elements with event tracking attributes.
*/
Foresight.prototype.start = function (root) { Foresight.prototype.start = function (root) {
var _this = this; var _this = this;
if (root === void 0) { root = document.body; } if (root === void 0) { root = document.body; }
...@@ -138,16 +170,21 @@ var Foresight = (function () { ...@@ -138,16 +170,21 @@ var Foresight = (function () {
.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); });
}; };
/**
* Enables event tracking for a DOM element with event tracking attribute.
*/
Foresight.prototype.track = function (element) { Foresight.prototype.track = function (element) {
if (!this._untrackFns.has(element)) { if (!this._untrackFns.has(element)) {
this._untrackFns.set(element, { click: null, view: null }); this._untrackFns.set(element, { click: null, view: null });
} }
var untrackFn = this._untrackFns.get(element); var untrackFn = this._untrackFns.get(element);
// Track clicks
if (element.getAttribute('data-track') !== null) { if (element.getAttribute('data-track') !== null) {
if (untrackFn.click == null) { if (untrackFn.click == null) {
untrackFn.click = this._trackClicks(element); untrackFn.click = this._trackClicks(element);
} }
} }
// Track views
if (element.getAttribute('data-track-view') !== null) { if (element.getAttribute('data-track-view') !== null) {
if (untrackFn.view == null) { if (untrackFn.view == null) {
untrackFn.view = this._trackViews(element); untrackFn.view = this._trackViews(element);
...@@ -155,6 +192,9 @@ var Foresight = (function () { ...@@ -155,6 +192,9 @@ var Foresight = (function () {
} }
this._untrackFns.set(element, untrackFn); this._untrackFns.set(element, untrackFn);
}; };
/**
* Disable event tracking for a DOM element.
*/
Foresight.prototype.untrack = function (element) { Foresight.prototype.untrack = function (element) {
var untrackFn = this._untrackFns.get(element); var untrackFn = this._untrackFns.get(element);
if (untrackFn === undefined) { if (untrackFn === undefined) {
...@@ -168,17 +208,30 @@ var Foresight = (function () { ...@@ -168,17 +208,30 @@ var Foresight = (function () {
} }
this._untrackFns.delete(element); this._untrackFns.delete(element);
}; };
/**
* Parse an event string and returns a `EventData` object.
* @private
*/
Foresight.prototype._parseEventString = function (eventString) { Foresight.prototype._parseEventString = function (eventString) {
var split = eventString.split(';'); var split = eventString.split(';');
var category = split[0], action = split[1], label = split[2]; var category = split[0], action = split[1], label = split[2];
// If only one argument is provided, then the argument is the action
if (split.length === 1) { if (split.length === 1) {
action = category; action = category;
category = undefined; category = undefined;
} }
return { category: category, action: action, label: label, interaction: true }; return { category: category, action: action, label: label, interaction: true };
}; };
/**
* Registers click listeners that triggers an analytics event when the element
* is clicked or middle clicked.
*
* @returns Returns a function to remove the event listener.
* @private
*/
Foresight.prototype._trackClicks = function (element) { Foresight.prototype._trackClicks = function (element) {
var _this = this; var _this = this;
// Define listen fucntion
var listener = function (e) { var listener = function (e) {
_this._onTrackedClick(element, e); _this._onTrackedClick(element, e);
}; };
...@@ -189,6 +242,13 @@ var Foresight = (function () { ...@@ -189,6 +242,13 @@ var Foresight = (function () {
element.removeEventListener('auxclick', listener); element.removeEventListener('auxclick', listener);
}; };
}; };
/**
* Registers a view observer that triggers an analytics event when the element
* is in view.
*
* @returns Returns a function that disconnects the view observer.
* @private
*/
Foresight.prototype._trackViews = function (element) { Foresight.prototype._trackViews = function (element) {
var _this = this; var _this = this;
this._observer.observe(element); this._observer.observe(element);
...@@ -196,10 +256,14 @@ var Foresight = (function () { ...@@ -196,10 +256,14 @@ var Foresight = (function () {
_this._observer.unobserve(element); _this._observer.unobserve(element);
}; };
}; };
/**
* Handles a click event on an element that is being tracked by Foresight.
* @private
*/
Foresight.prototype._onTrackedClick = function (element, event) { Foresight.prototype._onTrackedClick = function (element, event) {
var s = element.getAttribute('data-track'); var s = element.getAttribute('data-track');
var data = this._parseEventString(s); var data = this._parseEventString(s);
data.interaction = !this.options.nonInteractionClicks; data.interaction = this.options.clicksAreInteractions;
if (element.getAttribute('data-track:non-interaction') !== null) { if (element.getAttribute('data-track:non-interaction') !== null) {
data.interaction = false; data.interaction = false;
} }
...@@ -209,10 +273,14 @@ var Foresight = (function () { ...@@ -209,10 +273,14 @@ var Foresight = (function () {
'non_interaction': !data.interaction, 'non_interaction': !data.interaction,
}); });
}; };
/**
* Handles a view event on an element that is being tracked by Foresight.
* @private
*/
Foresight.prototype._onTrackedView = function (element, observer) { Foresight.prototype._onTrackedView = function (element, observer) {
var s = element.getAttribute('data-track-view'); var s = element.getAttribute('data-track-view');
var data = this._parseEventString(s); var data = this._parseEventString(s);
data.interaction = !this.options.nonInteractionViews; data.interaction = this.options.viewsAreInteractions;
if (element.getAttribute('data-track-view:interaction') !== null) { if (element.getAttribute('data-track-view:interaction') !== null) {
data.interaction = true; data.interaction = true;
} }
...@@ -222,9 +290,14 @@ var Foresight = (function () { ...@@ -222,9 +290,14 @@ var Foresight = (function () {
'non_interaction': !data.interaction, 'non_interaction': !data.interaction,
}); });
}; };
/**
* Default Options
* @static
*/
Foresight.defaultOptions = { Foresight.defaultOptions = {
nonInteractionClicks: false, defer: false,
nonInteractionViews: true, clicksAreInteractions: true,
viewsAreInteractions: false,
observerOptions: {}, observerOptions: {},
}; };
return Foresight; return Foresight;
...@@ -980,6 +1053,9 @@ exports.toArray = toArray_1.default; ...@@ -980,6 +1053,9 @@ exports.toArray = toArray_1.default;
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
/**
* Creates a new `Array` instance from an array-like or iterable object.
*/
function toArray(arrayLike) { function toArray(arrayLike) {
return [].slice.call(arrayLike); return [].slice.call(arrayLike);
} }
......
/*!
* Foresight
*
* @author James Ooi <james.ooi@forefront.com.my>
* @license MIT
* @copyright 2018 (c) FOREFRONT International Sdn Bhd
*/
import 'intersection-observer'; import 'intersection-observer';
/**
* Available options for configuring Foresight.
*/
interface ForesightConfig { interface ForesightConfig {
/** Defer tracking initialisation. */
defer?: boolean;
/** Configure the intersection observer. */
observerOptions?: IntersectionObserverInit; observerOptions?: IntersectionObserverInit;
nonInteractionClicks?: boolean; /** Treat clicks as an interactive event. Defaults to true. */
nonInteractionViews?: boolean; clicksAreInteractions?: boolean;
/** Treat views as an interactive event. Defaults to false. */
viewsAreInteractions?: boolean;
} }
/**
* Foresight is an analytics library that allows for declarative event tracking
* in your websites.
* @class
*/
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.
* @public
*/
options: ForesightConfig; options: ForesightConfig;
/**
* Stores a mapping of elements with is respective functions to de-register
* listeners.
* @private
*/
private _untrackFns; private _untrackFns;
/**
* Stores an instance of an IntersectionObserver.
* @private
*/
private _observer; private _observer;
/**
* @constructor
*/
constructor(config: ForesightConfig); constructor(config: ForesightConfig);
/**
* Start event tracking for all DOM elements with event tracking attributes.
*/
start(root?: Element): void; start(root?: Element): void;
/**
* Enables event tracking for a DOM element with event tracking attribute.
*/
track(element: Element): void; track(element: Element): void;
/**
* Disable event tracking for a DOM element.
*/
untrack(element: Element): void; untrack(element: Element): void;
/**
* Parse an event string and returns a `EventData` object.
* @private
*/
private _parseEventString; private _parseEventString;
/**
* Registers click listeners that triggers an analytics event when the element
* is clicked or middle clicked.
*
* @returns Returns a function to remove the event listener.
* @private
*/
private _trackClicks; private _trackClicks;
/**
* Registers a view observer that triggers an analytics event when the element
* is in view.
*
* @returns Returns a function that disconnects the view observer.
* @private
*/
private _trackViews; private _trackViews;
/**
* Handles a click event on an element that is being tracked by Foresight.
* @private
*/
private _onTrackedClick; private _onTrackedClick;
/**
* Handles a view event on an element that is being tracked by Foresight.
* @private
*/
private _onTrackedView; private _onTrackedView;
} }
export = Foresight; export = Foresight;
/**
* Creates a new `Array` instance from an array-like or iterable object.
*/
export default function toArray<T>(arrayLike: any): T[]; export default function toArray<T>(arrayLike: any): T[];
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