Skip to content

Commit d53910f

Browse files
codercatdevAlex Patterson
andauthored
add google ads (#590)
* add google ads * include Ads.txt * fix post date issue --------- Co-authored-by: Alex Patterson <alex.patterson@fusionauth.io>
1 parent e494b3a commit d53910f

4 files changed

Lines changed: 109 additions & 35 deletions

File tree

‎app/(main)/(post)/post/[slug]/page.tsx‎

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { groq, type PortableTextBlock } from "next-sanity";
33
import { notFound } from "next/navigation";
44
import { Suspense } from "react";
55

6+
//Google Ads
7+
import dynamic from "next/dynamic";
8+
const GoogleAdBanner = dynamic(() => import("@/components/google-ad-banner"), {
9+
ssr: false,
10+
});
11+
612
import Avatar from "@/components/avatar";
713
import DateComponent from "@/components/date";
814
import MoreContent from "@/components/more-content";
@@ -15,6 +21,7 @@ import { resolveOpenGraphImage } from "@/sanity/lib/utils";
1521
import CoverMedia from "@/components/cover-media";
1622
import MoreHeader from "@/components/more-header";
1723
import { BreadcrumbLinks } from "@/components/breadrumb-links";
24+
import SponsorCard from "@/components/sponsor-card";
1825

1926
type Props = {
2027
params: { slug: string };
@@ -66,11 +73,18 @@ export default async function PostPage({ params }: Props) {
6673
<h1 className="mb-12 text-4xl font-bold leading-tight tracking-tighter text-balance md:text-7xl md:leading-none lg:text-8xl">
6774
{post.title}
6875
</h1>
69-
<div className="hidden md:mb-12 md:block">
70-
<div className="flex flex-col gap-8">
71-
{post?.author && (
72-
<div className="flex">
73-
{post.author.map((a) => (
76+
<div className="mb-8 sm:mx-0 md:mb-16">
77+
<CoverMedia
78+
cloudinaryImage={post?.coverImage}
79+
cloudinaryVideo={post?.videoCloudinary}
80+
youtube={post?.youtube}
81+
/>
82+
</div>
83+
<div className="max-w-2xl sm:max-w-none">
84+
<div className="mb-6">
85+
{(post?.author) && (
86+
<div className="flex flex-wrap gap-2">
87+
{post?.author?.map((a) => (
7488
<Avatar
7589
key={a._id}
7690
name={a.title}
@@ -80,39 +94,28 @@ export default async function PostPage({ params }: Props) {
8094
))}
8195
</div>
8296
)}
83-
<div className="text-lg">
84-
<DateComponent dateString={post.date} />
85-
</div>
8697
</div>
87-
</div>
88-
<div className="mb-8 sm:mx-0 md:mb-16">
89-
<CoverMedia
90-
cloudinaryImage={post?.coverImage}
91-
cloudinaryVideo={post?.videoCloudinary}
92-
youtube={post?.youtube}
93-
/>
94-
</div>
95-
<div className="block md:hidden">
96-
<div className="max-w-2xl mx-auto">
97-
<div className="mb-6">
98-
{post.author && (
99-
<div className="flex">
100-
{post.author.map((a) => (
101-
<Avatar
102-
key={a._id}
103-
name={a.title}
104-
href={`/author/${a?.slug}`}
105-
coverImage={a?.coverImage}
106-
/>
107-
))}
108-
</div>
109-
)}
110-
</div>
111-
<div className="mb-4 text-lg">
112-
<DateComponent dateString={post.date} />
113-
</div>
98+
<div className="mb-4 text-lg">
99+
<DateComponent dateString={post.date} />
114100
</div>
115101
</div>
102+
{post?.sponsor?.length ? (
103+
<section className="flex flex-col mt-10 mb-10">
104+
<h2 className="mb-4 text-2xl font-bold">Sponsors</h2>
105+
<hr className="border-accent-2" />
106+
<div className="my-12 ">
107+
<SponsorCard sponsors={post.sponsor} />
108+
</div>
109+
<hr className="border-accent-2" />
110+
</section>
111+
) : (
112+
<GoogleAdBanner
113+
data-ad-slot="slotnumber"
114+
data-full-width-responsive="true"
115+
data-ad-layout="in-article"
116+
data-ad-format="fluid"
117+
/>
118+
)}
116119
{post.content?.length && (
117120
<PortableText
118121
className="mx-auto prose-violet lg:prose-xl dark:prose-invert"

‎app/(main)/layout.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import AlgoliaDialog from "@/components/algolia-dialog";
2727
import { FaBars } from "react-icons/fa6";
2828
import PlayerFloating from "@/components/player-floating";
2929
import { PlayerProvider } from "@/components/player-context";
30+
import Script from "next/script";
3031

3132
const nunito = Nunito({
3233
subsets: ["latin"],
@@ -138,6 +139,12 @@ export default async function RootLayout({
138139
<SpeedInsights />
139140
</ThemeProvider>
140141
</PlayerProvider>
142+
<Script
143+
async
144+
src={`https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=${process.env.NEXT_PUBLIC_GOOGLE_ADS_CLIENT_ID}`}
145+
strategy="lazyOnload"
146+
crossOrigin="anonymous"
147+
></Script>
141148
</body>
142149
</html>
143150
);

‎components/google-ad-banner.tsx‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"use client"
2+
import Router from "next/router";
3+
import { useEffect } from "react";
4+
declare global {
5+
interface Window {
6+
adsbygoogle: unknown[];
7+
}
8+
}
9+
10+
interface AdsBannerProps {
11+
"data-ad-slot": string;
12+
"data-ad-format": string;
13+
"data-full-width-responsive": string;
14+
"data-ad-layout"?: string;
15+
}
16+
17+
const GoogleAdBanner = (props: AdsBannerProps) => {
18+
useEffect(() => {
19+
const handleRouteChange = () => {
20+
const intervalId = setInterval(() => {
21+
try {
22+
// Check if the 'ins' element already has an ad in it
23+
if (window.adsbygoogle) {
24+
window.adsbygoogle.push({});
25+
clearInterval(intervalId);
26+
}
27+
} catch (err) {
28+
console.error("Error pushing ads: ", err);
29+
clearInterval(intervalId); // Ensure we clear interval on errors too
30+
}
31+
}, 100);
32+
return () => clearInterval(intervalId); // Clear interval on component unmount
33+
};
34+
35+
// Run the function when the component mounts
36+
handleRouteChange();
37+
38+
// Subscribe to route changes
39+
if (typeof window !== "undefined") {
40+
Router.events.on("routeChangeComplete", handleRouteChange);
41+
42+
// Unsubscribe from route changes when the component unmounts
43+
return () => {
44+
Router.events.off("routeChangeComplete", handleRouteChange);
45+
};
46+
}
47+
}, []);
48+
49+
return (
50+
<ins
51+
className="adsbygoogle adbanner-customize mt-2"
52+
style={{
53+
display: "block",
54+
overflow: "hidden",
55+
border: process.env.NODE_ENV === "development" ? "1px solid red" : "none",
56+
}}
57+
data-adtest="on"
58+
data-ad-client={process.env.NEXT_PUBLIC_GOOGLE_ADS_CLIENT_ID}
59+
{...props}
60+
/>
61+
);
62+
};
63+
export default GoogleAdBanner;

‎public/Ads.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google.com, pub-8603586563858288, DIRECT, f08c47fec0942fa0

0 commit comments

Comments
 (0)