Banner SDK JavaScript API
The OneTrust Banner SDK exposes an API on the page via JavaScript for interacting with the OneTrust Cookie Compliance platform.
Close Banner
Method | Close() |
Description | Closes the banner and takes consent as per the consent model. |
Parameters | n/a |
Returns | n/a |
Example
OneTrust.Close();
Allow All Consent
Method | AllowAll() |
Description | Enable consent for all groups, hosts and vendors. |
Parameters | n/a |
Returns | n/a |
Example
OneTrust.AllowAll();
Reject All Consent
Method | RejectAll() |
Description | Disable consent for all groups, hosts and vendors except strictly necessary group category. |
Parameters | n/a |
Returns | n/a |
Example
OneTrust.RejectAll();
Show Preference Center
Method | ToggleInfoDisplay() |
Description | Displays the Preference Center. |
Parameters | n/a |
Returns | n/a |
Example
OneTrust.ToggleInfoDisplay();
Load Banner (Single Page Application)
Method | LoadBanner() |
Description | Loads the banner in case of single page application (CookieSPAEnabled feature pack should be enabled). |
Parameters | n/a |
Returns | n/a |
Example
OneTrust.LoadBanner();
Insert Script
Method | InsertScript(url, selector, callback, options, groupId, async) |
Description | Used to insert the script based on user consent. |
Parameters | url (string ): Script tag reference url selector (string ): Container element of script tag. Possible values: "head", "body", "{element_id}" callback (string ): Callback method after script tag has been inserted options (function ): Contains behaviors once the script is inserted. See more below. groupId (string ): Category group id |
Returns | n/a |
Example
Optanon.InsertScript('https://www.w3schools.com/js/myScript1.js','body', null, null, 'C0001', true);
// Used to insert the HTML element like iFrame or image tag.
options
// set to true to delete all selector content before inserting script
options.deleteSelectorContent(boolean)
// set to true to show selector after inserting script
options.makeSelectorVisible(boolean)
// set the id's of arbitrary elements to show after inserting script
options.makeElementsVisible(array[string])
// set the id's of arbitrary elements to delete after inserting script
options.deleteElements(array[string])
Insert HTML
Method | InsertHTML(element, selector, callback, options, groupId) |
Description | Used to insert the HTML element like iFrame or image tag. |
Parameters | element (string ): HTML element string to be inserted selector (string ): HTML parent element id where the element will be inserted. Possible values: "parent_id" callback (string ): Callback method after HTML tag has been inserted options (function ): Contains behaviors once the script is inserted. See more below. groupId (string ): Category group id. |
Returns | n/a |
options
// set to true to delete all selector content before inserting script
options.deleteSelectorContent(boolean)
// set to true to show selector after inserting script
options.makeSelectorVisible(boolean)
// set the id's of arbitrary elements to show after inserting script
options.makeElementsVisible(array[string])
// set the id's of arbitrary elements to delete after inserting script
options.deleteElements(array[string])
Example
OneTrust.InsertHtml('<div>some div</div>', '#container', null, null, 'C0001');
Block Google Analytics
Method | BlockGoogleAnalytics(gaId, groupId) |
Description | Used to block google analytics when consent has not given. We can window['ga-disable-${gaID}'] whether this is set to true. |
Parameters | gaId (string ): Google Analytics ID groupId (string ): Group category ID |
Returns | n/a |
Example
OneTrust.BlockGoogleAnalytics('gaId', 'C0002');
Trigger Google Analytics Event
Method | TriggerGoogleAnalyticsEvent(category, action, label, value) |
Description | Used to trigger Google Analytics event. We can by inspecting DataLayer event trackOptanonEvent. |
Parameters | category (string ): category action (string ): action label (string ): label value (string ): value |
Returns | n/a |
Example
OneTrust.TriggerGoogleAnalyticsEvent(category, action, label, value);
Is OptanonAlertBoxClosed Cookie Set
Method | IsAlertBoxClosed() |
Description | When true , OptanonAlertBoxClosed cookie is set. This will result in the Banner not being shown to the user, unless re-consent is flagged on initialization. |
Parameters | n/a |
Returns | boolean |
Example
OneTrust.IsAlertBoxClosed();
Is OptanonAlertBoxClosed and Valid
Method | IsAlertBoxClosedAndValid() |
Description | When true , OptanonAlertBoxClosed cookie is set. This will result in the Banner not being shown to the user, unless re-consent is flagged on initialization. |
Parameters | n/a |
Returns | boolean |
Example
OneTrust.IsAlertBoxClosedAndValid();
Set OptanonAlertBoxClosed Cookie
Method | SetAlertBoxClosed() |
Description | Used to set OptanonAlertBoxClosed cookie based on isOptanonAlertBoxCookiePersistent. |
Parameters | n/a |
Returns | n/a |
Example
OneTrust.SetAlertBoxClosed();
On Consent Changed Callback
Method | OnConsentChanged(callback) |
Description | Callback for consent changed event. |
Parameters | callback (function ): callback |
Returns | n/a |
Example
OneTrust.OnConsentChanged(function() {});
Get Domain Data
Method | GetDomainData() |
Description | Get information about the cookie script - data includes, consent models, cookies in preference centre, etc. |
Parameters | n/a |
Returns | object |
Example
OneTrust.GetDomainData();
Returns
Contact OneTrust
Get Vendor Consent Requests
Method | getVendorConsentsRequestV2() |
Description | Will return getInAppTCData to support mobile applications. See more information IAB TCF 2.0 SDK Developer Guide. |
Parameters | n/a |
Returns | object |
Example
OneTrust.getVendorConsentsRequestV2();
Returns
Contact OneTrust
Test Log
Method | testLog() |
Description | View the configured script details and verify implementation using a new Javascript method in the browser console. |
Parameters | n/a |
Returns | console log |
Example
OneTrust.testLog();
Returns
- Domain with which the script is associated
- Consent model for the region
- If banner is shown or not shown
- Whether consent has been given (OptanonAlertBoxClosed presence)
- Current Category Statuses
- General Vendor Ids
- Geolocation and Template name
- List of active cookies with associated category IDs, host IDs, and vendor IDs
Change Language
Method | changeLanguage(lang) |
Description | Programmatically change the language that the banner and preference center are presented in on the page. This will throw an exception if the language has not been configured for the displayed template. |
Parameters | lang (string ): 2-digit ISO 639 language code. Make sure to configure the language for the Template and published. |
Returns | n/a |
Example
OneTrust.changeLanguage("en"); // English
OneTrust.changeLanguage("fr"); // French
OneTrust.changeLanguage("de"); // German
OneTrust.changeLanguage("ja"); // Japansese
OneTrust.changeLanguage("pt"); // Portuguese
Fetch and Download Preference Center
Method | FetchAndDownloadPC() |
Description | Fetch the Preference Center JSON and render it to view. |
Parameters | n/a |
Returns | n/a |
Example
OneTrust.FetchAndDownloadPC();
Get CSS
Method | getCSS() |
Description | Returns the CSS for the CMP. |
Parameters | n/a |
Returns | string |
Example
OneTrust.getCSS();
Returns
#onetrust-banner-sdk{ ... }
Update Consent
Method | UpdateConsent(groupIdType,bitValue) |
Description | Enable or disable consent for a category, host, or general vendor by providing the respective bit value |
Parameters | groupIdType (string ),bitValue (string ) |
Returns | n/a |
Example
OneTrust.UpdateConsent("Category","C0003:1"); //Enables consent for cookie category 3
OneTrust.UpdateConsent("Category","C0003:0"); //Disable consent for cookie category 3
OneTrust.UpdateConsent("Category","C0001:1,C0002:1,C0003:0"); //Enable consent for cookie category 1 and 2, Disable consent for cookie category 3
OneTrust.UpdateConsent("Host","H2:1"); //Enable consent for Host H2
OneTrust.UpdateConsent("Host","H2:0"); //Disable consent for Host H2
OneTrust.UpdateConsent("General Vendor","V33:1"); //Enable consent for General Vendor V33
OneTrust.UpdateConsent("General Vendor","V33:0"); //Disable consent for General Vendor V33
Set Data Subject Id
Method | setDataSubjectId(dataSubjectId,isAnonymous, identifierType) |
Description | Set an identifier for the current user. Optionally provide an identifier type or let it default to the default identifier set in Geolocation rules. More information on identifying users and setting default identifiers in Unified profile. |
Parameters | dataSubjectId (string), isAnonymous (boolean = false), identifierType (string = Default Identifier) |
Returns | n/a |
OneTrust.setDataSubjectId("someId") //sets Id to "someId", defaults to isAnonymous = false and identifierType = "Cookie Unique Id" or any default identifier set in geolocation rules
OneTrust.setDataSubjectId("someId", true) //sets Id to "someId", sets isAnonymous to true and defaults to identifierType = "Cookie Unique Id"
OneTrust.setDataSubjectId("[email protected]", false,"Email") // sets Id to "someId", sets isAnonymous to false and sets identifierType to "Email"
Sync Consent Profile
Method | syncConsentProfile(dataSubjectId,JWT_Token,isAnonymous) |
Description | Retrieve consent for an existing user from the OneTrust server - you can see the payload returned in the Network calls via the "preferences" call. This method will update the value of OptanonConsent. |
Parameters | dataSubjectId (string), JWT_Token (string) isAnonymous (boolean = false) |
Returns | n/a |
Example
OneTrust.syncConsentProfile("Identifier","JWT_Token",false)
Set Consent Profile
Method | setConsentProfile(payload) |
Description | Update the consent values of OptanonConsent |
Parameters | payload (string) - see example below |
Returns | n/a |
Example
OneTrust.setConsentProfile({"purposes":
[{"Id":"PURPOSE_ID","TransactionType":"CONFIRMED"},
{"Id":"PURPOSE_ID","TransactionType":"NOT_GIVEN"},
{"Id":"PURPOSE_ID","TransactionType":"CONFIRMED"}],
"identifier":"dataSubjectId"});
Send Consent Receipt
Method | sendReceipt |
Description | Send a receipt immediately. The receipt will be backdated to the last interaction date. This is particularly useful when not using Cross-Domain and Cross-Device Consent but still wanting to log a receipt immediately after identifying a data subject, creating, or updating their profile. |
Parameters | n/a |
Returns | object |
Example
OneTrust.sendReceipt()
For more information, see Unified Profile.
Updated 4 months ago