Skip to content

Commit 7ff8f55

Browse files
feat: add AdSense slots for blog posts and implement debug component for local development
1 parent ff64b45 commit 7ff8f55

7 files changed

Lines changed: 64 additions & 3 deletions

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ PUBLIC_ADSENSE_CLIENT=
66
# Ad unit slot IDs (configured in your AdSense dashboard). Each is optional; unset slots render nothing.
77
PUBLIC_ADSENSE_SLOT_TOOL_INARTICLE=
88
PUBLIC_ADSENSE_SLOT_TOOL_FOOTER=
9+
PUBLIC_ADSENSE_SLOT_BLOG_INARTICLE=

.github/workflows/deploy.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ jobs:
3535
env:
3636
NODE_ENV: production
3737
PUBLIC_ADSENSE_CLIENT: ${{ secrets.PUBLIC_ADSENSE_CLIENT }}
38-
PUBLIC_ADSENSE_FOOTER_SLOT: ${{ secrets.PUBLIC_ADSENSE_FOOTER_SLOT }}
38+
PUBLIC_ADSENSE_SLOT_TOOL_INARTICLE: ${{ secrets.PUBLIC_ADSENSE_SLOT_TOOL_INARTICLE }}
39+
PUBLIC_ADSENSE_SLOT_TOOL_FOOTER: ${{ secrets.PUBLIC_ADSENSE_SLOT_TOOL_FOOTER }}
40+
PUBLIC_ADSENSE_SLOT_BLOG_INARTICLE: ${{ secrets.PUBLIC_ADSENSE_SLOT_BLOG_INARTICLE }}
3941

4042
- name: Build Pagefind index
4143
run: npx pagefind --site dist

src/components/AdSenseDebug.astro

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
/**
3+
* Dev-only debug banner showing AdSense env var status.
4+
* Only renders in local development (import.meta.env.DEV).
5+
* Helps troubleshoot why ad slots aren't appearing locally.
6+
*/
7+
8+
const isDev = import.meta.env.DEV;
9+
const adsenseClient = import.meta.env.PUBLIC_ADSENSE_CLIENT;
10+
const slotToolInArticle = import.meta.env.PUBLIC_ADSENSE_SLOT_TOOL_INARTICLE;
11+
const slotToolFooter = import.meta.env.PUBLIC_ADSENSE_SLOT_TOOL_FOOTER;
12+
const slotBlogInArticle = import.meta.env.PUBLIC_ADSENSE_SLOT_BLOG_INARTICLE;
13+
14+
const vars = [
15+
{ name: 'PUBLIC_ADSENSE_CLIENT', value: adsenseClient },
16+
{ name: 'PUBLIC_ADSENSE_SLOT_TOOL_INARTICLE', value: slotToolInArticle },
17+
{ name: 'PUBLIC_ADSENSE_SLOT_TOOL_FOOTER', value: slotToolFooter },
18+
{ name: 'PUBLIC_ADSENSE_SLOT_BLOG_INARTICLE', value: slotBlogInArticle },
19+
];
20+
---
21+
22+
{isDev && (
23+
<div class="fixed bottom-4 right-4 z-50 max-w-sm bg-yellow-50 dark:bg-yellow-900 border-2 border-yellow-300 dark:border-yellow-700 rounded-lg p-3 text-xs font-mono shadow-lg">
24+
<div class="font-bold text-yellow-900 dark:text-yellow-100 mb-2">
25+
AdSense Debug (Dev Only)
26+
</div>
27+
<div class="space-y-1">
28+
{vars.map((v) => (
29+
<div class="flex justify-between gap-2">
30+
<span class="text-yellow-800 dark:text-yellow-200 truncate">{v.name}</span>
31+
<span class={v.value ? 'text-green-700 dark:text-green-300 font-bold' : 'text-red-700 dark:text-red-300'}>
32+
{v.value ? 'set' : 'empty'}
33+
</span>
34+
</div>
35+
))}
36+
</div>
37+
<div class="text-yellow-700 dark:text-yellow-300 mt-2 text-[10px]">
38+
Set vars in .env to see ad slots render.
39+
</div>
40+
</div>
41+
)}

src/components/ads/AdSlot.astro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const {
3333
3434
const adsenseClient = import.meta.env.PUBLIC_ADSENSE_CLIENT;
3535
const adsEnabled = Boolean(adsenseClient);
36+
const isInArticleFluid = format === 'fluid' && layout === 'in-article';
37+
const adStyle = isInArticleFluid ? 'display:block; text-align:center;' : 'display:block';
3638
---
3739

3840
{adsEnabled && (
@@ -42,12 +44,12 @@ const adsEnabled = Boolean(adsenseClient);
4244
>
4345
<ins
4446
class="adsbygoogle block"
45-
style="display:block"
47+
style={adStyle}
4648
data-ad-client={adsenseClient}
4749
data-ad-slot={slot}
4850
data-ad-format={format}
4951
data-ad-layout={layout}
50-
data-full-width-responsive={responsive ? 'true' : 'false'}
52+
data-full-width-responsive={isInArticleFluid ? undefined : responsive ? 'true' : 'false'}
5153
></ins>
5254
<script is:inline>
5355
(window.adsbygoogle = window.adsbygoogle || []).push({});

src/env.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ interface ImportMetaEnv {
77
readonly PUBLIC_ADSENSE_SLOT_TOOL_INARTICLE?: string;
88
/** AdSense slot ID for the footer unit on tool pages. */
99
readonly PUBLIC_ADSENSE_SLOT_TOOL_FOOTER?: string;
10+
/** AdSense slot ID for the in-article unit on blog posts. */
11+
readonly PUBLIC_ADSENSE_SLOT_BLOG_INARTICLE?: string;
1012
}
1113

1214
interface ImportMeta {

src/layouts/ArticleLayout.astro

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import BaseLayout from '@layouts/BaseLayout.astro';
3+
import AdSlot from '@components/ads/AdSlot.astro';
34
45
export interface Props {
56
title: string;
@@ -33,6 +34,7 @@ const dateFormatter = new Intl.DateTimeFormat('en', {
3334
3435
const rawBase = import.meta.env.BASE_URL;
3536
const base = rawBase.endsWith('/') ? rawBase : `${rawBase}/`;
37+
const adBlogInArticleSlot = import.meta.env.PUBLIC_ADSENSE_SLOT_BLOG_INARTICLE;
3638
---
3739

3840
<BaseLayout
@@ -81,6 +83,15 @@ const base = rawBase.endsWith('/') ? rawBase : `${rawBase}/`;
8183
<slot />
8284
</div>
8385

86+
{adBlogInArticleSlot && (
87+
<AdSlot
88+
slot={adBlogInArticleSlot}
89+
format="fluid"
90+
layout="in-article"
91+
class="mx-auto my-6"
92+
/>
93+
)}
94+
8495
{relatedPosts.length > 0 && (
8596
<section class="space-y-4 border-t border-gray-200 pt-8 dark:border-gray-700" aria-labelledby="related-posts">
8697
<h2 id="related-posts" class="text-2xl font-bold text-gray-900 dark:text-white">Related Posts</h2>

src/layouts/Layout.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import '@/styles/global.css';
3+
import AdSenseDebug from '@components/AdSenseDebug.astro';
34
45
export interface Props {
56
title?: string;
@@ -38,6 +39,7 @@ const adsEnabled = Boolean(adsenseClient);
3839
</head>
3940
<body>
4041
<slot />
42+
<AdSenseDebug />
4143
</body>
4244
</html>
4345

0 commit comments

Comments
 (0)