debian-mirror-gitlab/app/assets/javascripts/tracking.js

220 lines
6.2 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import { omitBy, isUndefined } from 'lodash';
2021-04-17 20:07:23 +05:30
import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants';
import { getExperimentData } from '~/experimentation/utils';
const standardContext = { ...window.gl?.snowplowStandardContext };
export const STANDARD_CONTEXT = {
schema: standardContext.schema,
data: {
...(standardContext.data || {}),
source: 'gitlab-javascript',
},
};
2019-10-12 21:52:04 +05:30
2019-12-04 20:38:33 +05:30
const DEFAULT_SNOWPLOW_OPTIONS = {
namespace: 'gl',
hostname: window.location.hostname,
cookieDomain: window.location.hostname,
appId: '',
userFingerprint: false,
respectDoNotTrack: true,
forceSecureTracker: true,
eventMethod: 'post',
2021-01-03 14:25:43 +05:30
contexts: { webPage: true, performanceTiming: true },
2019-12-04 20:38:33 +05:30
formTracking: false,
linkClickTracking: false,
2021-01-29 00:20:46 +05:30
pageUnloadTimer: 10,
2019-12-04 20:38:33 +05:30
};
2021-04-29 21:17:54 +05:30
const addExperimentContext = (opts) => {
const { experiment, ...options } = opts;
if (experiment) {
const data = getExperimentData(experiment);
if (data) {
const context = { schema: TRACKING_CONTEXT_SCHEMA, data };
return { ...options, context };
}
}
return options;
};
2020-05-24 23:13:21 +05:30
const createEventPayload = (el, { suffix = '' } = {}) => {
2021-04-29 21:17:54 +05:30
const action = (el.dataset.trackAction || el.dataset.trackEvent) + (suffix || '');
2019-12-21 20:55:43 +05:30
let value = el.dataset.trackValue || el.value || undefined;
if (el.type === 'checkbox' && !el.checked) value = false;
2021-04-29 21:17:54 +05:30
const context = addExperimentContext({
experiment: el.dataset.trackExperiment,
context: el.dataset.trackContext,
});
2021-04-17 20:07:23 +05:30
2019-12-21 20:55:43 +05:30
const data = {
label: el.dataset.trackLabel,
property: el.dataset.trackProperty,
value,
2021-04-29 21:17:54 +05:30
...context,
2019-12-21 20:55:43 +05:30
};
2020-05-24 23:13:21 +05:30
return {
action,
data: omitBy(data, isUndefined),
};
};
const eventHandler = (e, func, opts = {}) => {
2021-04-29 21:17:54 +05:30
const el = e.target.closest('[data-track-event], [data-track-action]');
2020-05-24 23:13:21 +05:30
if (!el) return;
const { action, data } = createEventPayload(el, opts);
func(opts.category, action, data);
2019-12-21 20:55:43 +05:30
};
const eventHandlers = (category, func) => {
2021-03-08 18:12:59 +05:30
const handler = (opts) => (e) => eventHandler(e, func, { ...{ category }, ...opts });
2019-12-21 20:55:43 +05:30
const handlers = [];
handlers.push({ name: 'click', func: handler() });
handlers.push({ name: 'show.bs.dropdown', func: handler({ suffix: '_show' }) });
handlers.push({ name: 'hide.bs.dropdown', func: handler({ suffix: '_hide' }) });
return handlers;
2019-10-12 21:52:04 +05:30
};
2021-04-17 20:07:23 +05:30
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);
};
2019-10-12 21:52:04 +05:30
export default class Tracking {
2021-04-17 20:07:23 +05:30
static queuedEvents = [];
static initialized = false;
2019-12-04 20:38:33 +05:30
static trackable() {
return !['1', 'yes'].includes(
window.doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack,
);
}
2021-04-17 20:07:23 +05:30
static flushPendingEvents() {
this.initialized = true;
while (this.queuedEvents.length) {
dispatchEvent(...this.queuedEvents.shift());
}
}
2019-10-12 21:52:04 +05:30
static enabled() {
2019-12-04 20:38:33 +05:30
return typeof window.snowplow === 'function' && this.trackable();
2019-10-12 21:52:04 +05:30
}
2021-04-17 20:07:23 +05:30
static event(...eventData) {
2019-10-12 21:52:04 +05:30
if (!this.enabled()) return false;
2021-04-17 20:07:23 +05:30
if (!this.initialized) {
this.queuedEvents.push(eventData);
return false;
}
return dispatchEvent(...eventData);
2019-10-12 21:52:04 +05:30
}
2020-05-24 23:13:21 +05:30
static bindDocument(category = document.body.dataset.page, parent = document) {
if (!this.enabled() || parent.trackingBound) return [];
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
// eslint-disable-next-line no-param-reassign
parent.trackingBound = true;
2019-10-12 21:52:04 +05:30
2019-12-21 20:55:43 +05:30
const handlers = eventHandlers(category, (...args) => this.event(...args));
2021-03-08 18:12:59 +05:30
handlers.forEach((event) => parent.addEventListener(event.name, event.func));
2019-12-21 20:55:43 +05:30
return handlers;
2019-10-12 21:52:04 +05:30
}
2020-05-24 23:13:21 +05:30
static trackLoadEvents(category = document.body.dataset.page, parent = document) {
if (!this.enabled()) return [];
2021-04-29 21:17:54 +05:30
const loadEvents = parent.querySelectorAll(
'[data-track-action="render"], [data-track-event="render"]',
);
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
loadEvents.forEach((element) => {
2020-05-24 23:13:21 +05:30
const { action, data } = createEventPayload(element);
this.event(category, action, data);
});
return loadEvents;
}
2021-06-08 01:23:25 +05:30
static enableFormTracking(config, contexts = []) {
if (!this.enabled()) return;
if (!config?.forms?.whitelist?.length && !config?.fields?.whitelist?.length) {
// eslint-disable-next-line @gitlab/require-i18n-strings
throw new Error('Unable to enable form event tracking without whitelist rules.');
}
contexts.unshift(STANDARD_CONTEXT);
const enabler = () => window.snowplow('enableFormTracking', config, contexts);
if (document.readyState !== 'loading') enabler();
else document.addEventListener('DOMContentLoaded', enabler);
}
2020-01-01 13:55:28 +05:30
static mixin(opts = {}) {
2019-12-21 20:55:43 +05:30
return {
2020-01-01 13:55:28 +05:30
computed: {
trackingCategory() {
const localCategory = this.tracking ? this.tracking.category : null;
return localCategory || opts.category;
},
trackingOptions() {
2021-04-29 21:17:54 +05:30
const options = addExperimentContext(opts);
return { ...options, ...this.tracking };
2020-01-01 13:55:28 +05:30
},
2019-12-21 20:55:43 +05:30
},
methods: {
2020-01-01 13:55:28 +05:30
track(action, data = {}) {
const category = data.category || this.trackingCategory;
const options = {
...this.trackingOptions,
...data,
};
Tracking.event(category, action, options);
2019-12-21 20:55:43 +05:30
},
},
2019-10-12 21:52:04 +05:30
};
}
}
2019-12-04 20:38:33 +05:30
export function initUserTracking() {
if (!Tracking.enabled()) return;
2019-12-21 20:55:43 +05:30
const opts = { ...DEFAULT_SNOWPLOW_OPTIONS, ...window.snowplowOptions };
2019-12-04 20:38:33 +05:30
window.snowplow('newTracker', opts.namespace, opts.hostname, opts);
2020-11-24 15:15:51 +05:30
document.dispatchEvent(new Event('SnowplowInitialized'));
2021-04-17 20:07:23 +05:30
Tracking.flushPendingEvents();
2020-11-24 15:15:51 +05:30
}
export function initDefaultTrackers() {
if (!Tracking.enabled()) return;
2019-12-04 20:38:33 +05:30
window.snowplow('enableActivityTracking', 30, 30);
2021-04-17 20:07:23 +05:30
// must be after enableActivityTracking
window.snowplow('trackPageView', null, [STANDARD_CONTEXT]);
2019-12-04 20:38:33 +05:30
2020-11-24 15:15:51 +05:30
if (window.snowplowOptions.formTracking) window.snowplow('enableFormTracking');
if (window.snowplowOptions.linkClickTracking) window.snowplow('enableLinkClickTracking');
2019-12-21 20:55:43 +05:30
Tracking.bindDocument();
2020-05-24 23:13:21 +05:30
Tracking.trackLoadEvents();
2019-12-04 20:38:33 +05:30
}