Commit 48b6bcaa by James Ooi

Implement auto track on initialisation

parent ba4e3ef3
/**
/*!
* Foresight
*
* @author James Ooi <james.ooi@forefront.com.my>
......@@ -16,11 +16,18 @@ declare var gtag: (action: string, ...args: any[]) => any;
* Available options for configuring Foresight.
*/
interface ForesightConfig {
/** Defer tracking initialisation. */
defer?: boolean
/** Configure the intersection observer. */
observerOptions?: IntersectionObserverInit
nonInteractionClicks?: boolean
nonInteractionViews?: boolean
}
/** Treat clicks as an interactive event. Defaults to true. */
clicksAreInteractions?: boolean
/** Treat views as an interactive event. Defaults to false. */
viewsAreInteractions?: boolean
}
/**
* Represents a particular event's data.
......@@ -45,8 +52,9 @@ class Foresight {
* @static
*/
static defaultOptions: Partial<ForesightConfig> = {
nonInteractionClicks: false,
nonInteractionViews: true,
defer: false,
clicksAreInteractions: true,
viewsAreInteractions: false,
observerOptions: {},
}
......@@ -84,6 +92,11 @@ class Foresight {
}
});
}, this.options.observerOptions);
// Start tracking
if (!this.options.defer) {
this.start();
}
}
/**
......@@ -211,7 +224,7 @@ class Foresight {
const s = element.getAttribute('data-track');
const data = this._parseEventString(s);
data.interaction = !this.options.nonInteractionClicks;
data.interaction = this.options.clicksAreInteractions;
if (element.getAttribute('data-track:non-interaction') !== null) {
data.interaction = false;
}
......@@ -231,7 +244,7 @@ class Foresight {
const s = element.getAttribute('data-track-view');
const data = this._parseEventString(s);
data.interaction = !this.options.nonInteractionViews;
data.interaction = this.options.viewsAreInteractions;
if (element.getAttribute('data-track-view:interaction') !== null) {
data.interaction = true;
}
......
......@@ -3,7 +3,6 @@
"target": "es5",
"allowJs": false,
"noImplicitAny": false,
"removeComments": true,
"lib": ["es6", "dom"]
},
"exclude": [
......
......@@ -5,7 +5,6 @@
"declarationDir": "./typings",
"allowJs": false,
"noImplicitAny": false,
"removeComments": true,
"lib": ["es6", "dom"]
},
"exclude": [
......
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