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

100% Statements 68/68
90.62% Branches 29/32
100% Functions 15/15
100% Lines 65/65

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158                        949x 1192x   1192x 12x 12x 5x 5x       1187x     949x                 49x   49x 49x   49x 1x     49x   49x 2x 2x   1x       49x         49x               49x           949x 9x   9x     949x 17x   17x 11x   6x   6x     949x 59x 59x   59x 18x     41x 41x     949x 2733x 911x   911x 911x 911x   911x     949x 8x 8x   8x     949x 13x   13x     949x 13x 13x   12x   1x       949x 10x   10x     949x 434x 43x     434x 4x                  
import { omitBy, isUndefined } from 'lodash';
import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants';
import { getExperimentData } from '~/experimentation/utils';
import {
  ACTION_ATTR_SELECTOR,
  LOAD_ACTION_ATTR_SELECTOR,
  URLS_CACHE_STORAGE_KEY,
  REFERRER_TTL,
  INTERNAL_EVENTS_SELECTOR,
  ALLOWED_ADDITIONAL_PROPERTIES,
} from './constants';
 
export const addExperimentContext = (opts) => {
  const { experiment, ...options } = opts;
 
  if (experiment) {
    const data = getExperimentData(experiment);
    if (data) {
      const context = { schema: TRACKING_CONTEXT_SCHEMA, data };
      return { ...options, context };
    }
  }
 
  return options;
};
 
export const createEventPayload = (el, { suffix = '' } = {}) => {
  const {
    trackAction,
    trackValue,
    trackExtra,
    trackExperiment,
    trackContext,
    trackLabel,
    trackProperty,
  } = el?.dataset || {};
 
  const action = `${trackAction}${suffix || ''}`;
  let value = trackValue || el.value || undefined;
 
  if (el.type === 'checkbox' && !el.checked) {
    value = 0;
  }
 
  let extra = trackExtra;
 
  if (extra !== undefined) {
    try {
      extra = JSON.parse(extra);
    } catch (e) {
      extra = undefined;
    }
  }
 
  const context = addExperimentContext({
    experiment: trackExperiment,
    context: trackContext,
  });
 
  const data = {
    label: trackLabel,
    property: trackProperty,
    value,
    extra,
    ...context,
  };
 
  return {
    action,
    data: omitBy(data, isUndefined),
  };
};
 
export const createInternalEventPayload = (el) => {
  const { eventTracking } = el?.dataset || {};
 
  return eventTracking;
};
 
export const InternalEventHandler = (e, func) => {
  const el = e.target.closest(INTERNAL_EVENTS_SELECTOR);
 
  if (!el) {
    return;
  }
  const event = createInternalEventPayload(el);
 
  func(event);
};
 
export const eventHandler = (e, func, opts = {}) => {
  const actionSelector = `${ACTION_ATTR_SELECTOR}:not(${LOAD_ACTION_ATTR_SELECTOR})`;
  const el = e.target.closest(actionSelector);
 
  if (!el) {
    return;
  }
 
  const { action, data } = createEventPayload(el, opts);
  func(opts.category, action, data);
};
 
export const getEventHandlers = (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;
};
 
export const renameKey = (o, oldKey, newKey) => {
  const ret = {};
  delete Object.assign(ret, o, { [newKey]: o[oldKey] })[oldKey];
 
  return ret;
};
 
export const filterOldReferrersCacheEntries = (cache) => {
  const now = Date.now();
 
  return cache.filter((entry) => entry.timestamp && entry.timestamp > now - REFERRER_TTL);
};
 
export const getReferrersCache = () => {
  try {
    const referrers = JSON.parse(window.localStorage.getItem(URLS_CACHE_STORAGE_KEY) || '[]');
 
    return filterOldReferrersCacheEntries(referrers);
  } catch {
    return [];
  }
};
 
export const addReferrersCacheEntry = (cache, entry) => {
  const referrers = JSON.stringify([{ ...entry, timestamp: Date.now() }, ...cache]);
 
  window.localStorage.setItem(URLS_CACHE_STORAGE_KEY, referrers);
};
 
export const validateAdditionalProperties = (additionalProperties) => {
  const disallowedProperties = Object.keys(additionalProperties).filter(
    (key) => !ALLOWED_ADDITIONAL_PROPERTIES.includes(key),
  );
 
  if (disallowedProperties.length > 0) {
    throw new Error(
      `Allowed additional properties are ${ALLOWED_ADDITIONAL_PROPERTIES.join(
        ', ',
      )} for InternalEvents tracking.\nDisallowed additional properties were provided: ${disallowedProperties.join(
        ', ',
      )}.`,
    );
  }
};