All files / app/assets/javascripts/tracking index.js

91.66% Statements 22/24
75% Branches 6/8
100% Functions 2/2
91.66% Lines 22/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                  2x       2x 2x   2x 2x                       10x       10x     10x   10x         10x 10x     10x       10x   10x 2x     10x 2x     10x 10x   10x 10x 10x    
import { getAllExperimentContexts } from '~/experimentation/utils';
import { DEFAULT_SNOWPLOW_OPTIONS } from './constants';
import getStandardContext from './get_standard_context';
import Tracking from './tracking';
import InternalEvents from './internal_events';
 
export { Tracking as default };
export { InternalEvents };
 
/**
 * Tracker initialization as defined in:
 * https://docs.snowplow.io/docs/collecting-data/collecting-from-own-applications/javascript-trackers/javascript-tracker/javascript-tracker-v3/tracker-setup/initialization-options/.
 * It also dispatches any event emitted before its execution.
 *
 * @returns {undefined}
 */
export function initUserTracking() {
  Iif (!Tracking.enabled()) {
    return;
  }
 
  const opts = { ...DEFAULT_SNOWPLOW_OPTIONS, ...window.snowplowOptions };
  window.snowplow('newTracker', opts.namespace, opts.hostname, opts);
 
  document.dispatchEvent(new Event('SnowplowInitialized'));
  Tracking.flushPendingEvents();
}
 
/**
 * 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}
 */
export function initDefaultTrackers() {
  Iif (!Tracking.enabled()) {
    return;
  }
 
  const opts = { ...DEFAULT_SNOWPLOW_OPTIONS, ...window.snowplowOptions };
 
  // must be before initializing the trackers
  Tracking.setAnonymousUrls();
 
  window.snowplow('enableActivityTracking', {
    minimumVisitLength: 30,
    heartbeatDelay: 30,
  });
  // must be after enableActivityTracking
  const standardContext = getStandardContext();
  const experimentContexts = getAllExperimentContexts();
  // 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', {
    title: 'GitLab',
    context: [standardContext, ...experimentContexts],
  });
  window.snowplow('setDocumentTitle', 'GitLab');
 
  if (window.snowplowOptions.formTracking) {
    Tracking.enableFormTracking(opts.formTrackingConfig);
  }
 
  if (window.snowplowOptions.linkClickTracking) {
    window.snowplow('enableLinkClickTracking');
  }
 
  Tracking.bindDocument();
  Tracking.trackLoadEvents();
 
  InternalEvents.bindInternalEventDocument();
  InternalEvents.trackInternalLoadEvents();
  InternalEvents.initBrowserSDK();
}