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

110 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-12-21 20:55:43 +05:30
import _ from 'underscore';
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',
contexts: { webPage: true },
formTracking: false,
linkClickTracking: false,
};
2019-12-21 20:55:43 +05:30
const eventHandler = (e, func, opts = {}) => {
const el = e.target.closest('[data-track-event]');
const action = el && el.dataset.trackEvent;
if (!action) return;
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,
};
func(opts.category, action + (opts.suffix || ''), _.omit(data, _.isUndefined));
};
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-10-12 21:52:04 +05:30
};
export default class Tracking {
2019-12-04 20:38:33 +05:30
static trackable() {
return !['1', 'yes'].includes(
window.doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack,
);
}
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
}
2019-12-21 20:55:43 +05:30
static event(category = document.body.dataset.page, action = 'generic', data = {}) {
2019-10-12 21:52:04 +05:30
if (!this.enabled()) return false;
// eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings
if (!category) throw new Error('Tracking: no category provided for tracking.');
2019-12-21 20:55:43 +05:30
const { label, property, value, context } = data;
const contexts = context ? [context] : undefined;
return window.snowplow('trackStructEvent', category, action, label, property, value, contexts);
2019-10-12 21:52:04 +05:30
}
2019-12-21 20:55:43 +05:30
static bindDocument(category = document.body.dataset.page, documentOverride = null) {
const el = documentOverride || document;
if (!this.enabled() || el.trackingBound) return [];
2019-10-12 21:52:04 +05:30
2019-12-21 20:55:43 +05:30
el.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));
handlers.forEach(event => el.addEventListener(event.name, event.func));
return handlers;
2019-10-12 21:52:04 +05:30
}
2019-12-21 20:55:43 +05:30
static mixin(opts) {
return {
data() {
return {
tracking: {
// eslint-disable-next-line no-underscore-dangle
category: this.$options.name || this.$options._componentTag,
},
};
},
methods: {
track(action, data) {
const category = opts.category || data.category || this.tracking.category;
Tracking.event(category || 'unspecified', action, { ...opts, ...this.tracking, ...data });
},
},
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);
window.snowplow('enableActivityTracking', 30, 30);
window.snowplow('trackPageView'); // must be after enableActivityTracking
if (opts.formTracking) window.snowplow('enableFormTracking');
if (opts.linkClickTracking) window.snowplow('enableLinkClickTracking');
2019-12-21 20:55:43 +05:30
Tracking.bindDocument();
2019-12-04 20:38:33 +05:30
}