Skip to content

Instantly share code, notes, and snippets.

@AramZS
Created January 2, 2020 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AramZS/84e0a0174bda784b69be6b4bbf24a51e to your computer and use it in GitHub Desktop.
Save AramZS/84e0a0174bda784b69be6b4bbf24a51e to your computer and use it in GitHub Desktop.
A Basic Testers Kit for USP API CCPA Compliance.
// Instantiate a function immediately to stack trace for finding what vendors are leveraging the function
Function.prototype.clone = function() {
var that = this;
var temp = function temporary() { return that.apply(this, arguments); };
for(var key in this) {
if (this.hasOwnProperty(key)) {
temp[key] = this[key];
}
}
return temp;
};
window.__oldUsapi = window.__uspapi.clone();
window.__uspapi = function(){
var args = Array.from(arguments);
window.__oldUsapi.apply(null, args);
try {
throw new Error('foobar-usp');
} catch (e){
console.log('usp debug');
console.debug(e.stack);
}
};
// Log a postMessage attempt
var ccpaLog = function(event){
if (event.data.__uspapiReturn){
console.log('test CCPA in Ad Event ', 'value: ', event.data.__uspapiReturn , 'event: ', event);
}
}
// Attempt a postMessage (this won't always work)
window.addEventListener(
"message", ccpaLog
);
console.log('CCPA Test postMessaged. Awaiting response. Sent to:', window.origin);
// Send a postMessage. This won't always trigger your debug depending on how the page has set up the uspapi interface.
window.top.postMessage({
__uspapiCall:
{
command: "getUSPData",
parameter: 'foobar',
version: 1,
callId: 'testUniqueId'
}
}, "*");
// Test the on-window object (this should also trigger your debug)
window.__uspapi('getUSPData', 1, function(uspData, success){console.log(uspData, success)});
@AramZS
Copy link
Author

AramZS commented Jan 2, 2020

This reason postMessage may not give you a debug dump is that some folks might implement their event listener by checking an on-page value instead of calling the window.__uspapi

@AramZS
Copy link
Author

AramZS commented Jan 2, 2020

Also, if you get the error Cannot read property 'clone' of undefined it is because the window level IAB function doesn't exist, in which case you might want to ask how your opt-out is registered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment