CMP JavaScript API
The Osano Consent Management Platform (CMP) JavaScript API documentation details its publicly available features.
The JavaScript API makes functions, properties, and events available to developers so that you can easily implement additional functionality based on the visitor's consent status (e.g., load scripts upon visitor consent being granted).
Important Notes
- ⚠️ Utilizing this API to modify or disrupt Osano CMP's intended functionality is strongly discouraged. It may cause your site to not be compliant with privacy laws and regulations; while you have the ability to make such a change, it is not sanctioned by or approved by Osano and proceeding with the change will automatically terminate Section 4.7 of our Terms of Service which is our "No Fines Pledge". It may also cause defective behavior in future revisions.
- ⚠️ The Osano CMP script,
osano.js, must be the very first script in the document. Loading or injecting any other script prior to Osano CMP may result in it being unable to ensure compliance with regional consent laws. The one exception is using the Pre-load interface as necessary. See Installing Osano for more details.
Contents
Pre-load and window.Osano()
You can hook into the JavaScript API before Osano CMP loads by adding this script to the page before osano.js:
(function (w, o, d) {
w[o] =
w[o] ||
function () {
w[o][d].push(arguments);
};
w[o][d] = w[o][d] || [];
})(window, 'Osano', 'data');
It creates a window.Osano() function that you can use to push arguments to the JavaScript API. You can add event listeners and set properties. For example:
<html>
<head>
<script>
(function (w, o, d) {
w[o] =
w[o] ||
function () {
w[o][d].push(arguments);
};
w[o][d] = w[o][d] || [];
})(window, 'Osano', 'data');
window.Osano('onInitialized', consent => console.log('Osano.js initialized', consent));
window.Osano('onCookieBlocked', cookie =>
console.debug('Osano.js blocked cookie:', cookie)
);
window.Osano('userData', 'example user data');
</script>
<script src="https://cmp.osano.com/[...]/osano.js"></script>
...
</head>
</html>
window.Osano() can continue to be used after Osano CMP loads.
Adding Event Listeners
To add an event listener with the window.Osano() function, call it with two arguments: the event name and the callback. The same events are available to both the pre-load function as the JavaScript API but their names are in "onEventName" format:
- onAnalytics
- onCookieBlocked
- onConsentChanged
- onConsentNew
- onConsentSaved
- onIframeBlocked
- onInitialized
- onLocaleUpdated
- onMarketing
- onOptOut
- onPersonalization
- onScriptBlocked
- onStorage
- onUiChanged
Usage:
// window.Osano('onEventName', callback);
// example:
window.Osano('onUiChanged', (component, stateChange) => {
if (component === 'drawer' && stateChange === 'show') {
console.log('Preferences drawer has been opened.');
}
});
Setting Properties
Adding property values with the window.Osano() function takes two arguments, the property name and the value. Only "setter" properties on the JavaScript API are supported:
Usage:
// window.Osano('propertyName', value);
// example:
window.Osano('userData', 'some user data');
JavaScript API Reference
The Osano CMP JavaScript API is available after initialization on the browser's window object: window.Osano.cm. It is available immediately after the Osano CMP script (osano.js) in the document. You do not need to wait for Osano.cm to become available.
Use of deprecated features is not recommended as they may be removed in a future revision.
Functions
Each of these functions are accessible via window.Osano.cm. For example, window.osano.cm.showDrawer().
- addEventListener()
- deprecated emit()
- getConsent()
- hideDialog()
- hideDoNotSell()
- hideDrawer()
- hideWidget()
- off()
- on()
- ready()
- render()
- removeEventListener()
- showDialog()
- showDoNotSell()
- showDrawer()
- showWidget()