Coding, Insights, and Digital Discoveries 👩🏻💻
From Stay-at-Home Mom to a Developer After 50
Imagine turning your blog or website into a money-making machine with almost zero effort. That's exactly what Google AdSense does! It's like having a tiny money-making robot that works 24/7 on your website.
When people visit your site, Google automatically shows ads that match your content. Every time someone clicks an ad, you earn money. It's that simple! Even small websites can start earning between $100 to $500 per month, and popular sites can make thousands.
Next.js is like a Swiss Army knife for building websites. It's super fast, easy to use, and makes your site look professional with minimal effort. Here's why it's awesome:
First, create a Google AdSense account. Once approved, Google will give you a special code snippet that looks like this:
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"></script>
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
Create a new file called AdSense.tsx
in your Next.js project:
"use client";
import React, { useEffect } from "react";
const AdSense = () => {
useEffect(() => {
// Safely load AdSense script
if (typeof window !== "undefined") {
const script = document.createElement("script");
script.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX";
script.async = true;
document.head.appendChild(script);
}
}, []);
return (
<ins
className="adsbygoogle"
style={{ display: "block" }}
data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXX"
data-ad-format="auto"
data-full-width-responsive="true"
></ins>
);
};
export default AdSense;
Create a global.d.ts
file to help TypeScript understand AdSense:
declare global {
interface Window {
adsbygoogle: any[];
}
}
export {};
In your next.config.js
, add these settings to allow AdSense:
module.exports = {
async headers() {
return [
{
source: '/:path*',
headers: [
{
key: 'Content-Security-Policy',
value: `
script-src 'self' https://pagead2.googlesyndication.com;
img-src * data:;
`,
},
],
},
];
},
};
This is a simplified version of next.config file, you may need to add more directives into CSP (Content Security Policy). I debugged a lot using Google's dev tool to track down what other URLs from Google should be added into the file.
Adding Google AdSense to your website sounds simple, but it can quickly become a technical nightmare. I specialize in turning your monetization dreams into reality. I offer professional assistance to:
Quick, hassle-free AdSense setup for your website. Let's get you earning! 💰💰💰