Commit 2129c69f by James Ooi

Add ability to override send event function

parent 370a70de
# Foresight # Foresight
### Foresight is an analytics library that allows for declarative event tracking in your websites. ### Foresight is an analytics library that allows for declarative event tracking.
* Lightweight (<3kb minified and gzipped) * Lightweight (<3kb minified and gzipped)
* Declarative event tracking * Declarative event tracking
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
### Direct download ### Direct download
Download and install the latest distribution file from this Download and install the latest distribution file from this
Git repository and load it in your webpage. For production cases, we encourage you to download the minified version ([`foresight.min.js`](#)) Git repository and load it in your webpage. For production cases, we encourage you to download the minified version ([`foresight.min.js`](blob/master/dist/foresight.min.js))
```html ```html
<script src="/path/to/foresight.min.js"></script> <script src="/path/to/foresight.min.js"></script>
...@@ -94,19 +94,19 @@ The above markup would send the following event data when the `div` enters the v ...@@ -94,19 +94,19 @@ The above markup would send the following event data when the `div` enters the v
## Configuration ## Configuration
```ts ```js
{ {
/** Defer tracking initialisation. */ /** Defer tracking initialisation. */
defer?: boolean defer: false
/** Configure the intersection observer. */ /** Configure the intersection observer. */
observerOptions?: IntersectionObserverConfig observerOptions: { /* Refer to Intersection Observer documentation */ }
/** Treat clicks as an interactive event. Defaults to `true`. */ /** Treat clicks as an interactive event. Defaults to `true`. */
clicksAreInteractions?: boolean clicksAreInteractions: true
/** Treat views as an interactive event. Defaults to `false`. */ /** Treat views as an interactive event. Defaults to `false`. */
viewsAreInteractions?: boolean viewsAreInteractions: false
} }
``` ```
...@@ -140,4 +140,16 @@ Parameters: ...@@ -140,4 +140,16 @@ Parameters:
--- ---
#### `send(data: EventData)`
Manually send an analytics event data.
Parameters:
* `data: EventData` An object containing the event data with the following keys:
* `category: string`
* `action: string`
* `label: string`
* `interaction: boolean`
---
Copyright © 2018 FOREFRONT International Sdn Bhd. Copyright © 2018 FOREFRONT International Sdn Bhd.
\ No newline at end of file
...@@ -121,8 +121,7 @@ var __assign = (this && this.__assign) || function () { ...@@ -121,8 +121,7 @@ var __assign = (this && this.__assign) || function () {
__webpack_require__(1); __webpack_require__(1);
var Utils = __webpack_require__(2); var Utils = __webpack_require__(2);
/** /**
* Foresight is an analytics library that allows for declarative event tracking * Foresight is an analytics library that allows for declarative event tracking.
* in your websites.
* @class * @class
*/ */
var Foresight = /** @class */ (function () { var Foresight = /** @class */ (function () {
...@@ -130,6 +129,7 @@ var Foresight = /** @class */ (function () { ...@@ -130,6 +129,7 @@ var Foresight = /** @class */ (function () {
* @constructor * @constructor
*/ */
function Foresight(config) { function Foresight(config) {
if (config === void 0) { config = {}; }
var _this = this; var _this = this;
/** /**
* Stores a mapping of elements with is respective functions to de-register * Stores a mapping of elements with is respective functions to de-register
...@@ -158,20 +158,18 @@ var Foresight = /** @class */ (function () { ...@@ -158,20 +158,18 @@ var Foresight = /** @class */ (function () {
} }
} }
/** /**
* Start event tracking for all DOM elements with event tracking attributes. * Start tracking all elements with tracking attributes for events.
* @param {Element} root The container to scan for elements. Defaults to `body`.
*/ */
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; }
if (gtag === undefined) {
throw "The 'gtag' function is undefined. Has Google Analytics been loaded yet?";
}
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); });
}; };
/** /**
* Enables event tracking for a DOM element with event tracking attribute. * Start tracking an element for events.
*/ */
Foresight.prototype.track = function (element) { Foresight.prototype.track = function (element) {
if (!this._untrackFns.has(element)) { if (!this._untrackFns.has(element)) {
...@@ -193,7 +191,7 @@ var Foresight = /** @class */ (function () { ...@@ -193,7 +191,7 @@ var Foresight = /** @class */ (function () {
this._untrackFns.set(element, untrackFn); this._untrackFns.set(element, untrackFn);
}; };
/** /**
* Disable event tracking for a DOM element. * Stop tracking an element for events.
*/ */
Foresight.prototype.untrack = function (element) { Foresight.prototype.untrack = function (element) {
var untrackFn = this._untrackFns.get(element); var untrackFn = this._untrackFns.get(element);
...@@ -209,6 +207,31 @@ var Foresight = /** @class */ (function () { ...@@ -209,6 +207,31 @@ var Foresight = /** @class */ (function () {
this._untrackFns.delete(element); this._untrackFns.delete(element);
}; };
/** /**
* Manually send an analytics event data.
*/
Foresight.prototype.send = function (data) {
if (this.options.sendEventFn !== null) {
return this.options.sendEventFn(data);
}
if (typeof window['gtag'] === 'function') {
return window['gtag']('event', data.action, {
'event_label': data.label,
'event_category': data.category,
'non_interaction': !data.interaction,
});
}
if (typeof window['ga'] === 'function') {
return window['ga']('send', {
hitType: 'event',
eventCategory: data.category,
eventAction: data.action,
eventLabel: data.label,
nonInteraction: !data.interaction,
});
}
throw 'No available analytics backend found. Has Google Analytics been loaded yet?';
};
/**
* Parse an event string and returns a `EventData` object. * Parse an event string and returns a `EventData` object.
* @private * @private
*/ */
...@@ -267,11 +290,7 @@ var Foresight = /** @class */ (function () { ...@@ -267,11 +290,7 @@ var Foresight = /** @class */ (function () {
if (element.getAttribute('data-track:non-interaction') !== null) { if (element.getAttribute('data-track:non-interaction') !== null) {
data.interaction = false; data.interaction = false;
} }
gtag('event', data.action, { this.send(data);
'event_label': data.label,
'event_category': data.category,
'non_interaction': !data.interaction,
});
}; };
/** /**
* Handles a view event on an element that is being tracked by Foresight. * Handles a view event on an element that is being tracked by Foresight.
...@@ -284,11 +303,7 @@ var Foresight = /** @class */ (function () { ...@@ -284,11 +303,7 @@ var Foresight = /** @class */ (function () {
if (element.getAttribute('data-track-view:interaction') !== null) { if (element.getAttribute('data-track-view:interaction') !== null) {
data.interaction = true; data.interaction = true;
} }
gtag('event', data.action, { this.send(data);
'event_label': data.label,
'event_category': data.category,
'non_interaction': !data.interaction,
});
}; };
/** /**
* Default Options * Default Options
...@@ -296,9 +311,10 @@ var Foresight = /** @class */ (function () { ...@@ -296,9 +311,10 @@ var Foresight = /** @class */ (function () {
*/ */
Foresight.defaultOptions = { Foresight.defaultOptions = {
defer: false, defer: false,
observerOptions: {},
clicksAreInteractions: true, clicksAreInteractions: true,
viewsAreInteractions: false, viewsAreInteractions: false,
observerOptions: {}, sendEventFn: null,
}; };
return Foresight; return Foresight;
}()); }());
......
...@@ -27,6 +27,9 @@ interface ForesightConfig { ...@@ -27,6 +27,9 @@ interface ForesightConfig {
/** Treat views as an interactive event. Defaults to `false`. */ /** Treat views as an interactive event. Defaults to `false`. */
viewsAreInteractions?: boolean viewsAreInteractions?: boolean
/** Override the default send event function. */
sendEventFn?: (data: EventData) => any
} }
/** /**
...@@ -41,8 +44,7 @@ interface EventData { ...@@ -41,8 +44,7 @@ interface EventData {
/** /**
* Foresight is an analytics library that allows for declarative event tracking * Foresight is an analytics library that allows for declarative event tracking.
* in your websites.
* @class * @class
*/ */
class Foresight { class Foresight {
...@@ -56,6 +58,7 @@ class Foresight { ...@@ -56,6 +58,7 @@ class Foresight {
observerOptions: {}, observerOptions: {},
clicksAreInteractions: true, clicksAreInteractions: true,
viewsAreInteractions: false, viewsAreInteractions: false,
sendEventFn: null,
} }
/** /**
...@@ -104,10 +107,6 @@ class Foresight { ...@@ -104,10 +107,6 @@ class Foresight {
* @param {Element} root The container to scan for elements. Defaults to `body`. * @param {Element} root The container to scan for elements. Defaults to `body`.
*/ */
start(root: Element = document.body) { start(root: Element = document.body) {
if (gtag === undefined) {
throw `The 'gtag' function is undefined. Has Google Analytics been loaded yet?`;
}
Utils Utils
.toArray<Element>(root.querySelectorAll('[data-track], [data-track-view]')) .toArray<Element>(root.querySelectorAll('[data-track], [data-track-view]'))
.map(element => this.track(element)); .map(element => this.track(element));
...@@ -162,6 +161,35 @@ class Foresight { ...@@ -162,6 +161,35 @@ class Foresight {
} }
/** /**
* Manually send an analytics event data.
*/
send(data: EventData) {
if (this.options.sendEventFn !== null) {
return this.options.sendEventFn(data);
}
if (typeof window['gtag'] === 'function') {
return window['gtag']('event', data.action, {
'event_label': data.label,
'event_category': data.category,
'non_interaction': !data.interaction,
});
}
if (typeof window['ga'] === 'function') {
return window['ga']('send', {
hitType: 'event',
eventCategory: data.category,
eventAction: data.action,
eventLabel: data.label,
nonInteraction: !data.interaction,
})
}
throw 'No available analytics backend found. Has Google Analytics been loaded yet?';
}
/**
* Parse an event string and returns a `EventData` object. * Parse an event string and returns a `EventData` object.
* @private * @private
*/ */
...@@ -230,11 +258,7 @@ class Foresight { ...@@ -230,11 +258,7 @@ class Foresight {
data.interaction = false; data.interaction = false;
} }
gtag('event', data.action, { this.send(data);
'event_label': data.label,
'event_category': data.category,
'non_interaction': !data.interaction,
});
} }
/** /**
...@@ -250,11 +274,7 @@ class Foresight { ...@@ -250,11 +274,7 @@ class Foresight {
data.interaction = true; data.interaction = true;
} }
gtag('event', data.action, { this.send(data);
'event_label': data.label,
'event_category': data.category,
'non_interaction': !data.interaction,
});
} }
} }
......
...@@ -14,14 +14,24 @@ interface ForesightConfig { ...@@ -14,14 +14,24 @@ interface ForesightConfig {
defer?: boolean; defer?: boolean;
/** Configure the intersection observer. */ /** Configure the intersection observer. */
observerOptions?: IntersectionObserverInit; observerOptions?: IntersectionObserverInit;
/** Treat clicks as an interactive event. Defaults to true. */ /** Treat clicks as an interactive event. Defaults to `true`. */
clicksAreInteractions?: boolean; clicksAreInteractions?: boolean;
/** Treat views as an interactive event. Defaults to false. */ /** Treat views as an interactive event. Defaults to `false`. */
viewsAreInteractions?: boolean; viewsAreInteractions?: boolean;
/** Override the default send event function. */
sendEventFn?: (data: EventData) => any;
} }
/** /**
* Foresight is an analytics library that allows for declarative event tracking * Represents a particular event's data.
* in your websites. */
interface EventData {
category: string;
action: string;
label: string;
interaction: boolean;
}
/**
* Foresight is an analytics library that allows for declarative event tracking.
* @class * @class
*/ */
declare class Foresight { declare class Foresight {
...@@ -49,20 +59,25 @@ declare class Foresight { ...@@ -49,20 +59,25 @@ declare class Foresight {
/** /**
* @constructor * @constructor
*/ */
constructor(config: ForesightConfig); constructor(config?: ForesightConfig);
/** /**
* Start event tracking for all DOM elements with event tracking attributes. * Start tracking all elements with tracking attributes for events.
* @param {Element} root The container to scan for elements. Defaults to `body`.
*/ */
start(root?: Element): void; start(root?: Element): void;
/** /**
* Enables event tracking for a DOM element with event tracking attribute. * Start tracking an element for events.
*/ */
track(element: Element): void; track(element: Element): void;
/** /**
* Disable event tracking for a DOM element. * Stop tracking an element for events.
*/ */
untrack(element: Element): void; untrack(element: Element): void;
/** /**
* Manually send an analytics event data.
*/
send(data: EventData): any;
/**
* Parse an event string and returns a `EventData` object. * Parse an event string and returns a `EventData` object.
* @private * @private
*/ */
......
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