仅用于记录谷歌广告在代码端的开发
google广告
AFS广告主要是指嵌入在搜索引擎上的广告,用户在进行搜索时看到的广告,与搜索关键词息息相关。
AFC广告则是展示在网页内容中的广告,与网页的文本内容相关。
AFC类型广告
**AFC广告则是展示在网页内容中的广告,与网页的文本内容相关。**
- 当只引入AFC广告链接时
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-123456***" crossorigin="anonymous"></script>
,会自动显示display广告,具体如何显示是产品在后台设置,我们不用管。 - 当产品需要让我在特定位置或特定时机显示广告时,我们需要插入HTML代码块
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-123456***" data-ad-slot="8*********" data-ad-format="auto" data-full-width-responsive="true"></ins>
,或者使用js插入;然后执行代码插入语句(adsbygoogle = window.adsbygoogle || []).push({});
,即可在特定位置插入广告,但该广告也不是必现,主要靠产品在后台设置。
// AFC广告
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-123456***" crossorigin="anonymous"></script>
// 方法1:广告容器
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-123456***"
data-ad-slot="8*********"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
// 加载广告
<script>
(adsbygoogle = window.adsbygoogle || []).push({
});
</script>
// 方法2:js创建广告容器
insertGgafc() {
// 创建广告容器:给容器设置宽度就相当于给广告设置宽度
const adContainer = document.createElement('div');
adContainer.className = 'middle-ad-container';
adContainer.style.cssText = 'margin: 20px 0; max-width: 520px;';
// 创建广告元素
const adInsElement = document.createElement('ins');
adInsElement.className = 'adsbygoogle';
adInsElement.style.display = 'block';
adInsElement.setAttribute('data-ad-client', 'ca-pub-6468790746781495');
adInsElement.setAttribute('data-ad-slot', '6799291808');
adInsElement.setAttribute('data-ad-format', 'auto');
adInsElement.setAttribute('data-full-width-responsive', 'true');
// 将广告添加到容器
adContainer.appendChild(adInsElement);
// 加载广告
try {
(adsbygoogle = window.adsbygoogle || []).push({
});
} catch (err) {
console.error('Ad load error:', err);
adContainer.remove();
}
}
AFS类型广告
AFS广告主要是指嵌入在搜索引擎上的广告,用户在进行搜索时看到的广告,与搜索关键词息息相关。 AFS广告比较麻烦了
// 以下两段代码都是产品给的,直接粘贴即可
// 搜索引擎
<script async src="https://cse.google.com/cse.js?cx=c123456***"></script>
// 搜索结果容器
<div class="gcse-searchresults-only"></div>
CSE广告
CSE广告(Customer Search Engine)通常指的是客户搜索引擎广告。
// 这里无需额外添加代码,上述搜索引擎结果会自带这个CSE广告
含有赞助商广告
加粗字样的结果则为CSE广告
RS广告
RS广告通常指的是相关搜索广告(Related Search ads),这些广告一般显示在用户进行搜索时的结果页面上,与搜索的关键词相关。
其中pubId
和styleId
需要产品给我们
// RS广告
window.__gcse = {
parsetags: 'onload', // Defaults to 'onload'
initializationCallback: null,
searchCallbacks: {
web: {
rendered: function (gname, query, promoElts, resultElts) {
// 移除已存在的 RS 广告
const existingRsBox = document.getElementById('rs-box-1');
if (existingRsBox) {
existingRsBox.remove();
}
// 只在第一页显示 RS 广告
if (isPageOne()) {
let rsContainer = document.createElement('div');
rsContainer.id = 'rs-box-1';
rsContainer.style.margin = '20px 0';
let posE = resultElts[6];
if (posE && posE.parentNode) {
posE.parentNode.insertBefore(rsContainer, posE);
afsInit();
}
}
},
},
},
};
function afsInit() {
var q = gaEventUtils.getParam('q');
if (q) {
loadSearchAd(q);
}
}
function loadSearchAd(query) {
if (query) {
var searchPage = getSearchUrl(