Configure The Script
Open cookieconsent.js and review the settings near the top of the file. At minimum, replace the default Privacy Policy URL with the correct URL for your website.
privacyPolicyUrl: "/privacy-policy/",
cookieExpiryDays: 30,
position: "bottom-left",
europeOnly: false,
notifyOnce: false,
showRejectButton: true,
showPreferencesButton: true,
reloadAfterPreferenceChange: true,
The main settings control:
- Privacy Policy URL
- Saved choice duration
- Notice screen position
- European visitor targeting
- First-page-only behaviour
- Essential Only button visibility
- Cookie preferences icon visibility
- Preference-change page reloads
- Notice wording and button labels
- Cookie details table content
The default configuration displays the notice to all visitors and continues displaying it until they select Accept All Cookies, select Essential Only or close it. The cookie preferences icon allows the visitor to reopen the controls later.
Block Optional Scripts
Essential website scripts and cookies should continue loading normally. To prevent an optional JavaScript file from loading before consent, change its type to text/plain, add data-cookieconsent="optional" and move an external script URL from src to data-src.
<script type="text/plain" data-cookieconsent="optional" data-src="/js/optional-script.js"></script>
<script type="text/plain" data-cookieconsent="optional">
// Optional inline JavaScript
</script>
The script automatically activates these blocks when the visitor selects Accept All Cookies. They remain inactive when the visitor selects Essential Only.
Google Analytics example
Replace G-XXXXXXXXXX with your Google Analytics measurement ID:
<!-- Google tag (gtag.js) -->
<script type="text/plain" data-cookieconsent="optional" data-src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX" async></script>
<script type="text/plain" data-cookieconsent="optional">
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
Optional PHP cookies
PHP session, login, security and shopping basket cookies can normally remain essential. For an optional PHP cookie or server-side feature, check the saved consent choice before loading it:
<?php
$optional_cookies_allowed =
isset($_COOKIE['cookieconsent_choice']) &&
$_COOKIE['cookieconsent_choice'] === 'accepted';
if ($optional_cookies_allowed) {
// Set optional PHP cookies or load optional server-side features here.
}
?>
The updated cookie choice is available to PHP on the next page request. The default preference-change reload setting helps apply a changed choice immediately.
Test The Installation
Open the website in a private browsing window or clear the existing cookie choice before testing.
You can also reset the script from the browser developer console:
CookieConsent.reset();
Test both available choices:
- Select Essential Only and confirm that optional scripts such as Google Analytics do not load.
- Open Cookie Preferences, enable optional cookies and confirm that the optional scripts load.
- Refresh the page and confirm that the saved choice is remembered.
- Check the notice and cookie preferences panel on desktop and mobile screen sizes.
When replacing the CSS or JavaScript file in the future, update the version number in the file URL or clear your website cache so visitors receive the latest copy.
Request Support
We hope you found this documentation useful. If you run into any issues we will be happy to assist you.