Skip to content

Commit a63fe96

Browse files
cookie consent and ga tracking
1 parent e5d00fe commit a63fe96

File tree

6 files changed

+96
-5
lines changed

6 files changed

+96
-5
lines changed

docs/docusaurus.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ const config: Config = {
6363
} as Preset.Options,
6464
],
6565
],
66-
6766
themeConfig: {
6867
// Replace with your project's social card
6968
image: "img/docusaurus-social-card.jpg",
@@ -111,7 +110,7 @@ const config: Config = {
111110
{
112111
label: "Learn",
113112
to: "/learn/intro",
114-
},
113+
},
115114
{
116115
label: "Code",
117116
to: "/learn/code/intro",

docs/package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"clsx": "^2.0.0",
2222
"prism-react-renderer": "^2.3.0",
2323
"react": "^19.0.0",
24-
"react-dom": "^19.0.0"
24+
"react-dom": "^19.0.0",
25+
"react-cookie-consent": "9.0.0"
2526
},
2627
"devDependencies": {
2728
"@docusaurus/module-type-aliases": "3.7.0",

docs/sidebars.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type {SidebarsConfig} from "@docusaurus/plugin-content-docs";
2-
32
// This runs in Node.js - Don't use client-side code here (browser APIs, JSX...)
43

54
/**

docs/src/components/BitByBitRenderCanvas/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const BitByBitRenderCanvas: React.FC<Props> = React.memo(({
5151
script: scriptProp,
5252
title = "Interactive Script",
5353
description,
54-
iframeUrl: baseUrl = "http://localhost:4202",
54+
iframeUrl: baseUrl = "http://s.bitbybit.dev",
5555
requireManualStart = false,
5656
backgroundImageUrl = "https://bitbybit.dev/assets/landscape.jpeg",
5757
height = "600px",

docs/src/theme/Layout/index.tsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React, { useState, type ReactNode } from "react";
2+
import Layout from "@theme-original/Layout";
3+
import type LayoutType from "@theme/Layout";
4+
import type { WrapperProps } from "@docusaurus/types";
5+
import CookieConsent, { Cookies } from "react-cookie-consent";
6+
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
7+
8+
type Props = WrapperProps<typeof LayoutType>;
9+
const GA_MEASUREMENT_ID = "G-VQHYSMBCKM";
10+
export default function LayoutWrapper(props: Props): ReactNode {
11+
12+
const [trackingInitialized, setTrackingInitialized] = useState(false);
13+
14+
const initializeGATracking = () => {
15+
if (ExecutionEnvironment.canUseDOM && !window.gtag) {
16+
const script = document.createElement("script");
17+
script.src = `https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`;
18+
script.async = true;
19+
document.head.appendChild(script);
20+
21+
script.onload = () => {
22+
const w = window as any;
23+
w.dataLayer = w.dataLayer || [];
24+
function gtag() {
25+
(w.dataLayer as any).push(arguments);
26+
}
27+
w.gtag = gtag;
28+
(gtag as any)("js", new Date());
29+
(gtag as any)("config", GA_MEASUREMENT_ID);
30+
};
31+
}
32+
};
33+
34+
const cookieConsent = Cookies.get("bitbybit-docs-cookie-consent");
35+
36+
if (cookieConsent === "true" && trackingInitialized === false) {
37+
initializeGATracking();
38+
setTrackingInitialized(true);
39+
}
40+
41+
return (
42+
<>
43+
<Layout {...props} />
44+
<CookieConsent
45+
location="bottom"
46+
buttonText="ACCEPT COOKIES ❤️"
47+
declineButtonText="DECLINE"
48+
cookieName="bitbybit-docs-cookie-consent"
49+
style={{ background: "#2B373B" }}
50+
enableDeclineButton
51+
overlay
52+
flipButtons
53+
buttonStyle={{ backgroundColor: "#f0cebb", color: "#1a1c1f", fontWeight: "bold", fontSize: "13px", borderRadius: "5px", padding: "10px 20px" }}
54+
declineButtonStyle={{ backgroundColor: "#1a1c1f", color: "#f0cebb", fontWeight: "bold", fontSize: "13px", borderRadius: "5px", padding: "10px 20px" }}
55+
56+
onAccept={(acceptedByScrolling) => {
57+
if (acceptedByScrolling) {
58+
// triggered if user scrolls past threshold
59+
} else {
60+
initializeGATracking();
61+
}
62+
}}
63+
>
64+
<h2>Help us improve Bitbybit</h2>
65+
<p>
66+
To help us improve Bitbybit and its documentation, we’d like to use Google Analytics, which requires setting cookies. Do you consent to this?
67+
</p>
68+
</CookieConsent>
69+
</>
70+
);
71+
}

0 commit comments

Comments
 (0)