Settings are optional, include the settings you want to change.
{settings:{key: value}}
reload
Boolean[Default: false]
If true the window will reload after the user changes their cookie preference.
required
Boolean[Default: false] (added v1.1.3)
If true the user will not be able to interact with the page until cookie options have been selected, the background will also darken (or lighten depending on theme) and the page will be scroll blocked.
hide_icon
Boolean[Default: false]
If true it hides the icon thats shows when the panel is closed.
(the panel will always stay open if no cookie options have been selected).
cookies_to_exclude
Array[Default: []]
If cookie name is in Array the cookie will be ignored when deleting all cookies. These could be cookies from your application/framework.
keep_all_cookies
Boolean[Default: false]
If true it leaves the cookies as they are and then fires accepted_function or declined_function. The cookies_to_exclude will not be needed if this setting is true, removal of cookies could then be handled by the declined_function for more granular control.
If false deletes all cookies when cookie settings change, then fires accepted_function or declined_function. If your application/framework sets cookies it may be best to set this to true.
first_visit_checked
Boolean[Default: false]
On users first visit (before they select an option).
If true all the checkboxes on the panel will be checked.
If false all the checkboxes on the panel will be unchecked.
start_dropdown_closed
Boolean[Default: false]
If true and if both optional and required cookies are set, the expansion panels will start closed. Useful if you have a large number of cookies.
If false and if both optional and required cookies are set, optional cookies expansion panel will start expanded and required cookies will start closed.
check_switch_icons
Boolean[Default: false] (added v1.2.1)
If true the × and ✓ icons show on the checkbox.
If false they dont.
cookies_duration
Number[Default: 365] (added v1.2.8)
Number of days until Cookiemunch's cookies expire (Cookiemunch only).
cookies_secure
Boolean[Default: false] (added v1.2.8)
Set's the secure flag on Cookiemunch's cookies (Cookiemunch only).
String[Defaults: None, Required, Select, Selected, All, No, Yes, Close]
Text on the buttons.
All keys and values are required for the cookies object, except for group.
These are the optional opt in cookies.
{cookies:[{key: value,...},{...},...]}
id
String
ID (needs to be unique).
group
String(added v1.1.0)
Adds the cookie to a group (Analytics, Marketing, ect...)
This will create another tab like optional and required with the name of the group.
Any cookies in the cookies object that dont have a group will go into the Optional tab.
name
String
Name of the cookie provider on the pop-up (e.g., Google tag manager).
used_for
String
A brief description of what the cookie is used for.
url_text
String
Text of the link to get more information (e.g., Privacy).
url
String
URL to get more information.
accepted_function
String || Function
String:
A string of javascript, example:
accepted_function: "console.log('example'); window['ga-disable-YOUR-ID'] = false; window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'YOUR-ID');"
this can be useful if getting data from an api call.
Function:
Name of the function that will be called if that cookie is accepted. Function needs to be in or above cookiemunch(options) scope. If no function exists will throw an error, see example below.
Be careful if you intend on using this function to add script tags, may get duplication if the user toggles the cookie option on/off. You could use the declined_function to remove them but would probably be best to use settings.reload.
Name of the function that will be called if that cookie is declined. Function needs to be in or above cookiemunch(options) scope. If no function exists will throw an error (create an empty function if not needed), see example below.
All keys and values are required for required_cookies object.
These are the required cookies in order for the site to function.
{required_cookies:[{key: value,...},{...},...]}
name
String
Name of the cookie provider on the pop-up (e.g., Cloudflare).
used_for
String
A brief description of what the cookie is used for.
url_text
String
Text of the link to get more information (e.g., Privacy).
Calling cookiemunch(options) loads the plugin with the options object and fires the accepted_function or declined_function (depending on which the user checked). If block_functions is true the accepted_function or declined_function will not fire.
Use this to initialize the plugin on page load (after the Cookiemunch js file is loaded):
(Don't pass block_functions as true here or cookie functions will not be fired!).
cookiemunch(cookiemunch_options);
To change plugin settings without firing cookie functions:
This will toggle the panel to open and close if cookies have been previously accepted or declined:
cookiemunch_remove_all_cookies()
This will remove all cookies, useful for testing as a user that is coming to your site for the first time. Use with caution, see cookies_to_exclude and keep_all_cookies in the settings object.
<script>
cookiemunch_options = {
settings: {
reload: false,
required: false,
hide_icon: false,
cookies_to_exclude: [],
keep_all_cookies: false,
first_visit_checked: false,
start_dropdown_closed: false,
check_switch_icons: false,
cookies_duration: 365,
cookies_secure: false,
cookie_image: 'https://unpkg.com/@dunks1980/cookiemunch/cookiemunch.svg',
cookie_title: 'Cookies settings',
cookie_optional: 'Optional',
cookie_required: 'Required',
cookie_accept_label: 'Allow Cookies:',
cookie_required_label: 'These Cookies are required in order for the site to function.',
cookie_button_none: 'None',
cookie_button_required: 'Required',
cookie_button_select: 'Select',
cookie_button_selected: 'Selected',
cookie_button_all: 'All',
cookie_button_no: 'No',
cookie_button_yes: 'Yes',
cookie_button_agree: 'Close'
},
cookies: [
{
id: 'Optional Example',
group: '',
name: 'Optional Example',
used_for: 'Description or what this cookie is for.',
url_text: 'Read more',
url: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies',
accepted_function: accepted_function,
declined_function: declined_function
}
],
required_cookies: [
{
name: 'Essential cookies',
used_for: 'these are cookies that are either: used solely to carry out or facilitate the transmission of communications over a network; or. strictly necessary to provide an online service (e.g. our website or a service on our website) which you have requested.',
url_text: 'Read more',
url: 'https://gdprprivacypolicy.org/cookies-policy/'
}
]
};
function accepted_function() {
console.log("accepted_function");
}
function declined_function() {
console.log("declined_function");
}
cookiemunch(cookiemunch_options);
</script>
copy
Example of the setup on this site:
<script>
cookiemunch_options = {
settings: {
first_visit_checked: true,
cookie_image: '/cookiemunch.svg',
cookie_title: 'Cookiemunch cookies',
start_dropdown_closed: true
},
cookies: [
{
id: "gtm",
name: "Google tag manager",
used_for: "To collect analyitical data and monitor performance.",
url_text: "Privacy",
url: "https://policies.google.com/privacy",
accepted_function: cookiemunch_gtm,
declined_function: cookiemunch_gtm_off
},
{
id: "ga",
name: "some analylitics",
used_for: "Some description of what the cookie is used for.",
url_text: "Privacy & Terms",
url: "https://dunks1980.com/",
accepted_function: some_function_name,
declined_function: some_function_name_off
},
{
id: "tracker",
name: "some tracker",
used_for: "Some description of what the cookie is used for.",
url_text: "Privacy & Terms",
url: "https://dunks1980.com/",
accepted_function: some_tracker,
declined_function: some_tracker_off
}
],
required_cookies: [
{
name: "Cloudflare",
used_for: "Cookie is set by Cloudflare to speed up their load times...",
url_text: "Privacy policy",
url: "https://www.cloudflare.com/en-gb/privacypolicy/"
}
]
};
cookiemunch(cookiemunch_options);
function cookiemunch_gtm() {
// Google analylitics function fires! (example)
window['ga-disable-YOUR-ID'] = false;
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'YOUR-ID');
}
function cookiemunch_gtm_off() {
// remove Google tag manage code
window['ga-disable-YOUR-ID'] = true;
}
function some_function_name() {
// some analylitics function fires!
}
function some_function_name_off() {
// remove some analylitics function fires!
}
function some_tracker() {
// some tracker function fires!
}
function some_tracker_off() {
// remove some tracker function fires!
}
</script>
copy
Vue.js example:
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<h1>{{ cookiemunch_message }}</h1>
</template>
<script>
import { cookiemunch } from "@dunks1980/cookiemunch";
import svgimage from "@dunks1980/cookiemunch/cookiemunch.svg";
import "@dunks1980/cookiemunch/cookiemunch.min.css";
export default {
name: "App",
data() {
return {
cookiemunch_options: {
settings: undefined,
cookies: undefined,
required_cookies: undefined,
},
cookiemunch_message: undefined,
};
},
mounted() {
let vm = this;
this.cookiemunch_options.settings = {
first_visit_checked: true,
cookie_title: "Cookiemunch cookies",
cookie_image: svgimage,
start_dropdown_closed: true,
};
this.cookiemunch_options.cookies = [
{
id: "gtm",
name: "Google tag manager",
used_for: "To collect analyitical data and monitor performance.",
url_text: "Privacy",
url: "https://policies.google.com/privacy",
accepted_function: () => {
// example google tag code
window.dataLayer = window.dataLayer || [];
window["ga-disable-YOUR-ID"] = false;
function gtag() {
window.dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "YOUR-ID");
vm.cookiemunch_message =
"Accepted Google tag manager, function fires and 🍪 added!";
},
declined_function: () => {
window["ga-disable-YOUR-ID"] = true;
vm.cookiemunch_message =
"Declined Google tag manager, function fires and 🍪 removed!";
},
},
];
this.cookiemunch_options.required_cookies = [
{
name: "Cloudflare",
used_for: "Cookie is set by Cloudflare to speed up their load times...",
url_text: "Privacy policy",
url: "https://www.cloudflare.com/en-gb/privacypolicy/",
},
];
cookiemunch(this.cookiemunch_options);
},
};
</script>
copy
How do I style it?
If you would like to create a totally custom theme, the SCSS files used to create them can be found on GitHub.
If you only want to change the colours the following CSS is for the buttons and switchs, you can add it to your stylesheet or a style tag after the link tag: