function Tracker2(label) {
  console.log("[NAV-ANALYTICS]: LABEL - " + label);
  try {
    ga("navigationTracker.send", {
      hitType: "event",
      eventCategory: "Product-Catalog-HP",
      eventAction: "CTA Click",
      eventLabel: label, //nazwa przycisku
    });
  } catch (t) {
	console.warn(t)
  }
}


function goDeep(nodeList) {
  const avoidButtons = ["marka", "pokaż wszystkie"];

  if (nodeList.length === 0) return;

  Array.from(nodeList).forEach(node => {
    const children = node.children;
    if (children.length === 0) return;
    Array.from(children).forEach(child => {
      const child2 = child.children;
      if (child2.length === 0) return;
      Array.from(child2).forEach((childChildNode) => {
        childChildNode.addEventListener('click', (e) => {
          if(avoidButtons.includes(e.target.textContent.toLowerCase())) return;
            Tracker2(e.target.textContent)
          return;
        })

      })
    });
  });
}



function navAnalytics() {
  const navItemsArray = document.querySelectorAll('.nav-c .nav-itm');
  try {
  console.log("[NAV-ANALYTICS]: ON");
    navItemsArray.forEach(node => {
      // nav-optn
      const navDiv = node.querySelector('.nav-optn');
      const subNavDiv = node.querySelectorAll('.nav-open');
      
      navDiv.addEventListener('click', (e) => {
        if (e.target.tagName.toLowerCase() === 'img') return;
          Tracker2(e.target.textContent)
        return;
      })
  
      goDeep(subNavDiv)
    })
  } catch(e) {
    console.log("[NAV-ANALYTICS]: ERROR");
  }
}

(function (t, e, n, i, o, a) {
  (t.GoogleAnalyticsObject = i),
    (t.ga =
      t.ga ||
      function () {
        (t.ga.q = t.ga.q || []).push(arguments);
      }),
    (t.ga.l = +new Date()),
    (o = e.createElement(n)),
    (a = e.getElementsByTagName(n)[0]),
    (o.async = 1),
    (o.src = "https://www.google-analytics.com/analytics.js"),
    a.parentNode.insertBefore(o, a);
})(window, document, "script", "ga"),
setTimeout(function () {
  ga("create", "UA-116610834-2", { name: "navigationTracker" });
}, 3e3);

window.onload = navAnalytics