Commit 48b6bcaa by James Ooi

Implement auto track on initialisation

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