-
Notifications
You must be signed in to change notification settings - Fork 2
/
ResponsiveAdUnit.tsx
53 lines (43 loc) · 1.19 KB
/
ResponsiveAdUnit.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
import React, { useEffect } from 'react';
import Script, { ScriptProps } from "next/script";
import { useRouter } from 'next/router'
declare const window: any;
type ResponsiveAdUnitProps = {
publisherId: string;
slotId: string;
type?: string;
style?: any;
};
const initAd = () => {
(window.adsbygoogle = window.adsbygoogle || []).push({});
};
export function ResponsiveAdUnit({
publisherId,
slotId,
type = "default-ad-unit-type",
style = {}
}: ResponsiveAdUnitProps): JSX.Element | null {
useEffect(() => {
initAd();
})
const _publisherId =
process.env.NEXT_PUBLIC_ADSENSE_PUBLISHER_ID ?? publisherId;
if(!_publisherId || !slotId){
console.error("nextjs-google-adsense: publisherId or slotId can't be empty for the unit");
return null;
}
style.display = "block";
const router = useRouter()
return (
<div key={router.asPath.replace(/\//g, "-") + "-" + slotId + "-" + type} style={style}>
<ins
className="adsbygoogle"
style={{ display: 'block' }}
data-ad-client={`ca-${_publisherId}`}
data-ad-slot={String(slotId)}
data-ad-format="auto"
data-full-width-responsive="true"
/>
</div>
);
}