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

MethodClose()
DescriptionCloses the banner and takes consent as per the consent model.
Parametersn/a
Returnsn/a

Example

OneTrust.Close();

Allow All Consent

MethodAllowAll()
DescriptionEnable consent for all groups, hosts and vendors.
Parametersn/a
Returnsn/a

Example

OneTrust.AllowAll();

Reject All Consent

MethodRejectAll()
DescriptionDisable consent for all groups, hosts and vendors except strictly necessary group category.
Parametersn/a
Returnsn/a

Example

OneTrust.RejectAll();

Show Preference Center

MethodToggleInfoDisplay()
DescriptionDisplays the Preference Center.
Parametersn/a
Returnsn/a

Example

OneTrust.ToggleInfoDisplay();

Load Banner (Single Page Application)

MethodLoadBanner()
DescriptionLoads the banner in case of single page application (CookieSPAEnabled feature pack should be enabled).
Parametersn/a
Returnsn/a

Example

OneTrust.LoadBanner();

Insert Script

MethodInsertScript(url, selector, callback, options, groupId, async)
DescriptionUsed to insert the script based on user consent.
Parametersurl (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

Returnsn/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

MethodInsertHTML(element, selector, callback, options, groupId)
DescriptionUsed to insert the HTML element like iFrame or image tag.
Parameterselement (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.

Returnsn/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

MethodBlockGoogleAnalytics(gaId, groupId)
DescriptionUsed to block google analytics when consent has not given. We can window['ga-disable-${gaID}'] whether this is set to true.
ParametersgaId (string): Google Analytics ID
groupId (string): Group category ID
Returnsn/a

Example

OneTrust.BlockGoogleAnalytics('gaId', 'C0002');

Trigger Google Analytics Event

MethodTriggerGoogleAnalyticsEvent(category, action, label, value)
DescriptionUsed to trigger Google Analytics event. We can by inspecting DataLayer event trackOptanonEvent.
Parameterscategory (string): category
action (string): action
label (string): label
value (string): value
Returnsn/a

Example

OneTrust.TriggerGoogleAnalyticsEvent(category, action, label, value);

Is OptanonAlertBoxClosed Cookie Set

MethodIsAlertBoxClosed()
DescriptionWhen true, OptanonAlertBoxClosed cookie is set. This will result in the Banner not being shown to the user, unless re-consent is flagged on initialization.
Parametersn/a
Returnsboolean

Example

OneTrust.IsAlertBoxClosed();

Is OptanonAlertBoxClosed and Valid

MethodIsAlertBoxClosedAndValid()
DescriptionWhen true, OptanonAlertBoxClosed cookie is set. This will result in the Banner not being shown to the user, unless re-consent is flagged on initialization.
Parametersn/a
Returnsboolean

Example

OneTrust.IsAlertBoxClosedAndValid();

Set OptanonAlertBoxClosed Cookie

MethodSetAlertBoxClosed()
DescriptionUsed to set OptanonAlertBoxClosed cookie based on isOptanonAlertBoxCookiePersistent.
Parametersn/a
Returnsn/a

Example

OneTrust.SetAlertBoxClosed();

On Consent Changed Callback

MethodOnConsentChanged(callback)
DescriptionCallback for consent changed event.
Parameterscallback (function): callback
Returnsn/a

Example

OneTrust.OnConsentChanged(function() {});

Get Domain Data

MethodGetDomainData()
DescriptionGet information about the cookie script - data includes, consent models, cookies in preference centre, etc.
Parametersn/a
Returnsobject

Example

OneTrust.GetDomainData();

Returns

Contact OneTrust

Get Vendor Consent Requests

MethodgetVendorConsentsRequestV2()
DescriptionWill return getInAppTCData to support mobile applications. See more information IAB TCF 2.0 SDK Developer Guide.
Parametersn/a
Returnsobject

Example

OneTrust.getVendorConsentsRequestV2();

Returns

Contact OneTrust

Test Log

MethodtestLog()
DescriptionView the configured script details and verify implementation using a new Javascript method in the browser console.
Parametersn/a
Returnsconsole 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

MethodchangeLanguage(lang)
DescriptionProgrammatically 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.
Parameterslang(string): 2-digit ISO 639 language code. Make sure to configure the language for the Template and published.
Returnsn/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

MethodFetchAndDownloadPC()
DescriptionFetch the Preference Center JSON and render it to view.
Parametersn/a
Returnsn/a

Example

OneTrust.FetchAndDownloadPC();

Get CSS

MethodgetCSS()
DescriptionReturns the CSS for the CMP.
Parametersn/a
Returnsstring

Example

OneTrust.getCSS();

Returns

#onetrust-banner-sdk{ ... }

Update Consent

MethodUpdateConsent(groupIdType,bitValue)
DescriptionEnable or disable consent for a category, host, or general vendor by providing the respective bit value
ParametersgroupIdType(string),bitValue(string)
Returnsn/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

MethodsetDataSubjectId(dataSubjectId,isAnonymous)
DescriptionSet a datasubjectId for current user
ParametersdataSubjectId (string), isAnonymous (boolean = false)
Returnsn/a
OneTrust.setDataSubjectId("someId") //sets Id to "someId", defaults to isAnonymous = false
OneTrust.setDataSubjectId("someId", true) //sets Id to "someId", sets isAnonymous = true

Sync Consent Profile

MethodsyncConsentProfile(dataSubjectId,JWT_Token,isAnonymous)
DescriptionRetrieve 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.
ParametersdataSubjectId (string), JWT_Token (string) isAnonymous (boolean = false)
Returnsn/a

Example

OneTrust.syncConsentProfile("Identifier","JWT_Token",false)

Set Consent Profile

MethodsetConsentProfile(payload)
DescriptionUpdate the consent values of OptanonConsent
Parameterspayload (string) - see example below
Returnsn/a

Example

OneTrust.setConsentProfile({"purposes": 
                            [{"Id":"PURPOSE_ID","TransactionType":"CONFIRMED"},
                             {"Id":"PURPOSE_ID","TransactionType":"NOT_GIVEN"},
                             {"Id":"PURPOSE_ID","TransactionType":"CONFIRMED"}],
                            "identifier":"dataSubjectId"});