Skip to content

Commit aac3f54

Browse files
Check if cookies have been accepted before tracking the page (#915)
* Update fathom script * Update heap * Update hotjar * Update mixpanel * Update plausible * Update posthog * Update REO * Add changeset * Move cookie check out of function call * Move cookies check to function instead of variable
1 parent 04be551 commit aac3f54

File tree

8 files changed

+162
-0
lines changed

8 files changed

+162
-0
lines changed

.changeset/five-birds-post.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@gitbook/integration-plausible': minor
3+
'@gitbook/integration-mixpanel': minor
4+
'@gitbook/integration-posthog': minor
5+
'@gitbook/integration-fathom': minor
6+
'@gitbook/integration-hotjar': minor
7+
'@gitbook/integration-heap': minor
8+
'@gitbook/integration-reo.dev': minor
9+
---
10+
11+
Add check to see if cookies should be disabled or not

integrations/fathom/src/fathomScript.raw.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,30 @@
55
element.setAttribute('data-site', siteId);
66
element.setAttribute('data-spa', 'auto');
77
element.src = 'https://cdn.usefathom.com/script.js';
8+
const GRANTED_COOKIE = '__gitbook_cookie_granted';
9+
10+
function getCookie(cname) {
11+
const name = `${cname}=`;
12+
const decodedCookie = decodeURIComponent(document.cookie);
13+
const ca = decodedCookie.split(';');
14+
for (let i = 0; i < ca.length; i++) {
15+
let c = ca[i];
16+
while (c.charAt(0) === ' ') {
17+
c = c.substring(1);
18+
}
19+
if (c.indexOf(name) === 0) {
20+
return c.substring(name.length, c.length);
21+
}
22+
}
23+
return '';
24+
}
825

926
// Function to track external links
1027
function trackExternalLink(event) {
28+
if (getCookie(GRANTED_COOKIE) !== 'yes') {
29+
return;
30+
}
31+
1132
var item = event.target.closest('a');
1233
if (!item) return;
1334

integrations/heap/src/script.raw.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
(function (win, doc) {
22
const trackingID = '<TO_REPLACE>';
3+
const GRANTED_COOKIE = '__gitbook_cookie_granted';
4+
5+
function getCookie(cname) {
6+
const name = `${cname}=`;
7+
const decodedCookie = decodeURIComponent(document.cookie);
8+
const ca = decodedCookie.split(';');
9+
for (let i = 0; i < ca.length; i++) {
10+
let c = ca[i];
11+
while (c.charAt(0) === ' ') {
12+
c = c.substring(1);
13+
}
14+
if (c.indexOf(name) === 0) {
15+
return c.substring(name.length, c.length);
16+
}
17+
}
18+
return '';
19+
}
20+
21+
if (getCookie(GRANTED_COOKIE) !== 'yes') {
22+
return;
23+
}
24+
325
win.heap = win.heap || [];
426
heap.load = function (e, t) {
527
(win.heap.appid = e), (win.heap.config = t = t || {});

integrations/hotjar/src/script.raw.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
(function (h, o, t, j, a, r) {
22
const trackingID = '<TO_REPLACE>';
3+
const GRANTED_COOKIE = '__gitbook_cookie_granted';
4+
5+
function getCookie(cname) {
6+
const name = `${cname}=`;
7+
const decodedCookie = decodeURIComponent(document.cookie);
8+
const ca = decodedCookie.split(';');
9+
for (let i = 0; i < ca.length; i++) {
10+
let c = ca[i];
11+
while (c.charAt(0) === ' ') {
12+
c = c.substring(1);
13+
}
14+
if (c.indexOf(name) === 0) {
15+
return c.substring(name.length, c.length);
16+
}
17+
}
18+
return '';
19+
}
20+
21+
if (getCookie(GRANTED_COOKIE) !== 'yes') {
22+
return;
23+
}
24+
325
h.hj =
426
h.hj ||
527
function () {

integrations/mixpanel/src/script.raw.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
(function (f, b) {
2+
const GRANTED_COOKIE = '__gitbook_cookie_granted';
3+
4+
function getCookie(cname) {
5+
const name = `${cname}=`;
6+
const decodedCookie = decodeURIComponent(document.cookie);
7+
const ca = decodedCookie.split(';');
8+
for (let i = 0; i < ca.length; i++) {
9+
let c = ca[i];
10+
while (c.charAt(0) === ' ') {
11+
c = c.substring(1);
12+
}
13+
if (c.indexOf(name) === 0) {
14+
return c.substring(name.length, c.length);
15+
}
16+
}
17+
return '';
18+
}
19+
20+
if (getCookie(GRANTED_COOKIE) !== 'yes') {
21+
return;
22+
}
23+
224
if (!b.__SV) {
325
var e, g, i, h;
426
window.mixpanel = b;

integrations/plausible/src/plausibleScript.raw.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,33 @@
66
const apiUrl = '<api>' || 'https://plausible.io/api/event';
77
const domain = '<domain>';
88

9+
const GRANTED_COOKIE = '__gitbook_cookie_granted';
10+
11+
function getCookie(cname) {
12+
const name = `${cname}=`;
13+
const decodedCookie = decodeURIComponent(document.cookie);
14+
const ca = decodedCookie.split(';');
15+
for (let i = 0; i < ca.length; i++) {
16+
let c = ca[i];
17+
while (c.charAt(0) === ' ') {
18+
c = c.substring(1);
19+
}
20+
if (c.indexOf(name) === 0) {
21+
return c.substring(name.length, c.length);
22+
}
23+
}
24+
return '';
25+
}
26+
927
function logIgnoredEvent(event) {
1028
console.warn('Ignoring Event: ' + event);
1129
}
1230

1331
function trackEvent(eventName, options) {
32+
if (getCookie(GRANTED_COOKIE) !== 'yes') {
33+
return;
34+
}
35+
1436
if (
1537
/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(currentUrl.hostname) ||
1638
'file:' === currentUrl.protocol

integrations/posthog/src/script.raw.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
const projectApiKey = '<ph_project_api_key>';
22
const instanceAddress = '<ph_instance_address>';
3+
const GRANTED_COOKIE = '__gitbook_cookie_granted';
4+
5+
function getCookie(cname) {
6+
const name = `${cname}=`;
7+
const decodedCookie = decodeURIComponent(document.cookie);
8+
const ca = decodedCookie.split(';');
9+
for (let i = 0; i < ca.length; i++) {
10+
let c = ca[i];
11+
while (c.charAt(0) === ' ') {
12+
c = c.substring(1);
13+
}
14+
if (c.indexOf(name) === 0) {
15+
return c.substring(name.length, c.length);
16+
}
17+
}
18+
return '';
19+
}
320

421
!(function (t, e) {
22+
if (getCookie(GRANTED_COOKIE) !== 'yes') {
23+
return;
24+
}
25+
526
var o, n, p, r;
627
e.__SV ||
728
((window.posthog = e),

integrations/reo/src/script.raw.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
const trackingID = '<TO_REPLACE>';
2+
const GRANTED_COOKIE = '__gitbook_cookie_granted';
3+
4+
function getCookie(cname) {
5+
const name = `${cname}=`;
6+
const decodedCookie = decodeURIComponent(document.cookie);
7+
const ca = decodedCookie.split(';');
8+
for (let i = 0; i < ca.length; i++) {
9+
let c = ca[i];
10+
while (c.charAt(0) === ' ') {
11+
c = c.substring(1);
12+
}
13+
if (c.indexOf(name) === 0) {
14+
return c.substring(name.length, c.length);
15+
}
16+
}
17+
return '';
18+
}
219

320
(function (r, e, o) {
21+
if (getCookie(GRANTED_COOKIE) !== 'yes') {
22+
return;
23+
}
24+
425
var t, c, n;
526
c = { clientID: trackingID };
627
t = function () {

0 commit comments

Comments
 (0)