2020-04-21 15:21:10 +00:00
|
|
|
import { omitBy, isUndefined } from 'lodash';
|
2019-08-01 05:28:15 +00:00
|
|
|
|
2019-08-28 06:52:14 +00:00
|
|
|
const DEFAULT_SNOWPLOW_OPTIONS = {
|
|
|
|
namespace: 'gl',
|
|
|
|
hostname: window.location.hostname,
|
|
|
|
cookieDomain: window.location.hostname,
|
|
|
|
appId: '',
|
|
|
|
userFingerprint: false,
|
|
|
|
respectDoNotTrack: true,
|
|
|
|
forceSecureTracker: true,
|
|
|
|
eventMethod: 'post',
|
2020-10-13 18:08:58 +00:00
|
|
|
contexts: { webPage: true, performanceTiming: true },
|
2019-09-18 14:02:45 +00:00
|
|
|
formTracking: false,
|
|
|
|
linkClickTracking: false,
|
2019-08-28 06:52:14 +00:00
|
|
|
};
|
|
|
|
|
2020-04-22 18:09:52 +00:00
|
|
|
const createEventPayload = (el, { suffix = '' } = {}) => {
|
|
|
|
const action = el.dataset.trackEvent + (suffix || '');
|
2019-09-19 21:06:29 +00:00
|
|
|
let value = el.dataset.trackValue || el.value || undefined;
|
|
|
|
if (el.type === 'checkbox' && !el.checked) value = false;
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
label: el.dataset.trackLabel,
|
|
|
|
property: el.dataset.trackProperty,
|
|
|
|
value,
|
|
|
|
context: el.dataset.trackContext,
|
|
|
|
};
|
|
|
|
|
2020-04-22 18:09:52 +00:00
|
|
|
return {
|
|
|
|
action,
|
|
|
|
data: omitBy(data, isUndefined),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandler = (e, func, opts = {}) => {
|
|
|
|
const el = e.target.closest('[data-track-event]');
|
|
|
|
|
|
|
|
if (!el) return;
|
|
|
|
|
|
|
|
const { action, data } = createEventPayload(el, opts);
|
|
|
|
func(opts.category, action, data);
|
2019-09-19 21:06:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const eventHandlers = (category, func) => {
|
|
|
|
const handler = opts => e => eventHandler(e, func, { ...{ category }, ...opts });
|
|
|
|
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-08-01 05:28:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class Tracking {
|
2019-08-23 07:11:26 +00:00
|
|
|
static trackable() {
|
|
|
|
return !['1', 'yes'].includes(
|
|
|
|
window.doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-01 05:28:15 +00:00
|
|
|
static enabled() {
|
2019-08-23 07:11:26 +00:00
|
|
|
return typeof window.snowplow === 'function' && this.trackable();
|
2019-08-01 05:28:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 21:06:29 +00:00
|
|
|
static event(category = document.body.dataset.page, action = 'generic', data = {}) {
|
2019-08-01 05:28:15 +00:00
|
|
|
if (!this.enabled()) return false;
|
2020-03-18 15:09:45 +00:00
|
|
|
// eslint-disable-next-line @gitlab/require-i18n-strings
|
2019-08-01 05:28:15 +00:00
|
|
|
if (!category) throw new Error('Tracking: no category provided for tracking.');
|
|
|
|
|
2019-09-19 21:06:29 +00:00
|
|
|
const { label, property, value, context } = data;
|
|
|
|
const contexts = context ? [context] : undefined;
|
|
|
|
return window.snowplow('trackStructEvent', category, action, label, property, value, contexts);
|
2019-08-01 05:28:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 18:09:52 +00:00
|
|
|
static bindDocument(category = document.body.dataset.page, parent = document) {
|
|
|
|
if (!this.enabled() || parent.trackingBound) return [];
|
2019-08-01 05:28:15 +00:00
|
|
|
|
2020-04-22 18:09:52 +00:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
parent.trackingBound = true;
|
2019-08-01 05:28:15 +00:00
|
|
|
|
2019-09-19 21:06:29 +00:00
|
|
|
const handlers = eventHandlers(category, (...args) => this.event(...args));
|
2020-04-22 18:09:52 +00:00
|
|
|
handlers.forEach(event => parent.addEventListener(event.name, event.func));
|
2019-09-19 21:06:29 +00:00
|
|
|
return handlers;
|
2019-08-01 05:28:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 18:09:52 +00:00
|
|
|
static trackLoadEvents(category = document.body.dataset.page, parent = document) {
|
|
|
|
if (!this.enabled()) return [];
|
|
|
|
|
|
|
|
const loadEvents = parent.querySelectorAll('[data-track-event="render"]');
|
|
|
|
|
|
|
|
loadEvents.forEach(element => {
|
|
|
|
const { action, data } = createEventPayload(element);
|
|
|
|
this.event(category, action, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return loadEvents;
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:06:29 +00:00
|
|
|
static mixin(opts = {}) {
|
2019-09-19 21:06:29 +00:00
|
|
|
return {
|
2019-11-27 09:06:29 +00:00
|
|
|
computed: {
|
|
|
|
trackingCategory() {
|
|
|
|
const localCategory = this.tracking ? this.tracking.category : null;
|
|
|
|
return localCategory || opts.category;
|
|
|
|
},
|
|
|
|
trackingOptions() {
|
|
|
|
return { ...opts, ...this.tracking };
|
|
|
|
},
|
2019-09-19 21:06:29 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-11-27 09:06:29 +00:00
|
|
|
track(action, data = {}) {
|
|
|
|
const category = data.category || this.trackingCategory;
|
|
|
|
const options = {
|
|
|
|
...this.trackingOptions,
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
Tracking.event(category, action, options);
|
2019-09-19 21:06:29 +00:00
|
|
|
},
|
|
|
|
},
|
2019-08-01 05:28:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 06:52:14 +00:00
|
|
|
|
|
|
|
export function initUserTracking() {
|
|
|
|
if (!Tracking.enabled()) return;
|
|
|
|
|
2019-09-19 21:06:29 +00:00
|
|
|
const opts = { ...DEFAULT_SNOWPLOW_OPTIONS, ...window.snowplowOptions };
|
2019-08-28 06:52:14 +00:00
|
|
|
window.snowplow('newTracker', opts.namespace, opts.hostname, opts);
|
|
|
|
|
2020-09-04 12:08:27 +00:00
|
|
|
document.dispatchEvent(new Event('SnowplowInitialized'));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initDefaultTrackers() {
|
|
|
|
if (!Tracking.enabled()) return;
|
|
|
|
|
2019-09-18 14:02:45 +00:00
|
|
|
window.snowplow('enableActivityTracking', 30, 30);
|
|
|
|
window.snowplow('trackPageView'); // must be after enableActivityTracking
|
|
|
|
|
2020-09-04 12:08:27 +00:00
|
|
|
if (window.snowplowOptions.formTracking) window.snowplow('enableFormTracking');
|
|
|
|
if (window.snowplowOptions.linkClickTracking) window.snowplow('enableLinkClickTracking');
|
2019-09-19 21:06:29 +00:00
|
|
|
|
|
|
|
Tracking.bindDocument();
|
2020-04-22 18:09:52 +00:00
|
|
|
Tracking.trackLoadEvents();
|
2019-08-28 06:52:14 +00:00
|
|
|
}
|