Skip to content

Commit 12f32d1

Browse files
Disable Google Analytics or Adsense if not provided
1 parent a406a17 commit 12f32d1

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

.env.sample

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Enable analytics during development - for debugging purposes
2-
// Analytics are always enabled in non-dev environments
2+
// Analytics are always enabled in non-dev environments if a measurement ID is provided
33
NEXT_PUBLIC_ENABLE_ANALYTICS=false
44
// Enables ads during development - for debugging purposes
5-
// Ads are always enabled in non-dev environments
5+
// Ads are always enabled in non-dev environments if a publisher ID is provided
66
NEXT_PUBLIC_ENABLE_ADS=false
77

88
// Google Analytics measurement ID - not required

src/components/Adsense/adsense.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import React from "react";
33
import { getEnvironment } from "@/scripts/Utils/Environment";
44

55
export function Adsense(): React.ReactNode {
6+
const [useGA, setUseGA] = React.useState<boolean>(false);
7+
68
React.useEffect(() => {
79
if (getEnvironment() === "development") {
810
if (process.env.NEXT_PUBLIC_ENABLE_ADS) {
@@ -11,14 +13,18 @@ export function Adsense(): React.ReactNode {
1113
console.info("Google Adsense disabled during development");
1214
return;
1315
}
16+
} else if (
17+
process.env.NEXT_PUBLIC_ADSENSE_PUBLISHER_ID === "pub-XXXXXXXXXXXXXXXX" ||
18+
!process.env.NEXT_PUBLIC_ADSENSE_PUBLISHER_ID
19+
) {
20+
// console.info("No publisher ID provided, disabling Google Adsense");
21+
return;
1422
}
23+
24+
setUseGA(true);
1525
}, []);
1626

17-
return getEnvironment() === "development" ? (
18-
<></>
19-
) : (
20-
<GoogleAdSense publisherId="pub-XXXXXXXXXXXXXXXX" />
21-
);
27+
return useGA ? <></> : <GoogleAdSense publisherId="pub-XXXXXXXXXXXXXXXX" />;
2228
}
2329

2430
export default Adsense;

src/components/Analytics/analytics.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ export function Analytics(): React.ReactNode {
7474
console.info("Google Analytics disabled during development");
7575
return;
7676
}
77+
} else if (
78+
process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID === "G-XXXXXXXXXX" ||
79+
!process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID
80+
) {
81+
console.info("No measurement ID provided, disabling Google Analytics");
82+
return;
7783
}
7884

7985
setUseGA(true);

0 commit comments

Comments
 (0)