
Research
/Security News
Malicious npm Packages Target WhatsApp Developers with Remote Kill Switch
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
next-google-adsense
Advanced tools
Add Google AdSense to your Next.js app.
This package is deeply inspired by nextjs-google-adsense.
Why I don't use nextjs-google-adsense directly? Because it only support Auto Ads and Responsive Display Ad. I want to use In-article Ads. So I decided to create a new package. (read 👾 Why next-google-adsense? for more details)
Give me a ⭐ if you like it.
ads.txt
automatically (see Initialization / Verification for more details)next-google-adsense (this) | nextjs-google-adsense | |
---|---|---|
TypeScript | ✅ | ✅ |
Support Auto Ads | ✅ | ✅ |
Support Display Ad | ✅ | ✅ |
Support In-feed Ad | ✅ | ❌ |
Support In-article Ad | ✅ | ❌ |
Support Matched Content Ad | ✅ | ❌ |
Dynamic ads.txt | ✅ | ❌ |
Multiple ADs on one page | ✅ | ⚠️*1 |
*1: According to the their documentation seems it is ok to use multiple ADs on one page. But I found that it will cause an error.
npm install next-google-adsense
Visit the npm page.
There are two ways to verify your site (of course you can implement both):
AdSense code snippet
// pages/_app.tsx
// import the module
import { GoogleAdSense } from "next-google-adsense";
const App = ({ Component, pageProps }) => {
return (
<>
<GoogleAdSense publisherId="pub-XXXXXXXXXXXXXXXX" /> {/* 👈 16 digits */}
{/* or */}
<GoogleAdSense /> {/* if NEXT_PUBLIC_ADSENSE_PUBLISHER_ID is set */}
<Component {...pageProps} />
</>
);
};
export default App;
You can also add the publisherId
as environment variable as NEXT_PUBLIC_ADSENSE_PUBLISHER_ID
. The environment variable will override the prop if both are set.
Ads.txt snippet
// package.json
// ...
"scripts": {
"build": "next build && create-ads-txt", // 👈 if you want to create ads.txt automatically, recommended
"create-ads-txt": "create-ads-txt" // 👈 if you want to create ads.txt manually
},
// ...
⚠️ Your old
ads.txt
will be overwritten during the generation process.
You must set NEXT_PUBLIC_ADSENSE_PUBLISHER_ID
as environment variable. The environment variable will be used to generate the ads.txt
.
If you decide to use Auto Ads, you are good to go. The ads will be automatically displayed on your page. (Setup Auto Ads)
import { AdUnit } from "next-google-adsense";
const Page = () => {
return (
<>
<AdUnit
publisherId="pub-XXXXXXXXXXXXXXXX" {/* 👈 16 digits */}
slotId="XXXXXXXXXX" {/* 👈 10 digits */}
layout="display" {/* See the API Reference for more options */}
/>
{/* or */}
<AdUnit {/* if NEXT_PUBLIC_ADSENSE_PUBLISHER_ID is set */}
slotId="XXXXXXXXXX"
layout="display"
/>
<YourContent />
</>
);
};
export default Page;
<GoogleAdSense publisherId={string}>
Parameter | Optional | Type | Default | Description |
---|---|---|---|---|
publisherId | Yes | string | n/a | You can skip this parameter if you set the environment variable NEXT_PUBLIC_ADSENSE_PUBLISHER_ID . |
<AdUnit publisherId={string} slotId={string} layout={"display" | "in-article" | "custom"} customLayout={JSX.Element}>
Parameter | Optional | Type | Default | Description |
---|---|---|---|---|
publisherId | Yes | string | n/a | You can skip this parameter if you set the environment variable NEXT_PUBLIC_ADSENSE_PUBLISHER_ID . |
slotId | No | string | n/a | Google AdSense Slot ID. |
layout | Yes | "display" | "in-article" | "custom" | "display" | The layout of the AD. |
customLayout | Yes | JSX.Element | n/a | The custom layout of the AD. This parameter is required if layout is set to "custom". |
No layout fits your needs? Let's create a custom layout.
Sample custom layout:
export const InFeedAd = () => {
return (
<ins
className="adsbygoogle"
style={{ display: "block" }}
data-ad-format="fluid"
data-ad-layout-key="<AD_LAYOUT_KEY>"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"
/>
);
};
Example (provided by Google AdSense):
<script
async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
crossorigin="anonymous"
></script>
<ins
class="adsbygoogle"
style="display:block"
data-ad-format="fluid"
data-ad-layout-key="<AD_LAYOUT_KEY>"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"
></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
Remove all the <script>
tags.
You will get the following html:
<ins
class="adsbygoogle"
style="display:block"
data-ad-format="fluid"
data-ad-layout-key="<AD_LAYOUT_KEY>"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"
>
</ins>
Convert the html to JSX:
export const InFeedAd = () => {
return (
<ins
className="adsbygoogle"
style={{ display: "block" }}
data-ad-format="fluid"
data-ad-layout-key="<AD_LAYOUT_KEY>"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"
/>
);
};
Done! You can now use the custom layout in your app.
<AdUnit publisherId="<your-publisher-id>" slotId="<your-slot-id>" layout="custom" customLayout={InFeedAd}>
Full Code:
import { AdUnit } from "next-google-adsense";
export const InFeedAd = () => {
return (
<ins
className="adsbygoogle"
style={{ display: "block" }}
data-ad-format="fluid"
data-ad-layout-key="<AD_LAYOUT_KEY>"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"
/>
);
};
const Page = () => {
return (
<>
<AdUnit
publisherId="pub-XXXXXXXXXXXXXXXX" {/* 👈 16 digits */}
slotId="XXXXXXXXXX" {/* 👈 10 digits */}
layout="custom"
customLayout={InFeedAd}
/>
<YourContent />
</>
);
};
Contributions are welcome! If you find a bug or have a feature request, please open an issue. If you want to contribute code, please fork the repository and submit a pull request.
Before you open a pull request, please make sure that you run npm run dev:test
to make sure the code run as expected.
This project is licensed under the MIT License - see the LICENSE file for details
Love it? Consider a donation to support my work.
FAQs
Next.js Google AdSense
We found that next-google-adsense demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
Research
/Security News
Socket uncovered 11 malicious Go packages using obfuscated loaders to fetch and execute second-stage payloads via C2 domains.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.