npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nextjs-google-adsense

v1.0.0

Published

Google AdSense for Next.js

Downloads

392

Readme

Nextjs Google AdSense

Structure of this package is based on nextjs-google-analytics. Credits to @MauricioRobayo.

Google AdSense for Next.js

This package optimizes script loading using Next.js Script tag, which means that it will only work on apps using Next.js >= 11.0.0.

Installation

npm install --save nextjs-google-adsense
# or
yarn add nextjs-google-adsense

Initializing AdSense

Add the GoogleAdSense component with the publisherId prop set to your Publisher ID (optained from Google Adsense panel) to your custom App file:

// pages/_app.js
import { GoogleAdSense } from "nextjs-google-adsense";

const App = ({ Component, pageProps }) => {
  return (
    <>
      <GoogleAdSense publisherId="pub-XXXXXXXXXXXXXXXX" />
      <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.

This component will inject the script for AdSense units to be rendered with Next.js's Script afterInteractive strategy.

Usage

You can use automatic placements by Google AdSense or add manual ad units using this package.

Auto Ads (Recommended)

After initializing the AdSense, most effective way for ad unit placements is Auto Ads. Gives the best results for most use cases. Let Google's $B AI do the placement.

In order to activate Auto Ads, you need to login your Google AdSense panel and enable it for the particular domain name you are using for your Next.js project.

Google AdSense Panel > Ads (Sidebar) > By site (Tab) > Press Edit icon for your domain > Ad Settings > Expand Auto Ads and enable it > Enable Optimize exiting units

Additionally, options like Anchor ads and Side rails can be managed under "Ad Formats" accordion menu and you don't need to make any changes in your Next.js project for it.

Note that, Auto Ads might take a while to show up, and optimize itself depending on user behaviour.

Manual Ad Unit Placement

In order to place ads manually, use <ResponsiveAdUnit /> in any of your pages/ files or components.

Create a new Display ad unit;

Google AdSense Panel > Ads (Sidebar) > By ad unit (Tab) > Display ads > Name your unit, Choose "Responsive" > Create

// pages/index.js (or any component)
import { ResponsiveAdUnit } from "nextjs-google-adsense";

export default function Home() {
  return (
    <>
      <h1>My Home Page</h1>
      <p>Welcome to my homepage. This is hero text</p>

      <ResponsiveAdUnit
        publisherId="pub-XXXXXXXXXXXXXXXX"
        slotId="XXXXXXXXXX"
        type="after-home-hero"/>

      <p>This is my home page content</p>
      <p>Way more content here</p>

      <ResponsiveAdUnit
        publisherId="pub-XXXXXXXXXXXXXXXX"
        slotId="XXXXXXXXXX"
        type="end-of-homepage"/>
    </>
  );
};

export default Home;

Copy your data-ad-slot="XXXXXXXXXX" value into the component as slotId prop. You can omit publisherId if you defined NEXT_PUBLIC_ADSENSE_PUBLISHER_ID environment variable. The environment variable will override the prop if both are set.

The ad unit automatically gets re-rendered when the route changes. The type prop id used as key when there are multiple ad units in the page addition to the current router.asPath string. You can add a slug defining the unit's position on the page in order to differentiate a unit from others. Additionally you can re-render the ad unit by changing type prop after page load.

ResponsiveAdUnit also accepts style prop.

// TODO: Add "fixed" unit type, passing it for now since it is not prefered a lot.

Debugging your Google AdSense

// TODO: Add some steps here in order to debug it, feel free to PR. (Use gtag console and debug={true} prop for <GoogleAdSense/>)

Notes

Ignore AdSense head tag doesn't support data-nscript attribute. warning.

Contributing

Contributions, issues and feature requests are welcome!

Show your support

Give a ⭐️ if you like this project!

LICENSE

This software and maintainers are not affiliated with Google AdSense, Next.js or Vercel. And authors offer no warranties.

MIT