All files / ee/app/assets/javascripts/event_tracking navbar.js

0% Statements 0/21
0% Branches 0/13
0% Functions 0/4
0% Lines 0/19

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                                                                                               
import { mergeUrlParams } from '~/lib/utils/url_utility';
import Tracking from '~/tracking';
 
const TRACKING_CATEGORY = 'navbar_top';
const NAVSOURCE_KEY = 'nav_source';
const NAVSOURCE_VALUE = 'navbar';
 
/**
 * intercepts clicks on navbar links
 * and adds the 'nav_source=navbar' query parameter
 */
const appendLinkParam = (e) => {
  const target = e.currentTarget;
 
  // get closest link in case the target is a wrapping DOM node
  const link = target.tagName === 'A' ? target : target.closest('a');
 
  if (link && link.href && link.href.indexOf(`${NAVSOURCE_KEY}=${NAVSOURCE_VALUE}`) === -1) {
    const url = mergeUrlParams({ [NAVSOURCE_KEY]: NAVSOURCE_VALUE }, link.href);
    link.setAttribute('href', url);
  }
};
 
export default function trackNavbarEvents() {
  if (!Tracking.enabled()) return;
 
  const navbar = document.querySelector('.navbar-gitlab');
  if (!navbar) return;
 
  // track search inputs within frequent-items component
  navbar.querySelectorAll(`.frequent-items-dropdown-container input`).forEach((el) => {
    el.addEventListener('click', (e) => {
      const parentDropdown = e.currentTarget.closest('li.dropdown');
 
      Tracking.event(TRACKING_CATEGORY, 'activate_form_input', {
        label: `${parentDropdown.getAttribute('data-track-label')}_search`,
        property: '',
        value: '',
      });
    });
  });
 
  if (navbar) {
    navbar.addEventListener('click', appendLinkParam);
    navbar.addEventListener('contextmenu', appendLinkParam);
  }
}