ITADN

Bug: Analytics scripts are executed on deploy previews and send unwanted data

#1405Openyuriassuncx 创建于 2025-09-25
Y
yuriassuncxcommented
## Problem The `Analytics` component in [`website/components/Analytics.tsx`](https://github.com/deco-cx/apps/blob/main/website/components/Analytics.tsx) is currently loading Google Analytics and Google Tag Manager scripts even in **deploy preview environments** (e.g., `*.deno.dev` or `deco-sites-*.deno.dev`). This happens because of the following check: ```ts const isDeploy = !!context.isDeploy; ```` On Deno, this value is `true` not only for production, but also for deploy previews. As a result, analytics scripts are executed in previews, sending data to GA/GTM from non-production domains. These preview domains get marked as monitored domains in Google Tag diagnostics, which triggers errors and pollutes production analytics. ## Impact * **Unwanted data**: Preview/staging events are mixed with production analytics. * **Errors/warnings**: Google Tag Manager flags preview domains as unauthorized/monitored, leading to console errors. * **Harder QA**: Makes testing/debugging more difficult, since preview data pollutes real reports. ## Suggested Solution Improve the environment detection so analytics only runs in production. Possible approaches: * Use a stricter environment flag (e.g., `context.isProduction` or an explicit `DECO_ENV === 'production'`). * Exclude preview domains explicitly (`*.deno.dev`, `deco-sites-*.deno.dev`). * Introduce a new config/prop (e.g., `enableAnalytics`) that defaults to `false` for previews and `true` only in production. Example change: ```diff - const isDeploy = !!context.isDeploy; + const isProduction = context.env?.DECO_ENV === "production" || context.isProduction; - {isDeploy && ( ...analytics scripts... )} + {isProduction && ( ...analytics scripts... )} ``` ## Expected Outcome Analytics scripts (GTM/GA) should **only run in true production environments**. No data should be sent from deploy previews, ensuring: * Clean analytics data. * No Google Tag Manager errors from preview domains. * A more reliable debugging and testing experience. --- **Suggested label:** `analytics`
0 条评论