Skip to content

Instantly share code, notes, and snippets.

@zecka
Created November 27, 2024 13:10
Show Gist options
  • Save zecka/fbd3e9674664fb4a54972c8cddd84c9f to your computer and use it in GitHub Desktop.
Save zecka/fbd3e9674664fb4a54972c8cddd84c9f to your computer and use it in GitHub Desktop.
Onetrust SDK Typescript definition
export {};
/**
* In both methods above, the options parameter provides a way to further manipulate page content
* based on whether consent for a group of cookies has been given.
* This provides a simple mechanism that enables website owners to either incentivize users to opt-in or deliver
* a different user experience to visitors who opt-out.
* @see https://my.onetrust.com/articles/en_US/Knowledge/UUID-518074a1-a6da-81c3-be52-bae7685d9c94
*/
type OneTrustInsertScriptOptions = {
/** Delete all parent container content before inserting the element */
deleteSelectorContent?: boolean;
/** Shows parent container element after inserting the element. */
makeSelectorVisible?: boolean;
/** A list of html element id’s to show after inserting the element. */
makeElementsVisible: string[];
/** A list of html element id’s to delete after inserting the element. */
deleteElements: string[];
};
/**
* One Trust Javascript API
* @see https://developer.onetrust.com/onetrust/docs/javascript-api
*/
declare global {
type OneTrustApi = {
/**
* Close Banner
* @description Closes the banner and takes consent as per the consent model.
*/
Close: () => void;
/**
* Allow All Consent
* @description Enable consent for all groups, hosts and vendors.
*/
AllowAll: () => void;
/**
* Reject All Consent
* @description Disable consent for all groups, hosts and vendors except strictly necessary group category.
*/
RejectAll: () => void;
/**
* @description Show Preference Center
*/
ToggleInfoDisplay: () => void;
/**
* Load Banner (Single Page Application)
* @description Loads the banner in case of single page application (CookieSPAEnabled feature pack should be enabled).
*/
LoadBanner: () => void;
/**
* @description Used to insert the script based on user consent.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#insert-script
* @param url HTML tag to be inserted
* @param selector HTML parent element id where the element will be inserted
* @param callback A JavaScript function to be called once the element has been inserted
* @param options A list of behaviors for when the element is inserted
* @param groupId Group ID for which the element will be inserted
* @returns
*/
InsertScript: (
url: string,
selector: string,
callback: () => void | null,
options: OneTrustInsertScriptOptions | null,
groupId: string | null,
) => void;
/**
* @description Used to block google analytics when consent has not given. We can window['ga-disable-${gaID}'] whether this is set to true.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#block-google-analytics
* @example OneTrust.BlockGoogleAnalytics('gaId', 'C0002');
* @param gaId Google Analytics ID
* @param groupId Group category ID
* @returns void
*/
BlockGoogleAnalytics: (gaId: string, groupId: string) => void;
/**
* @description Used to trigger Google Analytics event. We can by inspecting DataLayer event trackOptanonEvent.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#trigger-google-analytics-event
*/
TriggerGoogleAnalyticsEvent: (
category: string,
action: string,
label: string,
value: string,
) => void;
/**
* @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.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#is-optanonalertboxclosed-cookie-set
*/
IsAlertBoxClosed: () => boolean;
/**
* @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.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#is-optanonalertboxclosed-and-valid
*/
IsAlertBoxClosedAndValid: () => boolean;
/**
* @description Used to set OptanonAlertBoxClosed cookie based on isOptanonAlertBoxCookiePersistent.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#set-optanonalertboxclosed-cookie
*/
SetAlertBoxClosed: () => void;
/**
* @description Callback for consent changed event.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#on-consent-changed-callback
*/
OnConsentChanged: (callback: () => void) => void;
/**
* @description Get information about the cookie script - data includes, consent models, cookies in preference centre, etc.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#get-domain-data
*/
GetDomainData: () => object;
/**
* @description View the configured script details and verify implementation using a new Javascript method in the browser console.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#test-log
* @return console log
* - 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
*/
testLog: () => void;
/**
* @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.
* @param lang 2-digit ISO 639 language code. Make sure to configure the language for the Template and published.
*/
changeLanguage: (lang: string) => void;
/**
* @description Fetch the Preference Center JSON and render it to view.
*/
FetchAndDownloadPC: () => void;
/**
* @description Returns the CSS for the CMP.
*/
getCSS: () => string;
/**
* @description Enable or disable consent for a category, host, or general vendor by providing the respective bit value
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#update-consent
* @example OneTrust.UpdateConsent("Category","C0003:1"); //Enables consent for cookie category 3
* @example OneTrust.UpdateConsent("Host","H2:0"); //Disable consent for Host H2
*/
UpdateConsent: (groupIdType: string, bitValue: string) => void;
/**
* @description Set a datasubjectId for current user
*/
setDataSubjectId: (dataSubjectId: string, isAnonymous?: boolean) => void;
/**
* @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 not update the value of OptanonConsent.
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#sync-consent-profile
*/
syncConsentProfile: (dataSubjectId: string, jwtToken: string, isAnonymous: boolean) => void;
/**
* @description Update the consent values of OptanonConsent
* @see https://developer.onetrust.com/onetrust/docs/javascript-api#set-consent-profile
*/
setConsentProfile: (payload: object) => void;
useGeoLocationService: boolean;
/**
* @see https://developer.onetrust.com/onetrust/docs/custom-geolocation-integration
*/
getGeolocationData: () => {
country: string;
state: string;
region?: string;
};
};
interface Window {
/**
* Callback for onetrust init
*/
OptanonWrapper: () => void;
/**
* One Trust Javascript API
* @see https://developer.onetrust.com/onetrust/docs/javascript-api
*/
OneTrust?: OneTrustApi;
OptanonActiveGroups?: string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment