Add tracking for clicking links
For example we would like to track clicks on weblinks.
Release-Notes: skip
Change-Id: Ieedd253d2ffefc7b321ac851765acc445341ae5a
diff --git a/polygerrit-ui/app/services/gr-reporting/gr-reporting_impl.ts b/polygerrit-ui/app/services/gr-reporting/gr-reporting_impl.ts
index 06e8204..5b6e852 100644
--- a/polygerrit-ui/app/services/gr-reporting/gr-reporting_impl.ts
+++ b/polygerrit-ui/app/services/gr-reporting/gr-reporting_impl.ts
@@ -17,6 +17,7 @@
Timing,
} from '../../constants/reporting';
import {onCLS, onFID, onLCP, Metric, onINP} from 'web-vitals';
+import {getEventPath, isElementTarget} from '../../utils/dom-util';
// Latency reporting constants.
@@ -186,6 +187,22 @@
});
}
+export function initClickReporter(reportingService: ReportingService) {
+ document.addEventListener('click', (e: MouseEvent) => {
+ const anchorEl = e
+ .composedPath()
+ .find(el => isElementTarget(el) && el.tagName.toUpperCase() === 'A') as
+ | HTMLAnchorElement
+ | undefined;
+ if (!anchorEl) return;
+ reportingService.reportInteraction(Interaction.LINK_CLICK, {
+ path: getEventPath(e),
+ link: anchorEl.href,
+ text: anchorEl.innerText,
+ });
+ });
+}
+
export function initWebVitals(reportingService: ReportingService) {
function reportWebVitalMetric(name: Timing, metric: Metric) {
let score = metric.value;