Skip to content

Commit

Permalink
Merge pull request #171 from UnsignedArduino/staging
Browse files Browse the repository at this point in the history
External script optimization and more analytics
  • Loading branch information
UnsignedArduino authored Jul 13, 2024
2 parents caf1553 + ddd368f commit 6e8b0b1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Enable analytics during development - for debugging purposes
// Analytics are always enabled in non-dev environments
// Analytics are always enabled in non-dev environments if a measurement ID is provided
NEXT_PUBLIC_ENABLE_ANALYTICS=false
// Enables ads during development - for debugging purposes
// Ads are always enabled in non-dev environments
// Ads are always enabled in non-dev environments if a publisher ID is provided
NEXT_PUBLIC_ENABLE_ADS=false

// Google Analytics measurement ID - not required
Expand Down
17 changes: 12 additions & 5 deletions src/components/Adsense/adsense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import React from "react";
import { getEnvironment } from "@/scripts/Utils/Environment";

export function Adsense(): React.ReactNode {
const [useGA, setUseGA] = React.useState<boolean>(false);

React.useEffect(() => {
if (
process.env.NEXT_PUBLIC_ADSENSE_PUBLISHER_ID === "pub-XXXXXXXXXXXXXXXX" ||
!process.env.NEXT_PUBLIC_ADSENSE_PUBLISHER_ID
) {
// console.info("No publisher ID provided, disabling Google Adsense");
return;
}
if (getEnvironment() === "development") {
if (process.env.NEXT_PUBLIC_ENABLE_ADS) {
console.info("Enabling Google Adsense during development");
Expand All @@ -12,13 +21,11 @@ export function Adsense(): React.ReactNode {
return;
}
}

setUseGA(true);
}, []);

return getEnvironment() === "development" ? (
<></>
) : (
<GoogleAdSense publisherId="pub-XXXXXXXXXXXXXXXX" />
);
return useGA ? <></> : <GoogleAdSense publisherId="pub-XXXXXXXXXXXXXXXX" />;
}

export default Adsense;
7 changes: 7 additions & 0 deletions src/components/Analytics/analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ export function Analytics(): React.ReactNode {
analytics_storage: getAnalyticsStorageConsent(),
});

if (
process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID === "G-XXXXXXXXXX" ||
!process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID
) {
console.info("No measurement ID provided, disabling Google Analytics");
return;
}
if (getEnvironment() === "development") {
if (process.env.NEXT_PUBLIC_ENABLE_ANALYTICS) {
console.info("Enabling Google Analytics during development");
Expand Down
15 changes: 15 additions & 0 deletions src/components/Analytics/specific.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ListLayout } from "@/components/AwesomeArcadeList/listLayout";

export namespace AnalyticEvents {
export function sendAwesomeClick(type: "extension" | "tool", repo: string) {
window.gtag("event", "click_awesome", {
Expand Down Expand Up @@ -31,4 +33,17 @@ export namespace AnalyticEvents {
variationId,
});
}

export function setUserPreferredTheme(
theme: "light" | "dark" | "auto light" | "auto dark",
) {
// https://ithoughthecamewithyou.com/post/user-scoped-custom-dimensions-in-google-analytics-4-using-gtag
console.log(`Setting user preferred theme to ${theme}`);
window.gtag("set", "user_properties", { theme: theme });
}

export function setUserPreferredLayout(layout: ListLayout) {
console.log(`Setting user preferred layout to ${layout}`);
window.gtag("set", "user_properties", { layout: layout });
}
}
11 changes: 5 additions & 6 deletions src/components/AwesomeArcadeList/listLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import { AnalyticEvents } from "@/components/Analytics/specific";

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

React.useEffect(() => {
const l = window.localStorage.getItem("listLayout");
if (l) {
setLayout(l as ListLayout);
} else {
setLayout("masonry");
}
const l = (window.localStorage.getItem("listLayout") ??
"masonry") as ListLayout;
setLayout(l);
AnalyticEvents.setUserPreferredLayout(l);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
11 changes: 11 additions & 0 deletions src/components/Navbar/ThemePicker/themePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import { AnalyticEvents } from "@/components/Analytics/specific";

export type Theme = "Light" | "Dark";
export type ThemeOptions = Theme | "Auto";
Expand Down Expand Up @@ -175,6 +176,16 @@ export function ThemeProxy({
}),
);
window.localStorage.setItem("theme", dropdownTheme);

if (dropdownTheme === "Auto") {
AnalyticEvents.setUserPreferredTheme(
osWantsDark.matches ? "auto dark" : "auto light",
);
} else {
AnalyticEvents.setUserPreferredTheme(
dropdownTheme.toLowerCase() as "light" | "dark",
);
}
}
}, [dropdownTheme, loadedPreferredTheme]);

Expand Down

0 comments on commit 6e8b0b1

Please sign in to comment.