Skip to content

Commit

Permalink
[9.x] Rebuild (#80)
Browse files Browse the repository at this point in the history
* Initial work on rebuild

* Apply Code Style changes

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Apply Code Style changes

* wip

* wip

* wip

* Apply Code Style changes

* wip

* wip

* wip

* wip

* wip

* Apply Code Style changes

---------

Co-authored-by: duncanmcclean <duncanmcclean@users.noreply.github.com>
  • Loading branch information
duncanmcclean and duncanmcclean authored May 6, 2024
1 parent 0a3d1b0 commit 9f20970
Show file tree
Hide file tree
Showing 43 changed files with 1,303 additions and 729 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vendor
.DS_Store
.phpunit.result.cache
.phpunit.cache
node_modules
package-lock.json
.php_cs.cache
Expand All @@ -11,4 +11,4 @@ composer.lock
dist
!dist/.gitkeep
vite.hot
.env
.env
12 changes: 8 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

## v9.0.0 (2024-xx-xx)

<!-- ### Read First 👀
Be sure to read the [Upgrade Guide](https://cookie-notice.duncanmcclean.com/upgrade-guides/v8-to-v9) first as manual changes may be necessary. -->
> In version 9, Cookie Notice has been completely overhauled, with a big focus on making it easier to manage third-party scripts and more flexibility for developers around customizing the consent widget.
> To upgrade, please follow the [upgrade guide](https://statamic.com/addons/duncanmcclean/cookie-notice/docs#upgrading-from-v8x-to-v9x).
### What's new

* Statamic 5 support #79 by @duncanmcclean
* A new Control Panel to let clients manage third-party scripts (including Google Tag Manager & Meta Pixel).

### What's changed
### What's improved
* Improved design on the consent widget
* Easier than ever to build custom consent widgets

### What's changed
* Dropped PHP 8.1 support
* Dropped Statamic 4 support
* Everything (seriously, it's a big update)
226 changes: 226 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
## Installation

1. Require Cookie Notice as a Composer dependency:
```
composer require duncanmcclean/cookie-notice
```
2. Publish the addon's configuration file:
```
php artisan vendor:publish --tag=cookie-notice-config
```
3. Add the Cookie Notice tags to your layout(s). Make sure the `{{ cookie_notice:scripts }}` tag is in the `<head>` and the `{{ cookie_notice:widget }}` tag is near the top of the `<body>`.
```antlers
<!doctype html>
<html>
<head>
<!-- Add this... -->
{{ cookie_notice:scripts }}
</head>
<body>
<!-- And this... -->
{{ cookie_notice:widget }}
</body>
</html>
```
4. That's it installed! You can now [configure the consent groups](#configuring-consent-groups) and [customize the widget](#customizing-the-widget).

## Configuring Consent Groups

By default, Cookie Notice ships with two consent groups: Necessary & Analytics. However, you should consider your own use and tweak the consent groups to suit the requirements of your site.

You can customize the consent groups in the `config/cookie-notice.php` config file.

```php
'consent_groups' => [
[
'name' => 'Necessary',
'handle' => 'necessary',
'enable_by_default' => true,
],
[
'name' => 'Analytics',
'handle' => 'analytics',
'description' => 'These cookies are used to provide us with analytics on which content visitors read, etc.',
'enable_by_default' => false,
],
],
```

For each consent group, you can provide the following options:

* `name` - This name will be displayed to the user.
* `handle` - The handle will be used as a unique identifier for this consent group.
* `description` - Optional. The description will be displayed alongside the group's name.
* `enable_by_default` - Optional. Determines whether the consent group will be enabled by default when users first visit your site. They'll still be able to disable the consent group if they wish.

## Managing Scripts

There's a couple of different ways you can manage scripts with the Cookie Notice addon.

### Via the Control Panel

This is likely the easiest way to manage scripts, since you (& your clients) can do it without needing to touch any code. You can access the "Manage Scripts" page in the Control Panel, under the "Utilities" section.

![Screenshot of "Manage Scripts" page in the Control Panel](./docs-cp.png)

From the "Manage Scripts" page, you can configure the scripts to be loaded for each of the configured consent groups. For ease of use, Google Tag Manager & Meta Pixels can be added by simply providing the container ID or pixel ID.

You can also configure the "Revision" of your scripts. This is particularly useful if you want to ensure users provide re-consent when you add new scripts.

##### Permissions

To access the "Manage Scripts" page, users will need the `manage scripts` permission.

### Via code

The recommended way to load JavaScript based on the user's consent is by adding `type="text/plain" data-consent-group="analytics"` to any `<script>` tags:

```html
<script type="text/plain" data-consent-group="analytics">
console.log('Running some analytics code...')
</script>
```

This will ensure that the script is only executed when the user provides consent for the `analytics` consent group.

## Events

If you need to run some specific JavaScript when the user provides or removes consent, you can listen for the `accepted` and `declined` events:

```js
window.CookieNotice.on('accepted', (consentGroup) => {
if (consentGroup === 'analytics') {
console.log('Running some analytics code...')
}
})

window.CookieNotice.on('declined', (consentGroup) => {
if (consentGroup === 'analytics') {
console.log('Un-running some analytics code...')
}
})
```

You may also listen for the `preferences_updated` event, which is dispatched whenever the user updates any of their consent preferences:

```js
window.CookieNotice.on('preferences_updated', (preferences) => {
console.log('The user updated some preference...', preferences);
})
```

> Make sure your JavaScript is **after** the `{{ cookie_notice:scripts }}` tag, otherwise the `window.CookieNotice` object won't be available.
## Consent Widget

Cookie Notice ships with a minimal consent widget, allowing your users to easily accept or decline the configured consent groups.

![Screenshot of the built-in widget](./docs-widget.png)

### Building a custom widget

If you'd prefer to build your own consent widget, to gain more control over the appearance, you can. Follow these steps:

1. Create a new view in your `resources/views` directory. For example, `resources/views/components/cookie-notice.antlers.html`.
2. In your `cookie-notice.php` config file, update the `widget_view` option to point to your new view:
```php
'widget_view' => 'components.cookie-notice',
```
3. Next, copy this boilerplate into your new view:
```antlers
<!-- Start of Cookie Notice Widget -->
<div id="cookie-notice" class="relative z-[999]">
<div class="fixed bottom-6 right-6 bg-white p-6 sm:mx-auto sm:max-w-lg">
<h2>This site uses cookies</h2>
<ul>
{{ consent_groups }}
<li>
<label for="group-{{ handle }}">
<input type="checkbox" id="group-{{ handle }}" name="group-{{ handle }}" class="w-4 h-4">
{{ name }}
{{ if description }}
<span>{{ description }}</span>
{{ /if }}
</label>
</li>
{{ /consent_groups }}
</ul>
<button type="button" data-save-preferences>Save Preferences</button>
</div>
</div>

<script>
document.addEventListener('DOMContentLoaded', () => {
window.CookieNotice.boot(
document.getElementById('cookie-notice'),
JSON.parse('{{ config | to_json }}')
);
});
</script>
<!-- End of Cookie Notice Widget -->
```
* You can loop through each of the consent groups using the `{{ consent_groups }}` tag pair.
* You can boot Cookie Notice's JavaScript by calling the `window.CookieNotice.boot()` method after the DOM has loaded. The first argument should be the root element of your widget, and the second argument should be the config object, which you can pass in using `{{ config | to_json }}`.
* For each consent group, you need have a `group-{{ handle }}` input.
* On the button, you need the `data-save-preferences` attribute so Cookie Notice can listen to `click` events.
4. Start customizing!

### Adding an "Update Preferences" button

After users provide consent when they first visit your site, they may wish to update their preferences at a later late. To make this possible, you should add an "Update Preferences" button somewhere on your site that'll re-open the consent widget.

You can place this button wherever you like - you just need to add the `data-show-cookie-notice-widget` attribute to the element:

```html
<button data-show-cookie-notice-widget>Update cookie consent</button>
```

## Translating

If your site uses a language other than English, you'll probably want to translate the "strings" in the Consent Widget and in the Control Panel. To do this, you'll need to create a JSON translations file:

1. If you don't already have one, create a `lang` directory in the root of your project.
2. Create a `{locale}.json` file in the `lang` directory. Replace `{locale}` with your locale.
3. In the JSON file, you may add key / value pairs for each of the strings you wish to translate:

```json
// lang/de.json

{
"Accept": "Annehmen"
}
```

## Upgrading from v8.x to v9.x

In version 9, Cookie Notice has been completely overhauled, with a big focus on making it easier to manage third-party scripts and more flexibility for developers around customizing the consent widget.

To upgrade to v9.x, you'll need to make some manual changes.

1. Delete the `cookie-notice.php` file from your `config` directory.
2. In your `composer.json` file, update the `duncanmcclean/cookie-notice` version to `^9.0`.
```json
"duncanmcclean/cookie-notice": "^9.0"
```
3. Run `composer update duncanmcclean/cookie-notice --with-dependencies`.
4. Re-publish the addon's configuration file and update it as needed:
```
php artisan vendor:publish --tag=cookie-notice-config
```
5. Make sure you're using **both** the `{{ cookie_notice:scripts }}` and `{{ cookie_notice }}` tags in your site's layout(s):
```antlers
<!doctype html>
<html>
<head>
<!-- This should be in the <head> -->
{{ cookie_notice:scripts }}
</head>
<body>
<!-- And this should be near the top of the <body>. You can also do {{ cookie_notice:widget }} -->
{{ cookie_notice }}
</body>
</html>
```
6. If you were previously calling the `window.cookieNotice.hasConsent('group_name')` method, you should refactor your code to use [event listeners](#events) instead.
7. If you were previously loading Google Tag Manager or a Meta Pixel manually, you may now configure them in [the Control Panel](#via-the-control-panel).
8. If you were previously overriding the `notice` view in `resources/views/vendor/cookie-notice`, you will need to re-implement in a [custom widget](#building-a-custom-widget).
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@

<!-- /statamic:hide -->

Cookie Notice lets you prompt your website visitors for cookie consent. The addon includes a clean Tailwind CSS design (fully-customisable) and support for consent groups.
Cookie Notice provides a simple, lightweight cookie consent widget for your Statamic site. The addon includes a clean Tailwind CSS design (fully-customisable) and support for consent groups.

**Disclaimer:** It's your responsibility to ensure your website complies with local cookie laws.
**Disclaimer:** We've made every effort to ensure this addon is compliant with cookie laws. However, it's your responsibility to ensure your website complies with local cookie laws.

[Read the docs](https://cookie-notice.duncanmcclean.com).
[Read the docs](https://statamic.com/addons/duncanmcclean/cookie-notice/docs).

## Features

### Cookie consent notification

As you'd expect, this addon gives you a lightweight cookie notification. The code for which is fully customisable to meet the design of your site.
### Cookie consent widget
This addon provides a simple, lightweight cookie consent widget. You're able to fully customize the design of the widget to match your site's design.

### Consent Groups
Cookie Notice has built-in support for consent groups. This allows your users to consent to specific types of cookies (eg. Required, Statistics, Marketing).

Cookie Notice has built-in support for consent groups - allowing your users to consent to specific types of cookies (eg. Required, Statistics, Marketing).

### Initialise code with consent
### New: Ability to manage scripts from the Control Panel
It's now possible to manage third-party scripts without leaving the Control Panel. Give your clients the ability to add or remove scripts without needing to ask you, or dive into the code. Supports adding Google Tag Manager, Meta Pixels and inline JavaScript.

You may run certain bits of code only if the user has given their consent.
### New: Events
Cookie Notice allows you to listen for changes in a user's consent, allowing you to run specific JavaScript based on those events.

## Support

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"require-dev": {
"orchestra/testbench": "^8.0 || ^9.0",
"pestphp/pest": "^2.2"
"pestphp/pest": "^2.2",
"pestphp/pest-plugin-laravel": "2.x-dev"
},
"config": {
"optimize-autoloader": true,
Expand Down
42 changes: 27 additions & 15 deletions config/cookie-notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,26 @@
| Cookie
|--------------------------------------------------------------------------
|
| It's ironic, but this addon uses cookies to store if a user has consented
| to cookies or not, and which ones they've consented to. Don't worry
| though, no cookie is stored if the user doesn't consent.
| It's ironic, but this addon uses cookies to determine which consent groups
| a user has consented to. Configure the name & expiry of the cookie here.
|
*/

'cookie_name' => 'COOKIE_NOTICE',
'cookie_name' => 'COOKIE_NOTICE_PREFERENCES',

'cookie_expiry' => 14,

/*
|--------------------------------------------------------------------------
| Consent Widget
|--------------------------------------------------------------------------
|
| Out of the box, this addon provides a simple consent widget. However, you're
| free to create your own widget, just specify the view here.
|
*/

'widget_view' => 'cookie-notice::widget',

/*
|--------------------------------------------------------------------------
Expand All @@ -25,18 +38,17 @@
|
*/

'groups' => [
'Necessary' => [
'required' => true,
'toggle_by_default' => true,
],
'Statistics' => [
'required' => false,
'toggle_by_default' => false,
'consent_groups' => [
[
'name' => 'Necessary',
'handle' => 'necessary',
'enable_by_default' => true,
],
'Marketing' => [
'required' => false,
'toggle_by_default' => false,
[
'name' => 'Analytics',
'handle' => 'analytics',
'description' => 'These cookies are used to provide us with analytics on which content visitors read, etc.',
'enable_by_default' => false,
],
],

Expand Down
Binary file added docs-cp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs-widget.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9f20970

Please sign in to comment.