Note: This developer site is moving to developers.google.com/tag-platform and will be redirected by September 30, 2021

Configure consent in Tag Manager

Google Tag Manager includes several features that work together to help you build consent management solutions with Tag Manager templates that can be deployed on the Community Template Gallery. These features include a set of APIs and consent types to give you more control over how tags behave. Learn more about Tag Manager consent settings and configuration.

To configure the default consent settings, use the setDefaultConsentState API. During your user's initial set up, prompt them for what defaults they prefer. The following example shows the setDefaultConsentState call for a user that indicated that ad_storage should be 'denied' by default and analytics_storage, functionality_storage, personalization_storage, and security_storage should be 'granted' by default:

const setDefaultConsentState = require('setDefaultConsentState');

setDefaultConsentState({
  'ad_storage': 'denied',
  'analytics_storage': 'granted',
  'functionality_storage': 'granted',
  'personalization_storage': 'granted',
  'security_storage': 'granted',
  'wait_for_update': 500
});

Update behavior

Once website users have indicated their consent status, typically through interacting with a consent banner, update their consent status accordingly with the updateConsentState api. The following example shows the updateConsentState call for a user that indicated they consent to ad_storage analytics_storage, functionality_storage, personalization_storage, and security_storage:

const updateConsentState = require('updateConsentState');

updateConsentState({
  'ad_storage': 'granted',
  'analytics_storage': 'granted',
  'functionality_storage': 'granted',
  'personalization_storage': 'granted',
  'security_storage': 'granted'
});

Implementation example

For the following implementation example, you must create three fields in the template editor.

The command field

  1. Under the Fields tab, click Add Field
  2. Choose Drop-down menu
  3. Change the name to be command
  4. Click Add menu item, then enter default for Item name and default for the Value
  5. Click Add menu item, then enter update for Item name and update for the Value

The defaultSettings field

  1. Under the Fields tab, click Add Field
  2. Choose Param table
  3. Change the name to be defaultSettings, then expand the field
  4. Update the Display name to be Default settings
  5. Click Add column, choose Text input, change the name to be region and check the Require column values to be unique. Expand the column, and change the display name to be Region (leave blank for all regions)
  6. Click Add column, choose Text input, change the name to be granted. Expand the column and change the display name to be Granted (comma separated)
  7. Click Add column, choose Text input, change the name to be granted. Expand the column and change the display name to be Granted (comma separated)

The updateSettings field

Repeat the steps for the defaultSettings field, but name the field updateSettings instead of defaultSettings.

Once those three fields are created, you can continue on to Code tab and enter the following code:

const setDefaultConsentState = require('setDefaultConsentState');
const updateConsentState = require('updateConsentState');

const splitInput = (input) => {
  return input.split(',')
    .map(entry => entry.trim())
    .filter(entry => entry.length !== 0);
};

const parseCommandData = (settings) => {
  const regions = splitInput(settings.region);
  const granted = splitInput(settings.granted);
  const denied = splitInput(settings.denied);

  const commandData = {};
  if (regions.length > 0) {
    commandData.region = regions;
  }
  granted.forEach(entry => {
    commandData[entry] = 'granted';
  });
  denied.forEach(entry => {
    commandData[entry] = 'denied';
  });
  return commandData;
};

if (data.command === 'default') {
  data.defaultSettings.forEach(settings => {
    const commandData = parseCommandData(settings);
    setDefaultConsentState(commandData);
  });
}
if (data.command === 'update') {
  data.updateSettings.forEach(settings => {
    const commandData = parseCommandData(settings);
    updateConsentState(commandData);
  });
}

data.gtmOnSuccess();

Consent types

consent type
ad_storage required
analytics_storage required
functionality_storage recommended
personalization_storage recommended
security_storage recommended

Region-specific behavior

To change the default tagging behavior for users from certain regions, specify a region in your consent command. By providing this value, you can fine tune defaults based on your users geographic locations. For example, to set analytics_storage to 'denied' for users from Spain and Alaska, and to set analytics_storage to 'granted' for all other users:

const setDefaultConsentState = require('setDefaultConsentState');

setDefaultConsentState({
  'analytics_storage': 'denied',
  'region': ['ES', 'US-AK']
});
setDefaultConsentState({
  'analytics_storage': 'granted'
});

Most specific takes precedence

If two default consent commands occur on the same page with values for a region and subregion, the one with a more specific region will take effect. For example, if you have ad_storage set to 'granted' for the region US and ad_storage set to 'denied' for the region US-CA, a visitor from California will have the more specific US-CA setting take effect. For this example, that would mean a visitor from US-CA would have ad_storage set to 'denied'.

Region ad_storage Behavior
US 'granted' Applies to users in the US that are not in CA
US-CA 'denied' Applies to users US-CA
Unspecified 'granted' Uses the default value of 'granted'. Applies to users that aren't in US or US-CA