-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook-call.js
More file actions
64 lines (58 loc) · 1.7 KB
/
Copy pathbook-call.js
File metadata and controls
64 lines (58 loc) · 1.7 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// book-call.js
(function () {
const CONSENT_KEY = 'mousaConsentV1';
const EVENT_NAME = 'clicked_contact_button_link';
const SELECTOR = '.book-call';
function hasConsent() {
try {
const raw = localStorage.getItem(CONSENT_KEY);
if (!raw) return false;
const c = JSON.parse(raw);
return c && c.accepted === true;
} catch (e) {
return false;
}
}
function fireEventIfAllowed() {
if (hasConsent() && typeof window.gtag === 'function') {
try {
window.gtag('event', EVENT_NAME, { transport_type: 'beacon' });
//console.log('book call event fired');
} catch (e) {
console.warn('gtag error', e);
}
}
}
function attachListeners() {
const els = document.querySelectorAll(SELECTOR);
if (!els || els.length === 0) return;
els.forEach(function (el) {
// avoid attaching twice
if (el.__bookCallAttached) return;
el.__bookCallAttached = true;
el.addEventListener('click', function () {
fireEventIfAllowed();
});
});
}
// Attach now and also after DOMContentLoaded if needed
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', attachListeners);
} else {
attachListeners();
}
// Also re-run if elements are added dynamically (mutation observer)
try {
const mo = new MutationObserver(function () {
attachListeners();
});
mo.observe(document.documentElement || document.body, { childList: true, subtree: true });
} catch (e) {
// MutationObserver not available — fine to skip
}
// Optional: expose for manual re-attach
window.__bookCall = {
attach: attachListeners,
hasConsent: hasConsent
};
})();