Get Your Own AdManager a Centralized Way to Manage Ads
AdManager Part 1: The Origin – Why I Needed a Custom Ads Management System
Managing ads across more than 10,000 Blogspot pages is no joke. I had been juggling various affiliate programs – Amazon, AliExpress, Booking, local retailers – each with different codes, banner formats, and end dates. What’s worse? I had no centralized way to monitor, update, or remove them once a partnership expired. The result? Outdated ads, broken links, and lost revenue.
🤯 The Problems I Faced
- Scattered ad codes: Embedded manually on every page.
- No expiry logic: Couldn't deactivate ads when affiliate deals ended.
- Revenue leakage: Some links were dead or redirected incorrectly.
- Manual effort: Updating ads meant editing hundreds of HTML pages.
💡 The Breakthrough Moment
One day, I asked myself: What if I could manage all my ad codes in one Google Sheet, and use a bit of Apps Script magic to control what appears on each post?
This was the seed for AdManager – a centralized, Google Sheets-powered ad delivery system that would read post labels and insert the correct ad code dynamically.
🚀 Need a Custom AdManager for Your Blog?
Let me build you a fully functional, easy-to-manage AdManager system that puts you in control of your blog's monetization — no coding needed, scalable, and budget-friendly.
✨ Live Examples:
- Type 1 – Manual Label-Based Ads: You assign a custom
data-label
to each container. - <div data-label="How to do blogging" id="adContainer1"></div>
- <div data-label="How to do blogging" id="adContainer2"></div>
- Type 2 – Automatic Ads by Post Labels: The system detects your post's labels and shows related ads.
- <div id="autoAdContainer"></div>
- Type 3 – Embed-Anywhere Ads: Use a compact script and container to embed ads on any site or post.
- <div class='embedAd' data-label='Barcelona Hotel' data-width='300px' data-height='250px'></div>
- <script async src='https://yourdomain.com/path-to/embed-ad-script.js'></script>
💌 Ready to boost your blog’s ad performance? Let’s make it happen! Just send me a message — I’ll handle the tech while you focus on your content.
🎯 The Vision
Build a tool where:
- I control all ads from a single Google Sheet.
- Each post shows ads based on its labels (e.g., “Fashion” shows a Zalando banner).
- I can disable any ad instantly by removing it from the Sheet.
- Tracking and updating affiliate links becomes painless.
🔍 What You’ll Learn in This Series
- How to structure the Google Sheet to hold your ad logic.
- How to write the Apps Script that powers ad display.
- How to auto-inject the script into thousands of Blogspot posts.
- How to troubleshoot performance, errors, and tracking.
Ready to take control of your ad revenue? In Part 2, I’ll show you how I structured the Sheet, defined my labels, and created the logic to match ads with Blogspot content. It’s simpler than you think!
Next up: AdManager Part 2 – Structuring the Google Sheet for Success
AdManager Part 2 – Structuring the Google Sheet for Success
Once I knew I wanted centralized control over my ad placements, I had to figure out what kind of data structure would power the system. After testing multiple formats, I came up with a simple, scalable setup using just one tab in Google Sheets: Sheet1.
📊 Sheet Layout
The columns I used:
- Label – The Blogspot label to match (e.g., "Fashion", "Tech").
- AdCode – The actual HTML/JavaScript snippet for the ad.
- Status – Set to "on" or "off" to activate or deactivate the ad.
- StartDate – Optional: When to start showing the ad.
- EndDate – Optional: When to stop showing the ad.
🧠 Why This Format Works
- Simple for humans to manage.
- Scalable to hundreds of ads.
- Easy for Apps Script to read and process.
📌 Sample Sheet
Label | AdCode | Status | StartDate | EndDate |
---|---|---|---|---|
Fashion | <script src="https://fashionads.com/ad1.js"></script> | on | 2025-01-01 | 2025-12-31 |
Tech | <a href="https://techaffiliate.com/deal"><img src="https://techaffiliate.com/banner.jpg" /></a> | off |
⚠️ Pro Tips
- Use plain text for ad code – no extra formatting in the cell.
- Keep your label names consistent with Blogspot tags.
- Use "off" to instantly stop showing an ad on all posts.
In Part 3, I’ll walk you through writing the Apps Script that connects your blog posts to this Sheet and dynamically injects the correct ad code.
Next up: AdManager Part 3 – Writing the Script That Powers It All
AdManager Part 3 – Writing the Script That Powers It All
Now that the Sheet is ready, it's time to connect the logic: Google Apps Script. This part is the brain of the operation—it reads the Sheet, looks at a Blogspot post’s labels, and shows the appropriate ads.
⚙️ Script Goals
- Read ad data from the AdManager → Sheet1 tab.
- Compare each row’s Label against the current page’s labels.
- Filter only active ads (status = “on” and valid date range).
- Output the matching ad codes.
📜 The Apps Script
Add this code in Extensions → Apps Script:
function doGet(e) {
const sheet = SpreadsheetApp.openById('YOUR_SHEET_ID').getSheetByName('Sheet1');
const data = sheet.getDataRange().getValues();
const query = e.parameter.labels ? e.parameter.labels.split(',') : [];
const today = new Date();
let output = '';
for (let i = 1; i < data.length; i++) {
const [label, adCode, status, start, end] = data[i];
if (status !== 'on') continue;
if (start && new Date(start) > today) continue;
if (end && new Date(end) < today) continue;
if (!query.includes(label)) continue;
output += adCode + '\n';
}
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.TEXT);
}
Make sure to replace 'YOUR_SHEET_ID'
with your actual Google Sheet ID.
🌐 Deploy the Script as Web App
- Go to Deploy → Test deployments.
- Choose type: Web app.
- Execute as: Me.
- Who has access: Anyone.
- Click Deploy, and authorize if needed.
- Copy your web app URL – you'll use this in the frontend.
✅ Output Example
Requesting:
https://script.google.com/macros/s/XXXXXXXXX/exec?labels=Fashion,Tech
Returns the ad codes for “Fashion” and “Tech” if they’re active.
Next up: AdManager Part 4 – Injecting the Script into 10,000 Blogspot Pages
AdManager Part 4 – Injecting the Script into 10,000 Blogspot Pages
Now comes the exciting part: delivering the ads to all your Blogspot posts automatically. This step requires inserting a small script into your Blogspot theme or each post’s HTML to load the ad dynamically using your Apps Script Web App.
🎯 Goal
- Each Blogspot post detects its own labels (tags).
- The script sends those labels to your Web App.
- The Web App returns matching ad code(s).
- The script injects them into a specific location on the page.
📥 Step-by-Step Injection Script
Add this JavaScript snippet into your Blogspot theme or inside each post (in the HTML view):
<script>
(function() {
const labels = [...document.querySelectorAll('.post-labels a')].map(el => el.textContent.trim()).join(',');
const url = 'https://script.google.com/macros/s/YOUR_SCRIPT_ID/exec?labels=' + encodeURIComponent(labels);
fetch(url)
.then(response => response.text())
.then(code => {
const container = document.getElementById('admanager-slot');
if (container) container.innerHTML = code;
});
})();
</script>
🪄 HTML Placeholder for Ads
Where you want the ads to show up, insert this:
<div id="admanager-slot"></div>
📌 Tips for Massive Scale
- Use Blogspot Layout → Theme → Edit HTML to inject the script site-wide.
- Or use Apps Script + Blogger API to bulk update post HTML (advanced).
✅ Final Result
Every post automatically pulls and displays ads matching its labels. You never have to manually update an ad again.
Next up: AdManager Part 5 – Managing Expired Ads & Troubleshooting
AdManager Part 5 – Managing Expired Ads & Troubleshooting
With 10,000+ pages and multiple affiliate partnerships, it's crucial to ensure ads don’t stay live after they expire. This part explains how to manage ad validity using date filters, and how to fix common issues.
🗓️ Managing Expired Ads
Remember the StartDate
and EndDate
fields in your Google Sheet? Here’s how the script uses them:
- Only show ads where Status = on
- StartDate ≤ today ≤ EndDate (if dates are provided)
This ensures your ads disappear on time. If you leave dates blank, the ad is always eligible—controlled only by the Status field.
🧪 Troubleshooting Common Problems
1. Ads not appearing?
- Check the label names – must exactly match Blogspot post tags.
- Ensure the Web App URL is correct in your script.
- Confirm the ad is set to "on" and within valid date range.
2. Empty HTML returned?
- Verify that
labels=
is correctly constructed in the fetch URL. - Use
Logger.log(query)
in the script to debug parameters.
3. Script not running at all?
- Check for JavaScript errors in the browser console (F12).
- Ensure Blogspot theme includes the JS snippet and ad placeholder.
🚦 Version Control for Ad Partners
If an affiliate ends the program or changes their code, just remove or update the ad row in your Sheet. No need to touch any post code.
📈 Bonus Tip: Tracking Performance
Add tracking parameters (like UTM codes) to your ad links or use an external redirect script to count clicks per ad ID. You can even extend the Sheet to include a Clicks
column updated via Google Analytics or Firebase.
Next up: AdManager Part 6 – Bonus: Future Ideas & Automation Tips
AdManager Part 6 – Bonus: Future Ideas & Automation Tips
With the AdManager system up and running, there’s still more you can do to improve automation, scaling, and ad performance. Here are some advanced ideas and tips to keep growing.
🤖 Auto Deactivation with Google Apps Script Triggers
Create a time-based trigger that runs daily and automatically sets ads to "off" if their EndDate has passed.
function autoDeactivateAds() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
const data = sheet.getDataRange().getValues();
const today = new Date();
for (let i = 1; i < data.length; i++) {
let end = data[i][4]; // EndDate
if (end && new Date(end) < today && data[i][2] === 'on') {
sheet.getRange(i+1, 3).setValue('off'); // Status column
}
}
}
Set this function to run with a trigger from Triggers → Add Trigger → Time-driven.
📦 Add Categories or Ad Types
Extend your Sheet with a Type column (e.g., "banner", "sidebar", "in-post"). Then adjust the script to load specific ad types into specific page areas.
🔍 Track Impressions and Clicks
- Add
onAdShown()
to count when an ad is injected. - Redirect click URLs through a tracker to count conversions.
Use Firebase, Google Analytics, or custom logs stored in another Google Sheet.
🔄 Multiple Ad Slots per Post
Define more than one placeholder, like:
<div id="admanager-top"></div>
<div id="admanager-bottom"></div>
And adjust your Apps Script to return specific ads by position.
🎉 Conclusion
This AdManager system started from a single need—controlling ads without manually editing 10,000+ pages—and evolved into a powerful, flexible tool that can grow as your site and partnerships grow.
Keep optimizing, automating, and building smarter tools. That’s what makes your blog sustainable in the long run. 🚀
Comments
Post a Comment