Set Up Cookie Consent on Your Website


AE Connect can show a cookie banner and a preferences dialog on your own website, remember each visitor's choices, respect Global Privacy Control (GPC), and tell your page code which categories of cookies it may use. This article shows how to turn it on and how to load marketing and analytics scripts only after a visitor agrees.



What you get

  • A cookie banner with Accept All, Decline, Preferences and Close.
  • A preferences dialog with Strictly Necessary (always on), Analytics Cookies and Marketing Cookies.
  • Choices that are saved and still apply after the visitor reloads the page.
  • Support for Global Privacy Control.
  • A consent-change event your code can listen for.
  • Optional Purposes and Vendors tabs for IAB settings.

The wording on the banner comes from the cookie text and data use text you set in your application. See Your Cookie Text Using AE Connect.



Before you start



Step 1: turn consent on

Add the AE Connect framework script from the Install AE Connect tab, and set consent_enabled in AEJSReady:

function AEJSReady(aeJS) {
    window.pageAEJS = aeJS;
    aeJS.settings['consent_enabled'] = true;
}

Visitors who have not chosen yet see the banner. For a visitor who has not chosen, analytics and marketing are off.



Step 2: add a Cookie Settings link

Let visitors change their mind later by calling cookie_preferences(). It opens the preferences dialog with the visitor's saved choices:

<a href="#" id="cookieSettingsLink">Cookie Settings</a>

<script>
document.getElementById('cookieSettingsLink')
    .addEventListener('click', function (event) {
        event.preventDefault();
        if (window.pageAEJS && window.pageAEJS.trigger.cookie_preferences) {
            window.pageAEJS.trigger.cookie_preferences();
        }
    });
</script>

Important

Do not use reset_consent() for a Cookie Settings link. It clears the visitor's saved choices, so the next page view behaves like a first visit. Use it only to reset deliberately, for example while testing. To show the banner again without clearing anything, use cookie_banner().



Step 3: set the categories and GPC behavior

The consent_categories setting says which categories exist and which are switched off when the visitor's browser sends Global Privacy Control. By default Strictly Necessary is required and analytics and marketing are optional and not restricted by GPC. To switch analytics and marketing off whenever GPC is present, set:

aeJS.settings['consent_categories'] = {
    essential: { required: true,  gpcRestricted: false },
    analytics: { required: false, gpcRestricted: true  },
    marketing: { required: false, gpcRestricted: true  }
};

When a restricted category applies, the visitor can still select it, but it stays off. The preferences dialog shows the notice Global Privacy Control detected. Applicable tracking and sharing categories are disabled.



Step 4: check consent before you load anything

Call get.consent() to read the current state:

var state = pageAEJS.get.consent();
// state.gpc                  true when GPC applies
// state.storedConsent        what the visitor saved
// state.effectiveConsent     what you may actually use

Stored consent is what the visitor chose. Effective consent is what is allowed after the required categories and GPC are applied. They can differ: a visitor may accept marketing while GPC is on, so marketing is stored as accepted but is not effective. Always gate your scripts on effective consent. The shortcut is:

if (pageAEJS.consentAllowed('analytics') === true) {
    // load analytics
}
if (pageAEJS.consentAllowed('marketing') === true) {
    // load marketing tags
}

If a tag manager container holds both analytics and marketing tags, load the container when either category is allowed, and keep each tag inside it gated by its own category.



Step 5: react when the visitor changes their choice

AEJSReady runs once. A visitor can accept, decline or withdraw consent afterwards, so listen for changes:

var tagsLoaded = false;

function loadTagsIfAllowed() {
    if (tagsLoaded) { return; }
    if (pageAEJS.consentAllowed('analytics') === true ||
        pageAEJS.consentAllowed('marketing') === true) {
        tagsLoaded = true;
        // load your tag manager container here
    }
}

function AEJSReady(aeJS) {
    window.pageAEJS = aeJS;
    aeJS.settings['consent_enabled'] = true;
    aeJS.events.onConsentChange.addHandler(function (state) {
        loadTagsIfAllowed();
    });
}

The event runs when the page loads and again after each change. It receives the same object as get.consent(). Guard against loading a script twice, as the example does. Withdrawing consent does not unload a script that is already on the page, so reload or stop sending data from that script yourself.



Optional: IAB purposes and vendors

If you use the IAB framework, list the approved purposes and vendors. The preferences dialog then gains Purposes and Vendors tabs:

aeJS.settings['iab_purposes'] = [1, 2, 7];
aeJS.settings['iab_vendors']  = [42, 25, 755];

Only use identifiers that are approved for your site. Simple category consent does not need either setting.



Test your setup

Use a private or incognito window.

  • A fresh visitor sees the banner, and consentAllowed('analytics') and consentAllowed('marketing') are false.
  • Preferences opens the dialog with Analytics and Marketing unchecked.
  • After Save Preferences, reload: the banner stays closed and the choices are kept.
  • Decline turns analytics and marketing off, and they stay off after a reload.
  • Your Cookie Settings link reopens the dialog with the saved choices.
  • With GPC on and the categories restricted, accepting all still leaves analytics and marketing off.
  • Your scripts do not run before consent, and they run once after it.

From the browser console you can inspect the state with pageAEJS.get.consent() and pageAEJS.consentAllowed(...).

For all settings, events and methods, see the AE JS Settings Reference, the AE JS Events Reference and the AE JS Methods & Triggers Reference.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article