GTM Variable based on cookie

By default our banner will send datalayer events when consent has been made or changed. These can be used to connect to your tags to fire whenever there's a change in consent state.

If your website or application sends datalayer events to GTM these might execute before our banner can send its events, this might lead to a loss of data.

In this case we suggest to setup variables in GTM based on our first party cookie.

1. Navigate to your Google Tag Manager account and go to the Variables page.

2. Click "Variables"

Image

3. Click "New"

Image

4. Double-click "Untitled Variable"

Image

5. Type "CF Cookie Performance"

6. Click here.

Image

7. Click "Custom Javascript"

Image

8. Click here.

Image

9. Past the Javascript code. In this example i'm using the performance category.

JavaScript

function() {
  var name = "cookiefirst-consent=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var cookieArray = decodedCookie.split(";");
  for (var i = 0; i < cookieArray.length; i++) {
    var c = cookieArray[i];
    while (c.charAt(0) === " ") {
      c = c.substring(1);
    }
    if (c.indexOf(name) === 0) {
      var value = c.substring(name.length, c.length);
      if(!value) {
        return null;
      }

      try {
        value = JSON.parse(value);
        return !!value['performance'] || null;
      }catch(e) {
        return null;
      }
    }
  }

  return null;
}
Image

10. Change performance for any of the other categories. (advertising, functional, performance) If you are using Service based consent, lookup the service name in your cookiefirst domain settings.

Image

11. Click "Format Value"

Image

12. By default if no consent has been set or it's not accepted it will return null. You can convert this to another value.

Image

13. We convert it to false or denied.

Image

14. Click "Save"

Image

15. Now you can use this variable as the input for a trigger, checking for consent for the specific category or service depending on the setup.

16. If you would like to use this as input for updating the Consent Mode. you should also convert true to granted and false to denied.

Repeat this step for every category or service you'd like to use.