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
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string | Yes | Script URL to load |
category | string | Yes | Consent category required, such as analytics or advertising |
callback | function | No | Called when the script finishes loading |
Example: load Facebook Pixel only after consent
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