Skip to content

Commit 84c0ca3

Browse files
committed
feat: disable tracking by default in non-production environments
1 parent 7eb8248 commit 84c0ca3

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

packages/analytics/src/client/utils.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
import type { SimpleAnalyticsProps } from "./simple-analytics";
2+
import { PHASE_PRODUCTION_BUILD } from "next/constants";
3+
4+
export function isProduction() {
5+
if (process.env.NEXT_PUBLIC_ENABLE_ANALYTICS_IN_DEV === "1") {
6+
return true;
7+
}
8+
9+
if (process.env.NODE_ENV !== "production") {
10+
return false;
11+
}
12+
13+
if (!process.env.NEXT_PUBLIC_VERCEL_ENV) {
14+
return true;
15+
}
16+
17+
return process.env.NEXT_PUBLIC_VERCEL_ENV !== "production";
18+
}
19+
20+
function isBuildTime() {
21+
return process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD;
22+
}
223

324
export function parseDataProps(settings?: SimpleAnalyticsProps) {
425
if (!process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME) {
526
console.error("No hostname provided for Simple Analytics");
627
return {};
728
}
829

30+
if (isBuildTime()) {
31+
return {};
32+
}
33+
34+
if (!isProduction()) {
35+
console.log("Simple Analytics is disabled by default in development and preview environments, enable it by setting NEXT_PUBLIC_ENABLE_ANALYTICS_IN_DEV=1 in your environment");
36+
return {};
37+
}
38+
939
if (!settings) {
1040
return {
1141
"data-hostname": process.env.NEXT_PUBLIC_SIMPLE_ANALYTICS_HOSTNAME,

packages/analytics/src/server/simple-analytics.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
HeaderOnlyContext,
88
ServerContext,
99
} from "./interfaces";
10-
import { isDoNotTrackEnabled, parseRequest } from "./utils";
10+
import { isBuildTime, isProduction, isDoNotTrackEnabled, parseRequest } from "./utils";
1111
import { parseHeaders } from "./headers";
1212
import { parseUtmParameters } from "./utm";
1313

@@ -40,6 +40,15 @@ export async function trackEvent(
4040
...parseHeaders(headers, options.ignoreMetrics),
4141
};
4242

43+
if (isBuildTime()) {
44+
return;
45+
}
46+
47+
if (!isProduction()) {
48+
console.log("Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment");
49+
return;
50+
}
51+
4352
const response = await fetch("https://queue.simpleanalyticscdn.com/events", {
4453
method: "POST",
4554
headers: {
@@ -110,6 +119,15 @@ export async function trackPageview(options: TrackPageviewOptions) {
110119
: {}),
111120
};
112121

122+
if (isBuildTime()) {
123+
return;
124+
}
125+
126+
if (!isProduction()) {
127+
console.log("Simple Analytics is disabled by default in development and preview environments, enable it by setting ENABLE_ANALYTICS_IN_DEV=1 in your environment");
128+
return;
129+
}
130+
113131
const response = await fetch("https://queue.simpleanalyticscdn.com/events", {
114132
method: "POST",
115133
headers: {

packages/analytics/src/server/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PHASE_PRODUCTION_BUILD } from "next/constants";
2+
13
export function isDoNotTrackEnabled(headers: Headers) {
24
return headers.has("DNT") && headers.get("DNT") === "1";
35
}
@@ -10,3 +12,27 @@ export function parseRequest(request: Request) {
1012
searchParams: url.searchParams,
1113
};
1214
}
15+
16+
export function isProduction() {
17+
if (process.env.ENABLE_ANALYTICS_IN_DEV === "1") {
18+
return true;
19+
}
20+
21+
if (process.env.NODE_ENV !== "production") {
22+
return false;
23+
}
24+
25+
if (!process.env.VERCEL_ENV) {
26+
return true;
27+
}
28+
29+
return process.env.VERCEL_ENV !== "production";
30+
}
31+
32+
export function isRunningOnVercel() {
33+
return process.env.VERCEL === "1";
34+
}
35+
36+
export function isBuildTime() {
37+
return process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD;
38+
}

0 commit comments

Comments
 (0)