tl;dr: Rik learns how to handle cookie consents in a legal and (hopefully) more fun way
This is the fifth in a series of blog posts detailing my journey to rebuild my poetry website - The RikVerse - using Svelte.js, Page.js and Tailwind CSS. Other posts in this series:
- Introduction to the project
- Setting up the shiny
- The client is not the server - 404 errors
- Building the blog reader
- Fun with Cookies!
- The book store bits
- Sharing is Caring - social media share buttons
- … And finally: the poems!
The code developed in this blog post can be found in this branch on GitHub.
Where were we?
In the previous post I added a blog reader to the site, with a page for listing posts and another page to display each post.
Here’s a quick reminder of my fourteen(!) goals for this site rebuild:
- [This post] Keep the donate button; make it More Fun To Use
- [This post] Add in cookie consent (meeting UK ICO guidance for explicit opt-in)
- [Next post] Make each publication page that book’s keystone page
- [Next post] Let people read the books in-site
- [NEW] Add social media share buttons to each page
- [Future post] Index of all the poems available to read
- [Future post] Tag filtering functionality on the poems index page
- [Future post] Easy for the user to access a poem’s associated media files (images, audio, video)
- [Future post] The landing page should display a randomly selected poem
[Done] Simpler, more minimal design; better fonts[Done] Navigation needs to be massively simplified[Done] Avoid 404 Page Not Found errors at all costs![Done] Add in poetry-and-writing-related blog posts[Done] Get rid of the database!
Caring about cookie consents
You know every time you visit a new website and it throws up a “Cookie Consent” banner thing and you have to click the “Accept” button to make it go away - and then you never see it again?
The people who run sites that use generic cookie consent banners are probably breaking the law (at least in the UK). Which isn’t a problem for us users of those sites. But it could well be a problem for those site owners. Because if they break the law, they could get fined. Break the law badly and the fines could be huge!
My take on the UK’s Information Commissioner’s Office’s updated guidance is:
- Users, when visiting a website, need to explicitly agree to have 3rd Party cookies placed on their device before the cookies are activated
- The use of of the cookies by each of the 3rd Parties has to be clearly explained
- We cannot pre-judge the user’s decisions - no more pop-up banners with huge “I ACCEPT” buttons!
- The user should be able to change their choice about cookies at any time, in an accessible and simple manner
What does this all mean for the RikVerse website?
Just because my website is small, it doesn’t mean that I can ignore these rules. The RikVerse runs best when users allow some 3rd party cookies to be installed on their devices. But I can’t just add them when visitors first arrive at the site, then hope nobody notices … or cares!
Why not? Because this site caters to Poets and Poetasters. These people really do care about stuff like privacy and security. And they know how to organise an online lynch mob!
… Never trust someone who shows an interest in poetry. They’re bad people. Bad!
On the positive side of things, these new cookie requirements give me an opportunity to experiment and learn. If I can make cookie consents on the RikVerse site “Fun”, then everyone wins, yes?
There’s three particular pieces of functionality that I want to run on the new site - each of which requires 3rd Party cookies to work properly:
- The donation button works with the help of PayPal, thus requires PayPal cookies;
- Sharing a page to Facebook needs a Facebook share button which only works with their various cookies;
- Similarly, the Twitter tweet button comes with its own host of Twitter cookies;
… That’s a lot of cookies for a little website!
I’ve also decided to add one further constraint: no popup cookies banner!
The visitor to the RikVerse site will not encounter anything cookie-related until they click on a button which would normally trigger a social media share, video watching event, or donation opportunity. Only at that point will the user be redirected to the Cookie Consents page to make their choices known.
Reading poems, or downloading books, requires no cookies whatsoever. Except the sort you dunk in your beverage of choice, of course!
Anyways. For the proof-of-concept development, I decided to first tackle my need to add a working PayPal donate button to the site.
Svelte store functionality
In order to know, and remember, user choices when it comes to cookie consents, we need to give the RikVerse website some state!
Svelte uses objects it calls stores for keeping track of state across a site. They are quite easy to use, and very responsive when they get to work. Because the RikVerse site will need to keep track of user cookie consents in a number of different components, it made sense for me to hold the state of consent decisions in them.
I created a new file - ./src/handleCookies.js
- for building and sharing the stores. Here’s the code that implements the PayPal store:
1 | import { writable } from 'svelte/store'; |
The Cookies page is where the user will be making decisions about consents. Here’s the new code for ./src/pages/CookieConsents.svelte
1 | <style> |
Note that after we import { paypalCookies }
we can directly access the store’s value by using a $ prefix in front of it: #if $paypalCookies === 'no'
, etc.
The Footer bar is also going to be affected by the PayPal cookie consent. Specifically, we will amend the begging button’s action dependant on whether they allow cookies, or refuse them, orr haven’t yet made a decision. This is the updated code for ./src/component/Footer.svelte
:
1 | <style> |
Testing the code shows that it is working - both the Cookies page and the Footer component react appropriately to user choices about PayPal cookies.
This is the view on first load:
… If only it was all this easy!
Sadly, it’s not that easy!
As a test, I clicked the reload button on my browser … and saw this:
The problem is, we have no longer-term memory of user decisions.
Luckily all modern browsers come equipped with functionality which makes this an easy thing for us to remedy:
window.sessionStorage
- for single visits to a site; andwindow.localStorage
- for remembering stuff on a long-term basis, between visits to a site.
I added code to ./src/handleCookies
which allowed me to do exactly this:
1 | // localStorage and sessionStorage functionality |
Now all I had to do was get the Svelte store to update the browser stores at the same time as it updated itself. Svelte allows us to do this through functionality which it calls custom stores.
I added some more code to ./src/handleCookies
to get this working:
1 | // Svelte store functionality |
… and updated my button handler functions in ./src/pages/CookieConsents.svelte
:
1 | <script> |
… And, with fingers crossed, tested a browser reload:
(Happy dance!)
Accepting and/or rejecting all cookies at once
Cookie banners have been criticised because they spring up as soon as a new visitor arrives on a site, with a big “I AGREE” button and a tiny “I want to choose” link which leads to almost everybody clicking on the “I AGREE” button to make the banner go away, never to be seen again.
While the RikVerse website won’t be working like that - as I said earlier, users only get directed to the Cookies page when they try to access some functionality that needs cookie consent before it works - it would be nice if the users could have big “I AGREE” (and equally big “NO COOKIES”) buttons on the Cookies page.
I went away and coded up this functionality. I also added in the buttons and cookie handlers for Twitter and Facebook. I won’t bore you with the code (it’s in GitHub if you want to view it), but here’s a screen grab of the end result:
Making PayPal work (when the user says: yes!)
This post has already gone on too long - and I haven’t generated any donations yet!
That’s because we have one final task to complete - adding PayPal functionality to the Footer component so that when people click on the button they get directed to PayPal to make their donation.
Making a PayPal donate button is a blog post in itself. Luckily those nice people over at Hands On Fundraising have already written it, so I don’t need to bother.
The upshot of it all is that we have to visit the PayPal site and create a donate button. At the end of the process the site spits out some html code which we copy-and-paste into our code.
PayPal will only try to add RikVerse-related cookies to the user’s browser or device when that code - which is actually a html form element - renders. This makes it really easy to control … with the help of Svelte, of course! Only if the user has actively opted in to PayPal cookies will the PayPal form be included in the rendered page.
Here’s the final code for the ./src/components/Footer.svelte
component:
1 | <style> |
Okay … I’ve had too much fun with cookies today, so for my next post I’ll implement the functionality needed to list my books, and to read my books without leaving the RikVerse.
And in the post after that I shall return to cookies - specifically to add social media sharing buttons to every page on the site.