Use client-side JavaScript to call the Adobe Granite Opt-Out Service to verify that you can use a cookie. Use the Granite.OptOutUtil JavaScript object to perform any of the following tasks:

  • Obtain a list of cookie names that indicate that that user does not consent to using cookies for tracking purposes.
  • Obtain a list of cookies that can be used.
  • Determine whether the web browser contains a cookie that indicates the user does not consent to the use of cookies for tracking.
  • Determine whether a specific cookie can be used.

The granite.utils client library folder provides the Granite.OptOutUtil object. Add the following code to your page head JSP to include a link to the JavaScript library:

<ui:includeClientLib categories="granite.utils" />

For example, the following JavaScript function determines whether the COOKIE_NAME cookie is permitted for use before writing to it:

function writeCookie(value){
   if (!Granite.OptOutUtil.maySetCookie("COOKIE_NAME"))
      return;
   if (value) {
      value = encodeURIComponent(value);
      document.cookie = "COOKIE_NAME=" + value;
   }
}

The Granite.OptOutUtil JavaScript Object

Granite.OptOutUtil enables you to determine whether cookie usage is allowed.

getCookieNames() function

The names of the cookies that, when present, indicate that the user has not given consent to the use of cookies.

Parameters

None.

Returns

An array of cookie names.

getWhitelistCookieNames() function

The names of cookies that can be used regardless of the user’s consent.

Parameters

None.

Returns

An array of cookie names.

isOptedOut() function

Determines whether the user’s browser contains any cookies that indicate that consent has not been given to use cookies.

Parameters

None.

Returns

A boolean value of true if a cookie is found that indicates no consent, and a value of false if no cookies indicate non-consent.

maySetCookie(cookieName) function

Determines whether a specific cookie can be used on the user’s browser. This function is equivalent to using the isOptedOut function with determining whether the given cookie is included in the list that the getWhitelistCookieNames function returns.

Parameters

  • cookieName: String. The name of the cookie.

Returns

A boolean value of true if cookieName can be used, or a value of false if cookieName cannot be used.

Experience Manager


Put the Customer at the Center and Build Relationships That Last a Lifetime

Online | Strategy Keynote | General Audience

First impressions last a lifetime. Great first impressions feel personal, connected, and relevant right from the start. From the first...

Wed, Mar 19, 2:30 PM PDT (9:30 PM UTC)

Register

Driving Marketing Agility and Scale: Transforming your Content Supply Chain with AI

Online | Strategy Keynote | General Audience

Marketers everywhere are feeling the pressure to deliver impactful campaigns faster and at greater scale. This Strategy Keynote explores...

Tue, Mar 18, 2:30 PM PDT (9:30 PM UTC)

Register

Connect with Experience League at Summit!

Get front-row access to top sessions, hands-on activities, and networking—wherever you are!

Learn more