mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-09-02 13:52:46 +02:00
Add google ad targeting
This commit is contained in:
@@ -1,6 +1,69 @@
|
|||||||
|
---
|
||||||
|
const page = Astro.url;
|
||||||
|
---
|
||||||
|
|
||||||
<script async src='https://securepubads.g.doubleclick.net/tag/js/gpt.js'
|
<script async src='https://securepubads.g.doubleclick.net/tag/js/gpt.js'
|
||||||
></script>
|
></script>
|
||||||
<script async>
|
<script async>
|
||||||
|
const ignoredPages = [
|
||||||
|
'login',
|
||||||
|
'signup',
|
||||||
|
'roadmaps',
|
||||||
|
'best-pracitces',
|
||||||
|
'guides',
|
||||||
|
'videos',
|
||||||
|
'roadmaps',
|
||||||
|
'community',
|
||||||
|
'start-here',
|
||||||
|
'ai',
|
||||||
|
'ai-tutor',
|
||||||
|
'teams',
|
||||||
|
'about',
|
||||||
|
'account',
|
||||||
|
'projects',
|
||||||
|
'questions',
|
||||||
|
'guides',
|
||||||
|
'advertise',
|
||||||
|
];
|
||||||
|
|
||||||
|
function sanitizeSettingValue(value) {
|
||||||
|
return (
|
||||||
|
String(value)
|
||||||
|
.trim()
|
||||||
|
// Remove characters forbidden at https://support.google.com/admanager/answer/10020177.
|
||||||
|
.replace(/'|\\|'|=|!|#|\*|~|;|\^|\(|\)|<|>|\[|\]|,|&/g, '')
|
||||||
|
// Extra spaces aren't forbidden, but rarely desired.
|
||||||
|
.replace(/\s+/, ' ')
|
||||||
|
// The + sign is also forbidden, but is being replaced with 'and' instead.
|
||||||
|
.replace('+', 'and')
|
||||||
|
// Maximum length of 40 for values.
|
||||||
|
.substring(0, 40)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const page = window.location.pathname;
|
||||||
|
const pageParts = page.split('/').filter(Boolean);
|
||||||
|
|
||||||
|
const isRoadmapPage =
|
||||||
|
pageParts.length === 1 && !ignoredPages.includes(pageParts[0]);
|
||||||
|
const isBestPracticesPage =
|
||||||
|
pageParts.length === 2 && pageParts[0] === 'best-practices';
|
||||||
|
|
||||||
|
let adSettings = {};
|
||||||
|
if (isRoadmapPage) {
|
||||||
|
adSettings = {
|
||||||
|
post_id: sanitizeSettingValue(pageParts[0]),
|
||||||
|
page_type: 'roadmap',
|
||||||
|
category: ['roadmap', sanitizeSettingValue(pageParts[0])],
|
||||||
|
};
|
||||||
|
} else if (isBestPracticesPage) {
|
||||||
|
adSettings = {
|
||||||
|
post_id: sanitizeSettingValue(pageParts[1]),
|
||||||
|
page_type: 'best-practice',
|
||||||
|
category: ['best-practice', sanitizeSettingValue(pageParts[1])],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
window.googletag = window.googletag || { cmd: [] };
|
window.googletag = window.googletag || { cmd: [] };
|
||||||
googletag.cmd.push(function () {
|
googletag.cmd.push(function () {
|
||||||
@@ -18,6 +81,14 @@
|
|||||||
'div-gpt-ad-1742391132948-0',
|
'div-gpt-ad-1742391132948-0',
|
||||||
)
|
)
|
||||||
.addService(googletag.pubads());
|
.addService(googletag.pubads());
|
||||||
|
|
||||||
|
// Set targeting for all ad slots on the page.
|
||||||
|
for (let key in adSettings) {
|
||||||
|
if (adSettings.hasOwnProperty(key)) {
|
||||||
|
googletag.pubads().setTargeting(key, adSettings[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
googletag.enableServices();
|
googletag.enableServices();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
Reference in New Issue
Block a user