Skip to content

Commit

Permalink
feat: integrate google adsense
Browse files Browse the repository at this point in the history
  • Loading branch information
adarshaacharya committed Jun 12, 2024
1 parent b3cc04f commit 2e50ced
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { siteMetadata } from "@/data/siteMetadata";
import Head from "./head";
import { GoogleAnalytics } from "@next/third-parties/google";
import { ENV } from "@/lib/env";
import { GoogleAdsense } from "@/components/google-adsense";

const space_grotesk = Space_Grotesk({
subsets: ["latin"],
Expand Down Expand Up @@ -79,6 +80,7 @@ export default function RootLayout({
<TailwindIndicator />
</ThemeProvider>
<GoogleAnalytics gaId={ENV.GOOGLE_ANALYTICS_ID} />
<GoogleAdsense pId={ENV.ADSENSE_CLIENT_ID} />
</body>
</html>
);
Expand Down
17 changes: 17 additions & 0 deletions src/components/google-adsense.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ENV } from "@/lib/env";
import Script from "next/script";

export const GoogleAdsense = ({ pId }: { pId: string }) => {
if (process.env.NODE_ENV !== "production") {
return null;
}

return (
<Script
async
src={`https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-${pId}`}
crossOrigin="anonymous"
strategy="afterInteractive"
/>
);
};
1 change: 1 addition & 0 deletions src/lib/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const ENV = {
GOOGLE_ANALYTICS_ID: process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID!,
NODE_ENV: process.env.NODE_ENV!,
ADSENSE_CLIENT_ID: process.env.NEXT_PUBLIC_ADSENSE_CLIENT_ID!,
};

0 comments on commit 2e50ced

Please sign in to comment.