Commit a6ab81a5 authored by Enrique Alcántara's avatar Enrique Alcántara

Merge branch '301221-fix-loading-issue-banner' into 'master'

Fix mounted event issues on invite members banner

See merge request gitlab-org/gitlab!54826
parents e8bb227d f8ff1969
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