Integrate Google AdSense in NextJS
1 - Sign up for a Google AdSense account
Before starting, make sure you have a Google AdSense account. If not, sign up at https://www.google.com/adsense/.
2 - Create an AdSense component
Create a React component to hold the AdSense ad code. Create a new tsx
file inside the /components folder and name it AdSense.tsx
src/components/AdSense.tsx
3 - Add AdSense script to _document.tsx
_document.tsx
Add the AdSense script to inside the <Head>
tag of the src/pages/_document.tsx
file to ensure it's loaded on every page.
Note: Replace ca-pub-XXXXXXXXXXXXXXXX
with your actual AdSense publisher ID.
Note: Replace ca-pub-XXXXXXXXXXXXXXXX
with your actual AdSense publisher ID.
4 - Use the AdSense component in pages
Now you can use the AdSense component in any page of your Next.js application.
You can find WordPress Template System pages inside the /src/wp-templates/
folder.
And you can find other pages like: Author page, Seach, Reading list, Submission, ... inside the /src/pages/
folder.
5 - Submit your website for review
After integrating AdSense into your website, submit it for review in your AdSense account.
Important notes:
Make sure to comply with all Google AdSense policies to avoid rejection or account suspension.
Consider the performance impact when adding ads. Too many ads can slow down your website.
Always test thoroughly after implementation to ensure everything works as expected.
Last updated