Skip to main content

Running a script only with consent

This page shows a marketing script that the site loads only with the visitor's consent. The script handles two moments: the page load with a consent already saved, and the choice made in the banner.

v2 banner

The v2 banner provides neither getConsent() nor the avacy:* events. See JavaScript events (v2). If you don't know which banner your site uses, read Which banner your site is using.

Who it is for​

For developers of the site who want to control from code when a third-party script starts. To block scripts and iframes without code, see Marking scripts and iframes.

Prerequisites​

Before you start, check that:

  • the banner is installed on the page. See HTML snippet;
  • the marketing script is not already on the page with a normal <script src> tag, which runs anyway;
  • you know the purpose or the vendor your script needs.

Steps​

  1. Copy the script into the page, after the banner snippet. The example uses Google Consent Mode: the marketing script starts with ad_storage, purpose 1 of Google Consent Mode.

    <script>
    (function () {
    function loadMarketing() {
    if (loadMarketing.done) return;
    loadMarketing.done = true;
    var script = document.createElement('script');
    script.src = 'https://example.com/marketing-pixel.js';
    script.async = true;
    document.head.appendChild(script);
    }

    function applyConsent() {
    var consent = window.Avacy && window.Avacy.getConsent();
    if (!consent || !consent.ready || !consent.valid) return;

    // Google Consent Mode, purpose 1 = ad_storage
    if (consent.purposes.gcm && consent.purposes.gcm[1]) loadMarketing();
    }

    // When the page loads
    var initial = window.Avacy && window.Avacy.getConsent();
    if (initial && initial.ready) {
    applyConsent();
    } else {
    document.addEventListener('avacy:ready', applyConsent, { once: true });
    }

    // After every visitor's choice
    document.addEventListener('avacy:consent-saved', applyConsent);
    })();
    </script>
  2. Replace https://example.com/marketing-pixel.js with the address of your script.

  3. If your script requires a different consent, change the condition in applyConsent (see Other conditions).

The script checks the consent at two moments. The check on load is for the returning visitor: they already have a saved consent, the banner does not appear and no choice event fires. The check after the choice is for the new visitor: avacy:consent-saved notifies the script and getConsent() is already updated. The check on initial.ready avoids missing avacy:ready if the banner is ready before the script. The events are in Banner events.

Other conditions​

TCF: the script requires purpose 1 and vendor 52 of the IAB Global Vendor List. They are placeholders. Use the ids from the IAB Europe Global Vendor List. The vendor id is also in the ID column of the dashboard's vendor list.

if (consent.purposes.tcf && consent.purposes.tcf[1] &&
consent.vendors.tcf && consent.vendors.tcf[52]) loadMarketing();

Custom vendor: the id is a string, under the custom framework. d116 is a placeholder: use your vendor's id, in the ID column of the dashboard's vendor list. vendors.custom contains only the vendor consent. Also check the purposes the vendor declares, for example purpose 4 of Google Consent Mode.

if (consent.vendors.custom && consent.vendors.custom['d116'] &&
consent.purposes.gcm && consent.purposes.gcm[4]) loadMarketing();

Check​

  1. Open the page in a private window. The banner appears and, in the browser's Network tab, the marketing script is not requested.
  2. Click Reject all. The script is still not requested.
  3. Clear the site's cookies, reload and click Accept cookies. The script is requested.
  4. Reload the page. With the saved consent the script is requested right away.

Common problems​

The most frequent problems:

  • The script starts even without consent: the page contains another <script src> tag from the same vendor, or a tag manager loads it. The example controls only the script it creates;
  • The script never starts: check the framework in the condition: with the TCF, consent.purposes.tcf exists only if the TCF is configured. Also check the id, by looking for the purpose or the vendor in the output of window.Avacy.getConsent();
  • The visitor revokes consent and the script stays active: a script already loaded stays on the page until reload, and the example does not unload it. After the reload the script does not start.

Last updated: 10/11/2026.