How to Configure Tinylytics Analytics for Production Environment Only
I have tried to prevent tinylytics stats from being retrieved outside of the production environment. When incorporating tinylytics javascript, accesses during development are also reflected in the statistics, so we added code so that statistics are obtained only in production. There may be a better way to do this, but for now, I'll introduce it here because it does what I want to do. This code works as follows: Place this code in the <head> section of your HTML and remove the original tinylytics script tag.Why is this code needed?
How this code works
Implementation
<!-- Check domain and enable analytics only for specific domain -->
<script>
// Get current hostname
const currentHostname = window.location.hostname;
// Specify production domain
const productionDomain = 'your.domain.com';
// Only load analytics on production domain
if (currentHostname === productionDomain) {
// Dynamically add tinylytics script
const analyticScript = document.createElement('script');
analyticScript.src = 'https://tinylytics.app/embed/Xx-xxxxxxxxxxxxxx.js';
analyticScript.defer = true;
document.head.appendChild(analyticScript);
console.log('Production environment: Analytics enabled');
} else {
console.log('Development environment: Analytics disabled');
}
</script>