Skip to content

Commit 6e8b0b1

Browse files
Merge pull request #171 from UnsignedArduino/staging
External script optimization and more analytics
2 parents caf1553 + ddd368f commit 6e8b0b1

6 files changed

Lines changed: 52 additions & 13 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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ 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(() => {
9+
if (
10+
process.env.NEXT_PUBLIC_ADSENSE_PUBLISHER_ID === "pub-XXXXXXXXXXXXXXXX" ||
11+
!process.env.NEXT_PUBLIC_ADSENSE_PUBLISHER_ID
12+
) {
13+
// console.info("No publisher ID provided, disabling Google Adsense");
14+
return;
15+
}
716
if (getEnvironment() === "development") {
817
if (process.env.NEXT_PUBLIC_ENABLE_ADS) {
918
console.info("Enabling Google Adsense during development");
@@ -12,13 +21,11 @@ export function Adsense(): React.ReactNode {
1221
return;
1322
}
1423
}
24+
25+
setUseGA(true);
1526
}, []);
1627

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

2431
export default Adsense;

src/components/Analytics/analytics.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ export function Analytics(): React.ReactNode {
6767
analytics_storage: getAnalyticsStorageConsent(),
6868
});
6969

70+
if (
71+
process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID === "G-XXXXXXXXXX" ||
72+
!process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID
73+
) {
74+
console.info("No measurement ID provided, disabling Google Analytics");
75+
return;
76+
}
7077
if (getEnvironment() === "development") {
7178
if (process.env.NEXT_PUBLIC_ENABLE_ANALYTICS) {
7279
console.info("Enabling Google Analytics during development");

src/components/Analytics/specific.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ListLayout } from "@/components/AwesomeArcadeList/listLayout";
2+
13
export namespace AnalyticEvents {
24
export function sendAwesomeClick(type: "extension" | "tool", repo: string) {
35
window.gtag("event", "click_awesome", {
@@ -31,4 +33,17 @@ export namespace AnalyticEvents {
3133
variationId,
3234
});
3335
}
36+
37+
export function setUserPreferredTheme(
38+
theme: "light" | "dark" | "auto light" | "auto dark",
39+
) {
40+
// https://ithoughthecamewithyou.com/post/user-scoped-custom-dimensions-in-google-analytics-4-using-gtag
41+
console.log(`Setting user preferred theme to ${theme}`);
42+
window.gtag("set", "user_properties", { theme: theme });
43+
}
44+
45+
export function setUserPreferredLayout(layout: ListLayout) {
46+
console.log(`Setting user preferred layout to ${layout}`);
47+
window.gtag("set", "user_properties", { layout: layout });
48+
}
3449
}

src/components/AwesomeArcadeList/listLayout.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import { AnalyticEvents } from "@/components/Analytics/specific";
23

34
const ListLayouts = ["masonry", "grid", "github"] as const;
45
export type ListLayout = (typeof ListLayouts)[number];
@@ -28,12 +29,10 @@ export default function NavbarDropdownListLayoutPicker({
2829
const [layout, setLayout] = React.useState<ListLayout>("masonry");
2930

3031
React.useEffect(() => {
31-
const l = window.localStorage.getItem("listLayout");
32-
if (l) {
33-
setLayout(l as ListLayout);
34-
} else {
35-
setLayout("masonry");
36-
}
32+
const l = (window.localStorage.getItem("listLayout") ??
33+
"masonry") as ListLayout;
34+
setLayout(l);
35+
AnalyticEvents.setUserPreferredLayout(l);
3736
// eslint-disable-next-line react-hooks/exhaustive-deps
3837
}, []);
3938

src/components/Navbar/ThemePicker/themePicker.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import { AnalyticEvents } from "@/components/Analytics/specific";
23

34
export type Theme = "Light" | "Dark";
45
export type ThemeOptions = Theme | "Auto";
@@ -175,6 +176,16 @@ export function ThemeProxy({
175176
}),
176177
);
177178
window.localStorage.setItem("theme", dropdownTheme);
179+
180+
if (dropdownTheme === "Auto") {
181+
AnalyticEvents.setUserPreferredTheme(
182+
osWantsDark.matches ? "auto dark" : "auto light",
183+
);
184+
} else {
185+
AnalyticEvents.setUserPreferredTheme(
186+
dropdownTheme.toLowerCase() as "light" | "dark",
187+
);
188+
}
178189
}
179190
}, [dropdownTheme, loadedPreferredTheme]);
180191

0 commit comments

Comments
 (0)