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

100% Statements 17/17
91.66% Branches 11/12
100% Functions 1/1
100% Lines 17/17

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                32x   1x     31x 31x   31x 31x   31x 3x 1x   2x       31x 14x     31x 31x               29x   2x 2x      
import * as Sentry from '~/sentry/sentry_browser_wrapper';
import getStandardContext from './get_standard_context';
 
export function dispatchSnowplowEvent(
  category = document.body.dataset.page,
  action = 'generic',
  data = {},
) {
  if (!category) {
    /* eslint-disable-next-line @gitlab/require-i18n-strings */
    throw new Error('Tracking: no category provided for tracking.');
  }
 
  const { label, property, extra = {} } = data;
  let { value } = data;
 
  const standardContext = getStandardContext({ extra });
  let contexts = [standardContext];
 
  if (data.context) {
    if (Array.isArray(data.context)) {
      contexts = [...contexts, ...data.context];
    } else {
      contexts.push(data.context);
    }
  }
 
  if (value !== undefined) {
    value = Number(value);
  }
 
  try {
    window.snowplow('trackStructEvent', {
      category,
      action,
      label,
      property,
      value,
      context: contexts,
    });
    return true;
  } catch (error) {
    Sentry.captureException(error);
    return false;
  }
}