IMO,如果您想遵循 Quantcast Choice 和 Google Adsense 的官方说明,方法如下:
<head>
在您的标签中添加所有这些:
谷歌的AdSense
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
//pause all ad requests until further notice
(adsbygoogle=window.adsbygoogle||[]).pauseAdRequests=1;
(adsbygoogle=window.adsbygoogle||[]).push({
google_ad_client: "ca-pub-XXXXXXXXXXXXXXXX",
enable_page_level_ads: true
});
</script>
用于 TCF 2.0 的 quantcast 选择 CMT
<script>
(function() {
var host = window.location.hostname;
var element = document.createElement('script');
var firstScript = document.getElementsByTagName('script')[0];
var url = 'https://quantcast.mgr.consensu.org'
.concat('/choice/', YOUR_QUANTCAST_ID_HERE, '/', host, '/choice.js')
var uspTries = 0;
var uspTriesLimit = 3;
element.async = true;
element.type = 'text/javascript';
element.src = url;
firstScript.parentNode.insertBefore(element, firstScript);
function makeStub() {
var TCF_LOCATOR_NAME = '__tcfapiLocator';
var queue = [];
var win = window;
var cmpFrame;
function addFrame() {
var doc = win.document;
var otherCMP = !!(win.frames[TCF_LOCATOR_NAME]);
if(!otherCMP) {
if(doc.body) {
var iframe = doc.createElement('iframe');
iframe.style.cssText = 'display:none';
iframe.name = TCF_LOCATOR_NAME;
doc.body.appendChild(iframe);
} else {
setTimeout(addFrame, 5);
}
}
return !otherCMP;
}
function tcfAPIHandler() {
var gdprApplies;
var args = arguments;
if(!args.length) {
return queue;
} else if(args[0] === 'setGdprApplies') {
if(
args.length > 3 &&
args[2] === 2 &&
typeof args[3] === 'boolean'
) {
gdprApplies = args[3];
if(typeof args[2] === 'function') {
args[2]('set', true);
}
}
} else if(args[0] === 'ping') {
var retr = {
gdprApplies: gdprApplies,
cmpLoaded: false,
cmpStatus: 'stub'
};
if(typeof args[2] === 'function') {
args[2](retr);
}
} else {
queue.push(args);
}
}
function postMessageEventHandler(event) {
var msgIsString = typeof event.data === 'string';
var json = {};
try {
if(msgIsString) {
json = JSON.parse(event.data);
} else {
json = event.data;
}
} catch (ignore) {}
var payload = json.__tcfapiCall;
if(payload) {
window.__tcfapi(
payload.command,
payload.version,
function(retValue, success) {
var returnMsg = {
__tcfapiReturn: {
returnValue: retValue,
success: success,
callId: payload.callId
}
};
if(msgIsString) {
returnMsg = JSON.stringify(returnMsg);
}
event.source.postMessage(returnMsg, '*');
},
payload.parameter
);
}
}
while (win) {
try {
if(win.frames[TCF_LOCATOR_NAME]) {
cmpFrame = win;
break;
}
} catch (ignore) {}
if(win === window.top) {
break;
}
win = win.parent;
}
if(!cmpFrame) {
addFrame();
win.__tcfapi = tcfAPIHandler;
win.addEventListener('message', postMessageEventHandler, false);
}
};
makeStub();
var uspStubFunction = function() {
var arg = arguments;
if(typeof window.__uspapi !== uspStubFunction) {
setTimeout(function() {
if(typeof window.__uspapi !== 'undefined') {
window.__uspapi.apply(window.__uspapi, arg);
}
}, 500);
}
};
var checkIfUspIsReady = function() {
uspTries++;
if(window.__uspapi === uspStubFunction && uspTries < uspTriesLimit) {
console.warn('USP is not accessible');
} else {
clearInterval(uspInterval);
}
};
if(typeof window.__uspapi === 'undefined') {
window.__uspapi = uspStubFunction;
var uspInterval = setInterval(checkIfUspIsReady, 6000);
}
})();
</script>
TCF 2.0 API 监听器
<script>
window.__tcfapi('addEventListener', 2, function(tcData, listenerSuccess) {
if(listenerSuccess) {
//check the eventstatus
if(tcData.eventStatus === 'useractioncomplete' || tcData.eventStatus === 'tcloaded') {
if(!tcData.gdprApplies) {
//GDPR does not apply to this user, load ads immediately
(adsbygoogle=window.adsbygoogle||[]).pauseAdRequests=0;
return;
}
//deal with parsing tcData for IAB Vendor consents
//deal with personalized/non-personalized Google ads
if((tcData.vendor.consents[755] || tcData.vendor.legitimateInterests[755]) && (tcData.purpose.consents[1] && tcData.purpose.consents[3] && tcData.purpose.consents[4] && tcData.purpose.legitimateInterests[2] && tcData.purpose.legitimateInterests[7] && tcData.purpose.legitimateInterests[9] && tcData.purpose.legitimateInterests[10])) {
//consent signals sufficient for personalized ads
//set personalized ads and unpause loading process
(adsbygoogle=window.adsbygoogle||[]).requestNonPersonalizedAds=0;
} else if((tcData.vendor.consents[755] || tcData.vendor.legitimateInterests[755]) && (tcData.purpose.consents[1] && tcData.purpose.legitimateInterests[2] && tcData.purpose.legitimateInterests[7] && tcData.purpose.legitimateInterests[9] && tcData.purpose.legitimateInterests[10])) {
//consent signals NOT sufficient for personalized ads
//set non-personalized ads and unpause loading process
(adsbygoogle=window.adsbygoogle||[]).requestNonPersonalizedAds=1;
}
(adsbygoogle=window.adsbygoogle||[]).pauseAdRequests=0;
}
}
});
</script>