Skip to main content

Configuring the Unified Consent JS SDK using the window object

It is possible to configure the Unified Consent JavaScript SDK by setting the window.UcByOsanoSdkConfig object. Setting these values allows you to call various UnifiedConsentByOsanoSDK methods without needing to explicitly pass a configuration. Any explicitly passed configuration will override the values set to the window.UcByOsanoSdkConfig object.

Setting the object

The window.UcByOsanoSdkConfig object is expected to be of CombinedConfig type.

If providing a custom cookieConfig, make sure to set it BEFORE you load the SDK, like so:

window.UcByOsanoSdkConfig = {
cookieConfig: {
expirationInDays: 10,
refreshOnVisit: false,
defaultActions: [
{
target: 'privacy-protocol-id',
vendor: 'config-id',
action: 'accept'
}
]
}
}
<script type="text/javascript" src="https://s.uc.osano.com/sdk/latest.min.js" crossorigin="anonymous"></script>

All other properties can be set at any point before calling methods of the UnifiedConsentByOsanoSDK module:

window.UcByOsanoSdkConfig = {
/** URL of authorization services provider */
issuer: 'https://uc.api.osano.com/v2/token/create'
/** The config ID to use when generating a token*/
configId: 'config-id'
/** The customer ID to use when generating a token*/
customerId: 'customer-id'
/** Token with authorization to execute call to the Core API */
token: 'existing-token'
/** Osano's Unified Consent CMP API URL */
apiUrl: 'https://uc.api.osano.com/v2'
/** Set a country code for the client instance */
overrideCountryCode: 'US'
/** Set a region code for the client instance */
overrideRegionCode: 'AL'
/** Overrides the default configuration of the cookies*/
cookieConfig: {
expirationInDays: 10,
refreshOnVisit: false,
defaultActions: [
{
target: 'privacy-protocol-id',
vendor: 'config-id',
action: 'accept'
}
]
}
/** Custom attributes*/
attributes: {
custom_attribute: 'custom-attribute-value'
}
/** The target iframe for initializing the embedded experience*/
targetIframe: document.getElementById('osano-uc-iframe')
}

This allows calling UnifiedConsentByOsanoSDK methods without the need to explicitly pass a configuration:

const { UnifiedConsentByOsanoSDK } = window.unifiedConsentJsSdk

/** The custom cookie config will be used without the need to explicitly pass it */
UnifiedConsentByOsanoSDK.getConsentDecision()

/**
* No need to pass a configuration to get the token because the issuer, configId, and customerId
* are present in the UcByOsanoSdkConfig object
*/
const token = UnifiedConsentByOsanoSDK.getToken()

/** If needed, add the token field to the UcByOsanoSdkConfig object */
UcByOsanoSdkConfig.token = token

UnifiedConsentByOsanoSDK.registerPrivacyProtocols().then(() => {
console.log('Privacy protocols have been registered')
})

/** targetIframe and cookieConfig are present in the UcByOsanoSdkConfig object, so this works fine */
const embeddedHandler = UnifiedConsentByOsanoSDK.initEmbeddedParent()

/** We can pass an empty object in place of the explicit config */
const client = UnifiedConsentByOsanoSDK.createClient({}, embeddedHandler)