Commit 6927379a by Chris

feat: optimise for GA4, add sendPageView for element clicked

parent 30d90232
...@@ -239,7 +239,7 @@ var Foresight = /** @class */ (function () { ...@@ -239,7 +239,7 @@ var Foresight = /** @class */ (function () {
return window['gtm'](__assign(__assign({ 'event': data.action, 'event_label': data.label, 'event_category': data.category, 'non_interaction': !data.interaction }, data.metrics), data.dimensions)); return window['gtm'](__assign(__assign({ 'event': data.action, 'event_label': data.label, 'event_category': data.category, 'non_interaction': !data.interaction }, data.metrics), data.dimensions));
} }
if (typeof window['gtag'] === 'function') { if (typeof window['gtag'] === 'function') {
return window['gtag']('event', data.action, __assign(__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(__assign(__assign(__assign({}, data.metrics), data.dimensions), (data.label ? { label: data.label } : {})), (data.category ? { category: data.category } : {})), { 'non_interaction': !data.interaction }));
} }
if (typeof window['ga'] === 'function') { if (typeof window['ga'] === 'function') {
return window['ga']('send', { return window['ga']('send', {
...@@ -335,6 +335,10 @@ var Foresight = /** @class */ (function () { ...@@ -335,6 +335,10 @@ var Foresight = /** @class */ (function () {
data.interaction = false; data.interaction = false;
} }
this.send(data); this.send(data);
if (element.hasAttribute('data-track:page-view')) {
var pagePath = element.getAttribute('data-track:page-view') || window.location.pathname;
this.sendPageView(pagePath);
}
}; };
/** /**
* 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.
...@@ -395,6 +399,18 @@ var Foresight = /** @class */ (function () { ...@@ -395,6 +399,18 @@ var Foresight = /** @class */ (function () {
}; };
this.send(scrollData); this.send(scrollData);
}; };
/**
* Send a page view event manually.
* @param {string} pagePath The page path to send. Defaults to current location.
*/
Foresight.prototype.sendPageView = function (pagePath) {
if (pagePath === void 0) { pagePath = window.location.pathname; }
if (typeof window['gtag'] === 'function') {
window['gtag']('event', 'page_view', {
page_path: pagePath
});
}
};
Foresight.defaultOptions = { Foresight.defaultOptions = {
defer: false, defer: false,
observerOptions: {}, observerOptions: {},
...@@ -1147,9 +1163,9 @@ window.IntersectionObserverEntry = IntersectionObserverEntry; ...@@ -1147,9 +1163,9 @@ window.IntersectionObserverEntry = IntersectionObserverEntry;
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
var toArray_1 = __webpack_require__(3); var toArray_1 = __webpack_require__(3);
exports.toArray = toArray_1.default; Object.defineProperty(exports, "toArray", { enumerable: true, get: function () { return toArray_1.default; } });
var debounce_1 = __webpack_require__(4); var debounce_1 = __webpack_require__(4);
exports.debounce = debounce_1.default; Object.defineProperty(exports, "debounce", { enumerable: true, get: function () { return debounce_1.default; } });
/***/ }), /***/ }),
...@@ -1175,6 +1191,7 @@ exports.default = toArray; ...@@ -1175,6 +1191,7 @@ exports.default = toArray;
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.debounce = void 0;
/** /**
* Returns a debounced function that as long as it continues to be invoked, * 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. * will not be triggered until it stops being called for N milliseconds.
......
...@@ -204,11 +204,11 @@ class Foresight { ...@@ -204,11 +204,11 @@ class Foresight {
if (typeof window['gtag'] === 'function') { if (typeof window['gtag'] === 'function') {
return window['gtag']('event', data.action, { return window['gtag']('event', data.action, {
'event_label': data.label,
'event_category': data.category,
'non_interaction': !data.interaction,
...data.metrics, ...data.metrics,
...data.dimensions ...data.dimensions,
...(data.label ? { label: data.label } : {}),
...(data.category ? { category: data.category } : {}),
'non_interaction': !data.interaction
}); });
} }
...@@ -320,6 +320,11 @@ class Foresight { ...@@ -320,6 +320,11 @@ class Foresight {
} }
this.send(<EventData> data); this.send(<EventData> data);
if (element.hasAttribute('data-track:page-view')) {
const pagePath = element.getAttribute('data-track:page-view') || window.location.pathname;
this.sendPageView(pagePath);
}
} }
/** /**
...@@ -386,6 +391,18 @@ class Foresight { ...@@ -386,6 +391,18 @@ class Foresight {
} }
this.send(scrollData); this.send(scrollData);
} }
/**
* Send a page view event manually.
* @param {string} pagePath The page path to send. Defaults to current location.
*/
sendPageView(pagePath: string = window.location.pathname) {
if (typeof window['gtag'] === 'function') {
window['gtag']('event', 'page_view', {
page_path: pagePath
});
}
}
} }
export = Foresight; export = Foresight;
...@@ -125,5 +125,10 @@ declare class Foresight { ...@@ -125,5 +125,10 @@ declare class Foresight {
*/ */
private _onScroll; private _onScroll;
private _onUnload; private _onUnload;
/**
* Send a page view event manually.
* @param {string} pagePath The page path to send. Defaults to current location.
*/
sendPageView(pagePath?: string): void;
} }
export = Foresight; export = Foresight;
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