-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-ad-unit.tsx
56 lines (49 loc) · 1.43 KB
/
google-ad-unit.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use client'
import React, { Fragment, useEffect } from 'react'
import { usePathname, useSearchParams } from 'next/navigation'
import { Suspense } from 'react'
import Script from 'next/script'
type Props = {
slot: string
client: string
layout?: undefined | "in-article" | "in-feed" | "display"
format?: "square" | "horizontal" | "vertical" | "auto" | "fluid"
}
declare global {
interface Window {
adsbygoogle?: any | any[]
}
}
const GoogleAd = ({ slot, client, layout, format="auto" }: Props) => {
const pathname = usePathname()
const searchParams = useSearchParams()
useEffect(() => {
try {
(window.adsbygoogle = window.adsbygoogle || []).push({})
} catch (err) {
console.error(err)
}
}, [pathname, searchParams])
return <Fragment>
<Script
async
src={`https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=${client}`}
strategy="lazyOnload"
crossOrigin="anonymous"
/>
<ins className="adsbygoogle"
style={{ display: "block" }}
data-ad-layout={ layout }
data-ad-client={ client }
data-ad-slot={ slot }
data-ad-format={ format }
data-full-width-responsive="true">
</ins>
</Fragment>
}
export const GoogleAdUnit = ({ slot, client, format, layout }: Props) => {
return <Suspense>
<GoogleAd slot={slot} client={client} format={format} layout={layout} />
</Suspense>
}
export default GoogleAdUnit