Skip to main content

Programmatic script loading

Load scripts conditionally based on consent. If consent is not yet given, the script is queued and loads automatically when the visitor consents.

// Loads immediately if analytics is consented, otherwise queued
OptSens.loadScript(
'https://www.googletagmanager.com/gtag/js?id=G-XXXXX',
'analytics',
function () {
console.log('GTM loaded');
gtag('config', 'G-XXXXX');
}
);

Parameters

ParameterTypeRequiredDescription
urlstringYesScript URL to load
categorystringYesConsent category required, such as analytics or advertising
callbackfunctionNoCalled when the script finishes loading
OptSens.loadScript(
'https://connect.facebook.net/en_US/fbevents.js',
'advertising',
function () {
fbq('init', 'YOUR_PIXEL_ID');
fbq('track', 'PageView');
}
);

Re-scan the DOM for tagged scripts

After injecting content dynamically (AJAX, SPA route change, CMS preview), call runScripts() to process newly added data-os-category elements:

// After injecting new content via AJAX
document.getElementById('widget-area').innerHTML = newContent;
OptSens.runScripts(); // process any new tagged scripts and iframes