This site uses third-party cookies, learn more or accept
dark light

Creating a Cookie Notification with Google's AMP

Written by Max Pelic on

The European Union (EU) requires websites to display a "Cookie Policy" notification when a user from the EU visits a website.

Luckily, if your site uses AMP, it's really easy to implement a cookie notification. Even better, you can set up your notification to only be visible to users un the EU.

If you're not familiar with AMP, check out this getting started guide

Basic Implementation

The first thing you'll need to do is include the AMP user notification script:

1<!-- AMP user notification -->

2<script async custom-element="amp-user-notification" src="https://cdn.ampproject.org/v0/amp-user-notification-0.1.js"></script>

Now you'll need to create the notification element. In this example, we'll just make it display the message "This site uses cookies." and a button to hide the message. Depending on how your site uses cookies, you'll need to update the notification.

Here's an example of the notification code:

1<amp-user-notification id="cookie-notif" layout="nodisplay">

2 <div>
3 <!-- text displayed in the notification -->
4 This site uses cookies.
5 <!-- "accept" link -->
6 <a on="tap:cookie-notif.dismiss">accept</a>
7 </div>
8</amp-user-notification>

The notification will only be displayed until the user clicks the "accept" link, it has built-in functionality to keep track of its status in LocalStroage.

Adding Geography Tracking

You can also set up the notification to only display in certain countries. The first step for location-based tracking is to include the AMP geo script:

1<!-- AMP geo -->

2<script async custom-element="amp-geo" src="https://cdn.ampproject.org/v0/amp-geo-0.1.js"></script>

Next, create an amp-geo element with the country codes for the EU (I got a list of codes from this repository):

1<amp-geo layout="nodisplay">

2 <script type="application/json">
3 {
4 "ISOCountryGroups": {
5 "ineu":
6 ["al","ad","at","az","by","be","ba","bg","hr","cy","cz","dk","ee","fi","fr","ge","de","gr","hu","is","ie", "it","kz","xk","lv","li","lt","lu","mk","mt","md","mc","me","nl","no","pl","pt","ro","ru","sm","rs","sk", "si","es","se","ch","tr","ua","gb","va"]
7 }
8 }
9 </script>
10</amp-geo>

Last but not least, we'll need to add a conditional statement on the amp-user-notification to only show the notification in the "ineu" group we created - we can use the data-show-if-geo attribute for that:

1<amp-user-notification id="cookie-notif" layout="nodisplay" data-show-if-geo="ineu">

2 <div>
3 <!-- text displayed in the notification -->
4 This site uses cookies.
5 <!-- "accept" link -->
6 <a on="tap:cookie-notif.dismiss">accept</a>
7 </div>
8</amp-user-notification>

Styling Your Notification

You can style your notification just like any other element. You can use the amp-user-notification selector or add a class or id to the element.

Share this article:

Previous Article: Pros and Cons of website builders

Next Article: 7 ways to drive more visitors to your website