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 initGroupDetails from '../shared/group_details';
document.addEventListener('DOMContentLoaded', () => {
leaveByUrl('group');
initGroupDetails();
});
leaveByUrl('group');
initGroupDetails();
......@@ -61,30 +61,51 @@ const eventHandlers = (category, func) => {
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 {
static queuedEvents = [];
static initialized = false;
static trackable() {
return !['1', 'yes'].includes(
window.doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack,
);
}
static flushPendingEvents() {
this.initialized = true;
while (this.queuedEvents.length) {
dispatchEvent(...this.queuedEvents.shift());
}
}
static enabled() {
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;
// 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);
if (!this.initialized) {
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) {
......@@ -143,6 +164,7 @@ export function initUserTracking() {
window.snowplow('newTracker', opts.namespace, opts.hostname, opts);
document.dispatchEvent(new Event('SnowplowInitialized'));
Tracking.flushPendingEvents();
}
export function initDefaultTrackers() {
......
......@@ -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', () => {
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