Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sour-rabbits-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/integration-cookiebot': major
---

Add cookiebot integration
18 changes: 16 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions integrations/cookiebot/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @gitbook/integration-cookiebot

## 0.1.0

### Minor Changes

- Initial Cookiebot cookie consent integration
Binary file added integrations/cookiebot/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions integrations/cookiebot/gitbook-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: cookiebot
title: Cookiebot
description: Add the Cookiebot cookie consent banner to your published GitBook site.
visibility: public
organization: gitbook
icon: ./assets/icon.png
script: ./src/index.ts
# The following scope(s) are available only to GitBook Staff
# See https://developer.gitbook.com/integrations/configurations#scopes
scopes:
- site:script:inject
- site:script:cookies
contentSecurityPolicy:
script-src: |
https://consent.cookiebot.com;
summary: |
# Overview
This integration allows you to add the Cookiebot cookie consent banner on your published GitBook site.

# How it works
The integration injects the Cookiebot CMP script (`uc.js`) on your page, using your Domain Group ID,
so that the cookie consent banner is displayed and consent is managed according to your Cookiebot configuration.

# Configure
Install the integration on the GitBook site of your choice.
Provide your Cookiebot Domain Group ID (the `data-cbid` value from your installation snippet, found under "Your scripts" in your Cookiebot account).
Choose auto or manual blocking mode to match how you configured Cookiebot on your site.
categories:
- analytics
configurations:
site:
properties:
implementation_id:
type: string
title: Domain Group ID (CBID)
description: Cookiebot domain group ID (data-cbid). Find it under Your scripts in your Cookiebot account.
blocking_mode:
type: string
title: Blocking mode
description: Auto sets data-blockingmode=auto. Manual omits it; tag scripts with data-cookieconsent.
default: manual
enum:
- auto
- manual
required:
- implementation_id
- blocking_mode
target: site
10 changes: 10 additions & 0 deletions integrations/cookiebot/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module '*.raw.js' {
const content: string;
export default content;
}

export declare global {
interface Window {
GitBook: import('@gitbook/browser-types').GitBookGlobal;
}
}
20 changes: 20 additions & 0 deletions integrations/cookiebot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@gitbook/integration-cookiebot",
"version": "0.1.0",
"private": true,
"dependencies": {
"@gitbook/api": "*",
"@gitbook/runtime": "*"
},
"devDependencies": {
"@gitbook/cli": "workspace:*",
"@gitbook/tsconfig": "workspace:*",
"@gitbook/browser-types": "^0.1.5"
},
"scripts": {
"typecheck": "tsc --noEmit",
"publish-integrations-staging": "gitbook publish .",
"check": "gitbook check",
"publish-integrations": "gitbook publish ."
}
}
48 changes: 48 additions & 0 deletions integrations/cookiebot/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
createIntegration,
FetchPublishScriptEventCallback,
RuntimeContext,
RuntimeEnvironment,
} from '@gitbook/runtime';

import script from './script.raw.js';

type CookiebotRuntimeContext = RuntimeContext<
RuntimeEnvironment<
{},
{
implementation_id?: string;
blocking_mode?: 'auto' | 'manual';
}
>
>;

export const handleFetchEvent: FetchPublishScriptEventCallback = async (
event,
{ environment }: CookiebotRuntimeContext,
) => {
const implementationId = environment.siteInstallation?.configuration?.implementation_id;
const blockingMode = environment.siteInstallation?.configuration?.blocking_mode;

if (!implementationId) {
return;
}

const blockingModeScript =
blockingMode === 'auto' ? "s.setAttribute('data-blockingmode', 'auto');" : '';

const scriptContent = (script as string)
.replace('<CBID>', implementationId)
.replace('__BLOCKING_MODE__', blockingModeScript);

return new Response(scriptContent, {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'max-age=604800',
},
});
};

export default createIntegration<CookiebotRuntimeContext>({
fetch_published_script: handleFetchEvent,
});
75 changes: 75 additions & 0 deletions integrations/cookiebot/src/script.raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
(function () {
var w = window;
var d = document;

function injectCookiebot() {
const existing = d.getElementById('Cookiebot');
if (existing) return;

var s = d.createElement('script');
s.id = 'Cookiebot';
s.type = 'text/javascript';
s.src = 'https://consent.cookiebot.com/uc.js';
s.setAttribute('data-cbid', '<CBID>');
__BLOCKING_MODE__;
d.head.appendChild(s);
}

function l() {
if (!w.GitBook || typeof w.GitBook.registerCookieBanner !== 'function') return;

const cookieState = w.GitBook.isCookiesTrackingDisabled();
const hasRecordedConsent = cookieState !== undefined;

w.GitBook.registerCookieBanner(function ({ onApprove, onReject }) {
injectCookiebot();

function emitConsent() {
try {
var c = w.Cookiebot;
if (!c || !c.consent) {
onReject();
return;
}
if (c.declined) {
onReject();
return;
}
var hasNonEssential =
c.consent.preferences || c.consent.statistics || c.consent.marketing;
if (hasNonEssential) {
onApprove();
} else {
onReject();
}
} catch (e) {
onReject();
}
}

w.addEventListener('CookiebotOnAccept', function () {
if (!hasRecordedConsent) {
emitConsent();
}
});

w.addEventListener('CookiebotOnDecline', function () {
if (!hasRecordedConsent) {
onReject();
}
});

w.addEventListener('CookiebotOnConsentReady', function () {
if (!hasRecordedConsent && w.Cookiebot && w.Cookiebot.hasResponse) {
emitConsent();
}
});
});
}

if (w.attachEvent) {
w.attachEvent('onload', l);
} else {
w.addEventListener('load', l, false);
}
})();
3 changes: 3 additions & 0 deletions integrations/cookiebot/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@gitbook/tsconfig/integration.json"
}
Loading