fix(analytics): prevent tracking on admin pages and local dev environments
Build and Push Docker Image / build (push) Successful in 19s
Build and Push Docker Image / build (push) Successful in 19s
This commit is contained in:
@@ -76,4 +76,5 @@ k8s-sync-content:
|
|||||||
fi; \
|
fi; \
|
||||||
echo "-> Target pod: $$POD"; \
|
echo "-> Target pod: $$POD"; \
|
||||||
kubectl cp sample-content/. pengzhan-dev/$$POD:/app/content/; \
|
kubectl cp sample-content/. pengzhan-dev/$$POD:/app/content/; \
|
||||||
|
kubectl exec -n pengzhan-dev $$POD -- chmod -R 775 /app/content; \
|
||||||
echo "-> Sync complete. Hot-reload watcher will index new content."
|
echo "-> Sync complete. Hot-reload watcher will index new content."
|
||||||
|
|||||||
@@ -1,17 +1,40 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
injectHead({ pluginConfig }) {
|
injectHead({ pluginConfig, pageType }) {
|
||||||
const trackingId = pluginConfig.tracking_id;
|
const trackingId = pluginConfig.tracking_id;
|
||||||
if (!trackingId) return '';
|
if (!trackingId) return '';
|
||||||
|
|
||||||
|
// 1. Never inject analytics on administrative moderation pages
|
||||||
|
if (pageType === 'admin') {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Do not inject in development, staging, or testing modes (unless explicitly enabled)
|
||||||
|
const isProd = process.env.NODE_ENV === 'production' && process.env.APP_ENV !== 'staging';
|
||||||
|
if (!isProd && !pluginConfig.enable_in_dev) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<!-- Google Analytics (GA4 via Plugin) -->
|
<!-- Google Analytics (GA4 via Plugin) -->
|
||||||
<script async src="https://www.googletagmanager.com/gtag/js?id=${trackingId}"></script>
|
<script async src="https://www.googletagmanager.com/gtag/js?id=${trackingId}"></script>
|
||||||
<script>
|
<script>
|
||||||
window.dataLayer = window.dataLayer || [];
|
(function() {
|
||||||
function gtag(){dataLayer.push(arguments);}
|
// 3. Client-side domain safeguard: only track on production pengzhan.dev domains
|
||||||
gtag('js', new Date());
|
var host = window.location.hostname;
|
||||||
gtag('config', '${trackingId}');
|
if (host !== 'pengzhan.dev' && !host.endsWith('.pengzhan.dev')) {
|
||||||
|
window['ga-disable-${trackingId}'] = true;
|
||||||
|
window.dataLayer = window.dataLayer || [];
|
||||||
|
window.gtag = function(){};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
window.dataLayer = window.dataLayer || [];
|
||||||
|
function gtag(){dataLayer.push(arguments);}
|
||||||
|
window.gtag = gtag;
|
||||||
|
gtag('js', new Date());
|
||||||
|
gtag('config', '${trackingId}');
|
||||||
|
})();
|
||||||
</script>
|
</script>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user