Skip to content

Commit f415035

Browse files
committed
FEATURE/CCSD-685 : Added matomo code change for button click events
1 parent 6724879 commit f415035

File tree

1 file changed

+89
-13
lines changed

1 file changed

+89
-13
lines changed

frontend/micro-ui/web/public/index.html

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,96 @@
1919
<meta name="theme-color" content="#00bcd1" />
2020
<title>Digit</title>
2121
<!-- <script src="https://s3.ap-south-1.amazonaws.com/egov-dev-assets/globalConfigs.js"></script> -->
22+
2223
<!-- Matomo -->
23-
<script>
24-
var _paq = window._paq = window._paq || [];
25-
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
26-
_paq.push(['trackPageView']);
27-
_paq.push(['enableLinkTracking']);
28-
(function () {
29-
var u = "https://digit-analytics.matomo.cloud/";
30-
_paq.push(['setTrackerUrl', u + 'matomo.php']);
31-
_paq.push(['setSiteId', '1']);
32-
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
33-
g.async = true; g.src = 'https://cdn.matomo.cloud/digit-analytics.matomo.cloud/matomo.js'; s.parentNode.insertBefore(g, s);
34-
})();
35-
</script>
24+
<script>
25+
var _paq = window._paq = window._paq || [];
26+
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
27+
_paq.push(['trackPageView']);
28+
_paq.push(['enableLinkTracking']);
29+
(function () {
30+
var u = "https://digit-analytics.matomo.cloud/";
31+
_paq.push(['setTrackerUrl', u + 'matomo.php']);
32+
_paq.push(['setSiteId', '1']);
33+
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
34+
g.async = true; g.src = 'https://cdn.matomo.cloud/digit-analytics.matomo.cloud/matomo.js';
35+
s.parentNode.insertBefore(g, s);
36+
})();
37+
// (function () {
38+
// let lastUrl = location.href;
39+
// new MutationObserver(() => {
40+
// const url = location.href;
41+
// if (url !== lastUrl) {
42+
// lastUrl = url;
43+
// _paq.push(['setCustomUrl', url]);
44+
// _paq.push(['trackPageView']);
45+
// }
46+
// }).observe(document, { subtree: true, childList: true });
47+
// })();
48+
49+
(function () {
50+
let lastUrl = location.href;
51+
52+
function trackUrlChange() {
53+
const url = location.href;
54+
if (url !== lastUrl) {
55+
lastUrl = url;
56+
_paq.push(['setCustomUrl', url]);
57+
_paq.push(['trackPageView']);
58+
}
59+
}
60+
61+
// Wrap pushState
62+
history.pushState = (function (original) {
63+
return function pushState() {
64+
const result = original.apply(this, arguments);
65+
trackUrlChange();
66+
return result;
67+
};
68+
})(history.pushState);
69+
70+
// Wrap replaceState
71+
history.replaceState = (function (original) {
72+
return function replaceState() {
73+
const result = original.apply(this, arguments);
74+
trackUrlChange();
75+
return result;
76+
};
77+
})(history.replaceState);
78+
79+
// Listen for back/forward
80+
window.addEventListener('popstate', trackUrlChange);
81+
})();
82+
83+
// Track clicks on buttons & links
84+
(function () {
85+
document.addEventListener("click", function (e) {
86+
const selector = `
87+
button,
88+
input[type="button"],
89+
input[type="submit"],
90+
input[type="actionButton"],
91+
[role="button"],
92+
a[href]:not([href^="mailto:"]):not([href^="tel:"]):not([href^="#"]):not([href^="javascript:"])
93+
`;
94+
const target = e.target.closest(selector);
95+
if (target) {
96+
const elementType = target.tagName.toLowerCase();
97+
const text = target.innerText.trim() || target.value || 'Unnamed';
98+
const id = target.id || 'no-id';
99+
const classes = target.className || 'no-class';
100+
const href = target.getAttribute('href') || '';
101+
102+
_paq.push([
103+
'trackEvent',
104+
'UI Interaction', // Category
105+
`${elementType} click`, // Action
106+
`Text: ${text}, ID: ${id}, Class: ${classes}, Href: ${href}`
107+
]);
108+
}
109+
});
110+
})();
111+
</script>
36112
<!-- End Matomo Code -->
37113
</head>
38114
<style>

0 commit comments

Comments
 (0)