Consent Signal Pixel compatibility
If your website uses IMG pixels, you will need to alter your code to pass through proper consent signals. Without either a Javascript pixel or altered IMG pixel, there will be no way for the pixel to understand the consent signal being passed to it by Quantcast Choice.
For web operators currently using Quantcast IMG pixels specifically, below is the code to use to make sure the IMG tag fires properly when consent is granted and to align with GDPR requirements. This accounts for the necessary change of adding the GDPR=0/1 and the consent string to the Quantcast pixel. Note: Remember to customize with any additional arguments you are passing today.
Sample Code
var pcode='p-xxxxx'; // this should be replaced by the quantcast p-code for the publisher or marketer
// set to true if you want the pixel to fire when no CMP is found (or returns error)
var firePixelIfCmpNotFound = false;
if (typeof(window.__tcfapi) !== "function") {
var f = window, cmpFrame;
while(!cmpFrame) {
try {
if (f.frames["__tcfapiLocator"])
cmpFrame = f;
} catch(e) {}
if(f === window.top) break;
f = f.parent;
}
var cmpCallbacks = {}
if (!cmpFrame) console.error("CMP not found");
window.__tcfapi = function(cmd, arg, callback) {
if (!cmpFrame) {
callback({msg:"CMP not found"}, false);
return;
}
var callId = Math.random() + "";
var msg = {__cmpCall: {command: cmd, parameter: arg, callId: callId}};
cmpCallbacks[callId] = callback;
cmpFrame.postMessage(msg, '*');
}
window.addEventListener("message", function(event) {
var dt=event.data;
var json = typeof dt === "string" ? (dt.indexOf("__cmpReturn") != -1 ? JSON.parse(dt) : {}) : dt;
if (json.__cmpReturn) {
var i = json.__cmpReturn;
cmpCallbacks[i.callId](i.returnValue, i.success);
delete cmpCallbacks[i.callId];
}
}, false);
}
window.__tcfapi('getTCData', 2, function(tcData, success) {
if (!firePixelIfCmpNotFound && !success)
return;
if ( !tcData.gdprApplies || (tcData.purpose.consents["1"]
&& tcData.vendor.consents["11"])) {
var img=new Image(1,1);
img.src='https://pixel.quantserve.com/pixel/'+pcode+'.gif?'+'gdpr='+(tcData.gdprApplies?'1&gdpr_consent='+tcData.tcString:0);
img.style='display:none';
function addImg() {
if (document.body)
document.body.appendChild(img);
else
setTimeout(addImg, 5);
}
addImg();
}
});
Base Code Example
<!-- Quantcast Choice. Consent Manager Tag v2.0 (for TCF 2.0) -->
<script type="text/javascript" async=true>
(function() {
var host = window.location.hostname;
var element = document.createElement('script');
var firstScript = document.getElementsByTagName('script')[0];
var milliseconds = new Date().getTime();
var url = 'https://test.cmp.quantcast.com'
.concat('/choice/', 'RtB59_07w9aAV', '/', host, '/choice.js')
.concat('?timestamp=', milliseconds);
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);
}
};
if (typeof module !== 'undefined') {
module.exports = makeStub;
} else {
makeStub();
}
})();
</script>
<!-- End Quantcast Choice. Consent Manager Tag v2.0 (for TCF 2.0) -->