2021-08-31 08:11:07 -04:00
|
|
|
import { getAllExperimentContexts } from '~/experimentation/utils';
|
2021-08-10 14:10:13 -04:00
|
|
|
import { DEFAULT_SNOWPLOW_OPTIONS } from './constants';
|
2021-05-31 05:10:22 -04:00
|
|
|
import getStandardContext from './get_standard_context';
|
2021-08-10 14:10:13 -04:00
|
|
|
import Tracking from './tracking';
|
2021-02-18 07:09:34 -05:00
|
|
|
|
2021-08-10 14:10:13 -04:00
|
|
|
export { Tracking as default };
|
2019-08-28 02:52:14 -04:00
|
|
|
|
2021-08-10 14:10:13 -04:00
|
|
|
/**
|
|
|
|
* Tracker initialization as defined in:
|
|
|
|
* https://docs.snowplowanalytics.com/docs/collecting-data/collecting-from-own-applications/javascript-trackers/javascript-tracker/javascript-tracker-v2/tracker-setup/initializing-a-tracker-2/.
|
|
|
|
* It also dispatches any event emitted before its execution.
|
|
|
|
*
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
2019-08-28 02:52:14 -04:00
|
|
|
export function initUserTracking() {
|
2021-08-10 14:10:13 -04:00
|
|
|
if (!Tracking.enabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-28 02:52:14 -04:00
|
|
|
|
2019-09-19 17:06:29 -04:00
|
|
|
const opts = { ...DEFAULT_SNOWPLOW_OPTIONS, ...window.snowplowOptions };
|
2019-08-28 02:52:14 -04:00
|
|
|
window.snowplow('newTracker', opts.namespace, opts.hostname, opts);
|
|
|
|
|
2020-09-04 08:08:27 -04:00
|
|
|
document.dispatchEvent(new Event('SnowplowInitialized'));
|
2021-02-23 19:11:11 -05:00
|
|
|
Tracking.flushPendingEvents();
|
2020-09-04 08:08:27 -04:00
|
|
|
}
|
|
|
|
|
2021-08-10 14:10:13 -04:00
|
|
|
/**
|
|
|
|
* Enables tracking of built-in events: page views, page pings.
|
|
|
|
* Optionally enables form and link tracking (automatically).
|
|
|
|
* Attaches event handlers for data-attributes powered events, and
|
|
|
|
* load-events (on render).
|
|
|
|
*
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
2020-09-04 08:08:27 -04:00
|
|
|
export function initDefaultTrackers() {
|
2021-08-10 14:10:13 -04:00
|
|
|
if (!Tracking.enabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-04 08:08:27 -04:00
|
|
|
|
2021-05-25 02:10:50 -04:00
|
|
|
const opts = { ...DEFAULT_SNOWPLOW_OPTIONS, ...window.snowplowOptions };
|
|
|
|
|
2021-09-17 14:11:44 -04:00
|
|
|
// must be before initializing the trackers
|
|
|
|
Tracking.setAnonymousUrls();
|
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
window.snowplow('enableActivityTracking', 30, 30);
|
2021-02-18 07:09:34 -05:00
|
|
|
// must be after enableActivityTracking
|
2021-05-31 05:10:22 -04:00
|
|
|
const standardContext = getStandardContext();
|
2021-08-31 08:11:07 -04:00
|
|
|
const experimentContexts = getAllExperimentContexts();
|
2022-01-05 07:16:26 -05:00
|
|
|
// To not expose personal identifying information, the page title is hardcoded as `GitLab`
|
|
|
|
// See: https://gitlab.com/gitlab-org/gitlab/-/issues/345243
|
|
|
|
window.snowplow('trackPageView', 'GitLab', [standardContext, ...experimentContexts]);
|
|
|
|
window.snowplow('setDocumentTitle', 'GitLab');
|
2019-09-18 10:02:45 -04:00
|
|
|
|
2021-08-10 14:10:13 -04:00
|
|
|
if (window.snowplowOptions.formTracking) {
|
|
|
|
Tracking.enableFormTracking(opts.formTrackingConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.snowplowOptions.linkClickTracking) {
|
|
|
|
window.snowplow('enableLinkClickTracking');
|
|
|
|
}
|
2019-09-19 17:06:29 -04:00
|
|
|
|
|
|
|
Tracking.bindDocument();
|
2020-04-22 14:09:52 -04:00
|
|
|
Tracking.trackLoadEvents();
|
2019-08-28 02:52:14 -04:00
|
|
|
}
|