Commit f8ff1969 authored by Doug Stull's avatar Doug Stull Committed by Enrique Alcántara

Add DOMContentLoaded event listener for snowplow tracking

- otherwise the mounted events will not fire correctly.
parent 2178384e
import leaveByUrl from '~/namespaces/leave_by_url'; import leaveByUrl from '~/namespaces/leave_by_url';
import initGroupDetails from '../shared/group_details'; import initGroupDetails from '../shared/group_details';
document.addEventListener('DOMContentLoaded', () => { leaveByUrl('group');
leaveByUrl('group'); initGroupDetails();
initGroupDetails();
});
...@@ -61,30 +61,51 @@ const eventHandlers = (category, func) => { ...@@ -61,30 +61,51 @@ const eventHandlers = (category, func) => {
return handlers; return handlers;
}; };
const dispatchEvent = (category = document.body.dataset.page, action = 'generic', data = {}) => {
// eslint-disable-next-line @gitlab/require-i18n-strings
if (!category) throw new Error('Tracking: no category provided for tracking.');
const { label, property, value } = data;
const contexts = [STANDARD_CONTEXT];
if (data.context) {
contexts.push(data.context);
}
return window.snowplow('trackStructEvent', category, action, label, property, value, contexts);
};
export default class Tracking { export default class Tracking {
static queuedEvents = [];
static initialized = false;
static trackable() { static trackable() {
return !['1', 'yes'].includes( return !['1', 'yes'].includes(
window.doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack, window.doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack,
); );
} }
static flushPendingEvents() {
this.initialized = true;
while (this.queuedEvents.length) {
dispatchEvent(...this.queuedEvents.shift());
}
}
static enabled() { static enabled() {
return typeof window.snowplow === 'function' && this.trackable(); return typeof window.snowplow === 'function' && this.trackable();
} }
static event(category = document.body.dataset.page, action = 'generic', data = {}) { static event(...eventData) {
if (!this.enabled()) return false; if (!this.enabled()) return false;
// eslint-disable-next-line @gitlab/require-i18n-strings
if (!category) throw new Error('Tracking: no category provided for tracking.');
const { label, property, value } = data;
const contexts = [STANDARD_CONTEXT];
if (data.context) { if (!this.initialized) {
contexts.push(data.context); this.queuedEvents.push(eventData);
return false;
} }
return window.snowplow('trackStructEvent', category, action, label, property, value, contexts); return dispatchEvent(...eventData);
} }
static bindDocument(category = document.body.dataset.page, parent = document) { static bindDocument(category = document.body.dataset.page, parent = document) {
...@@ -143,6 +164,7 @@ export function initUserTracking() { ...@@ -143,6 +164,7 @@ export function initUserTracking() {
window.snowplow('newTracker', opts.namespace, opts.hostname, opts); window.snowplow('newTracker', opts.namespace, opts.hostname, opts);
document.dispatchEvent(new Event('SnowplowInitialized')); document.dispatchEvent(new Event('SnowplowInitialized'));
Tracking.flushPendingEvents();
} }
export function initDefaultTrackers() { export function initDefaultTrackers() {
......
...@@ -149,6 +149,27 @@ describe('Tracking', () => { ...@@ -149,6 +149,27 @@ describe('Tracking', () => {
}); });
}); });
describe('.flushPendingEvents', () => {
it('flushes any pending events', () => {
Tracking.initialized = false;
Tracking.event('_category_', '_eventName_', { label: '_label_' });
expect(snowplowSpy).not.toHaveBeenCalled();
Tracking.flushPendingEvents();
expect(snowplowSpy).toHaveBeenCalledWith(
'trackStructEvent',
'_category_',
'_eventName_',
'_label_',
undefined,
undefined,
[STANDARD_CONTEXT],
);
});
});
describe('tracking interface events', () => { describe('tracking interface events', () => {
let eventSpy; let eventSpy;
......
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