PageSourceSearch

https://ubuntu.com/static/js/src/csp-event-handlers.js?v=5ad285e

js ubuntu.com collected 2026-09-24 06:03:18 UTC 2,459 bytes, 77 lines download raw bytes

1/**
2 * CSP-safe GA event tracking.
3 *
4 * Replaces inline `onclick="dataLayer.push(...)"` handlers (disallowed under
5 * our Content Security Policy) by binding listeners to elements that declare
6 * their GA event via `data-ga-*` attributes:
7 *  - data-ga-category / data-ga-action / data-ga-label (click events)
8 *  - data-ga-extra-category / -action / -label (secondary click on same element)
9 *  - data-ga-submit-category / -action / -label (form submit events)
10 *
11 * Each GA handler pushes a `GAEvent` to window.dataLayer for Google Tag Manager.
12 *
13 * Also wires CSP-safe replacements for inline UI handlers:
14 *  - data-js-back navigates back to the referring page (notification close)
15 */
16(function () {
17  "use strict";
18
19  function ready(fn) {
20    if (document.readyState !== "loading") {
21      fn();
22    } else {
23      document.addEventListener("DOMContentLoaded", fn);
24    }
25  }
26
27  function pushGAEvent(category, action, label) {
28    if (!category || !action) return;
29    if (typeof window.dataLayer === "undefined") {
30      window.dataLayer = [];
31    }
32    window.dataLayer.push({
33      event: "GAEvent",
34      eventCategory: category,
35      eventAction: action,
36      eventLabel: label || undefined,
37      eventValue: undefined,
38    });
39  }
40
41  ready(function () {
42    document.querySelectorAll("[data-ga-category]").forEach(function (el) {
43      el.addEventListener("click", function () {
44        pushGAEvent(
45          el.getAttribute("data-ga-category"),
46          el.getAttribute("data-ga-action"),
47          el.getAttribute("data-ga-label"),
48        );
49        pushGAEvent(
50          el.getAttribute("data-ga-extra-category"),
51          el.getAttribute("data-ga-extra-action"),
52          el.getAttribute("data-ga-extra-label"),
53        );
54      });
55    });
56
57    document
58      .querySelectorAll("[data-ga-submit-category]")
59      .forEach(function (form) {
60        form.addEventListener("submit", function () {
61          pushGAEvent(
62            form.getAttribute("data-ga-submit-category"),
63            form.getAttribute("data-ga-submit-action"),
64            form.getAttribute("data-ga-submit-label"),
65          );
66        });
67      });
68
69    // Notification "Close" links: navigate back to the referring page.
70    document.querySelectorAll("[data-js-back]").forEach(function (el) {
71      el.addEventListener("click", function (e) {
72        e.preventDefault();
73        window.location.href = document.referrer || "/";
74      });
75    });
76  });
77})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.