Create a EU Tracking Warning Banner in Wicket

A very good article detailing the EU E-Privacy Directive (aka “cookie law”) and compliance best practices has been posted by Craig Buckler on sitepoint.com. One of the conclusions of the article is the requirement to inform the website visitors about the tracking methods in use.

One possible way to fulfill the "cookie law" is by showing a messsage banner with the required information. The banner should be closeable and not appear again after the website visitor accepts the terms and conditions.

Let's implement it in Wicket:

We start by creating a class EuCookieBanner which will extend Wicket’s Panel. To control the visibility and make sure the banner does not appear after the terms and conditions have been accepted, we will use a cookie.

To check that the cookie is set and hide the banner, we have to overwrite the onConfigure method:


@Override
protected void onConfigure() {
  WebRequest webRequest =
    (WebRequest) RequestCycle.get().getRequest();
  if(webRequest.getCookie(COOKIE_NAME) != null) {
    setVisibilityAllowed(false);
  } 
  super.onConfigure();
}

Where COOKIE_NAME is a static final String holding the name for the cookie.

To hide the banner, the visitor will have to click on a link. The link should not only close the banner, but also set the cookie. The simplest way to achieve this is with an onclick script on the link:


"javascript:document.getElementById('euCookiePanel').style.display='none'; 
 document.cookie='%s=1;expires=%s';return false;"

Our panel contains a div (of course any other element would do it as well) with the id euCookiePanel. The onclick will result in hiding this div with style.display='none'.

The script contains two placeholders (%s) -- the name of the cookie and the expiration date, respectively. The expiration date is a GMT date and the value for the placeholder can be formatted with


SimpleDateFormat("dd MMM yyyy kk:mm:ss z")

Finally, we can replace the placeholders with String.format and put them with AttributeModifier.replace("onclick", …) on the link element.

Resources

Complete implementation with sample information text in English, German and Polish (I take no responsibility for the accuracy and legal conformity of the texts): https://github.com/matrak/wicket-eu-cookie-panel