'Problem to put google adsense ads on site in next js
I have a problem placing google adsense ads on the site in next js
I found these codes on the internet but it is not working. I do not know what else to do
The first code is a component in which it is to be placed wherever there is an advertisement
import React, { useEffect } from "react";
export default function Adsense() {
const loadAds = () => {
try {
if (typeof window !== "undefined") {
(window.adsbygoogle = window.adsbygoogle || []).push({});
}
} catch (error) {
console.log("adsense error", error.message);
}
};
useEffect(() => {
loadAds();
}, []);
return (
<ins
className="adsbygoogle"
style={{ display: "block" }}
data-ad-client="ca-pub-xxxxxxxxx"
data-ad-slot="xxxxxxxxxx"
data-ad-format="auto"
data-full-width-responsive="true"
></ins>
);
}
The second is to put in _document.js
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" />
<script dangerouslySetInnerHTML={{
__html: `
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "xxxxxxxxxx",
enable_page_level_ads: true
});
`,
}} />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
