tractatus/public/js/components/umami-tracker.js
TheFlow 573cbffa24 fix(analytics): remove data-domains restriction from tracking
- Removes data-domains attribute that was blocking tracking
- Allows tracking on agenticgovernance.digital and any subdomains
- Fixes issue where pages were not being tracked

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 10:02:44 +13:00

131 lines
3.9 KiB
JavaScript

/**
* Umami Analytics - Privacy-First Tracking
* No cookies, no personal data, GDPR-compliant
*
* Features:
* - Respects Do Not Track (DNT) browser setting
* - Honors user opt-out preference
* - Disabled in development environment
* - Lightweight async loading
*/
(function() {
'use strict';
// Configuration
const CONFIG = {
websiteId: 'e09dad07-361b-453b-9e2c-2132c657d203',
domain: 'agenticgovernance.digital',
scriptSrc: 'https://analytics.agenticgovernance.digital/script.js',
autoTrack: true
};
// Development environment check
const isDevelopment =
window.location.hostname === 'localhost' ||
window.location.hostname === '127.0.0.1' ||
window.location.hostname === '' ||
window.location.port === '9000'; // Local dev server
if (isDevelopment) {
console.log('[Umami Analytics] Disabled in development environment');
return;
}
// Respect Do Not Track (DNT) browser setting
const dnt = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
const dntEnabled = dnt === '1' || dnt === 'yes' || dnt === 'on';
if (dntEnabled) {
console.log('[Umami Analytics] Tracking disabled - Do Not Track enabled');
return;
}
// Check for user opt-out preference (localStorage)
try {
const optedOut = localStorage.getItem('umami.disabled') === 'true';
if (optedOut) {
console.log('[Umami Analytics] Tracking disabled - User opted out');
return;
}
} catch (e) {
// localStorage may not be available (privacy mode, etc.)
console.warn('[Umami Analytics] Cannot check opt-out preference:', e);
}
// Website ID validation
if (CONFIG.websiteId === 'REPLACE_WITH_ACTUAL_WEBSITE_ID') {
console.warn('[Umami Analytics] Website ID not configured. Update umami-tracker.js after Umami setup.');
return;
}
// Load Umami tracking script
const script = document.createElement('script');
script.async = true;
script.defer = true;
script.src = CONFIG.scriptSrc;
script.setAttribute('data-website-id', CONFIG.websiteId);
// Note: data-domains attribute removed to allow tracking on all subdomains
script.setAttribute('data-auto-track', CONFIG.autoTrack.toString());
// Error handling
script.onerror = function() {
console.error('[Umami Analytics] Failed to load tracking script from:', CONFIG.scriptSrc);
};
// Success callback
script.onload = function() {
console.log('[Umami Analytics] Tracking initialized (privacy-first, cookie-free)');
};
// Append script to head
document.head.appendChild(script);
// Expose opt-out function for privacy page
window.umamiOptOut = function() {
try {
localStorage.setItem('umami.disabled', 'true');
console.log('[Umami Analytics] User opted out successfully');
alert('Analytics tracking has been disabled. Reload the page to apply changes.');
return true;
} catch (e) {
console.error('[Umami Analytics] Failed to save opt-out preference:', e);
alert('Failed to save opt-out preference. Please ensure cookies/localStorage is enabled.');
return false;
}
};
// Expose opt-in function (to reverse opt-out)
window.umamiOptIn = function() {
try {
localStorage.removeItem('umami.disabled');
console.log('[Umami Analytics] User opted in successfully');
alert('Analytics tracking has been enabled. Reload the page to apply changes.');
return true;
} catch (e) {
console.error('[Umami Analytics] Failed to save opt-in preference:', e);
return false;
}
};
// Expose status check function
window.umamiStatus = function() {
const status = {
enabled: true,
development: isDevelopment,
dnt: dntEnabled,
optedOut: false,
websiteId: CONFIG.websiteId
};
try {
status.optedOut = localStorage.getItem('umami.disabled') === 'true';
} catch (e) {
status.optedOut = null;
}
console.table(status);
return status;
};
})();