Skip to content

Commit b62b101

Browse files
authored
Do not use insights cookie if disabled (#3087)
1 parent b9c929b commit b62b101

File tree

8 files changed

+45
-16
lines changed

8 files changed

+45
-16
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": minor
3+
---
4+
5+
Do not set cookie to identify visitor for insights when disabled.

bun.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"packages/colors": {
3737
"name": "@gitbook/colors",
38-
"version": "0.3.0",
38+
"version": "0.3.1",
3939
"devDependencies": {
4040
"typescript": "^5.5.3",
4141
},
@@ -49,7 +49,7 @@
4949
},
5050
"packages/gitbook": {
5151
"name": "gitbook",
52-
"version": "0.8.2",
52+
"version": "0.9.2",
5353
"dependencies": {
5454
"@gitbook/api": "*",
5555
"@gitbook/cache-do": "workspace:*",
@@ -136,7 +136,7 @@
136136
},
137137
"packages/gitbook-v2": {
138138
"name": "gitbook-v2",
139-
"version": "0.2.2",
139+
"version": "0.2.3",
140140
"dependencies": {
141141
"@gitbook/api": "*",
142142
"@gitbook/cache-tags": "workspace:*",
@@ -180,7 +180,7 @@
180180
},
181181
"packages/openapi-parser": {
182182
"name": "@gitbook/openapi-parser",
183-
"version": "2.1.1",
183+
"version": "2.1.2",
184184
"dependencies": {
185185
"@scalar/openapi-parser": "^0.10.10",
186186
"@scalar/openapi-types": "^0.1.9",
@@ -234,7 +234,7 @@
234234
},
235235
"packages/react-openapi": {
236236
"name": "@gitbook/react-openapi",
237-
"version": "1.1.6",
237+
"version": "1.1.9",
238238
"dependencies": {
239239
"@gitbook/openapi-parser": "workspace:*",
240240
"@scalar/api-client-react": "^1.2.5",
@@ -262,7 +262,7 @@
262262
},
263263
"overrides": {
264264
"@codemirror/state": "6.4.1",
265-
"@gitbook/api": "0.106.0",
265+
"@gitbook/api": "0.108.0",
266266
"react": "18.3.1",
267267
"react-dom": "18.3.1",
268268
},
@@ -623,7 +623,7 @@
623623

624624
"@fortawesome/fontawesome-svg-core": ["@fortawesome/[email protected]", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "6.6.0" } }, "sha512-KHwPkCk6oRT4HADE7smhfsKudt9N/9lm6EJ5BVg0tD1yPA5hht837fB87F8pn15D8JfTqQOjhKTktwmLMiD7Kg=="],
625625

626-
"@gitbook/api": ["@gitbook/api@0.106.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-2qA/w18JwHe2fwR2A45q1pEdCZArxqUloegfNibu0xngDhea+iKTXSBrN94wD6Lwkh7cqBp9MvtxC+YMz3hx1g=="],
626+
"@gitbook/api": ["@gitbook/api@0.108.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-OytauuT3IMf0zl6rBvIacQ7YphzNhpuNAvEVQbysCGo3BhGYD1xv+vXVmtkAGUnFEAclw2fg2VjWPt3sOlQsAg=="],
627627

628628
"@gitbook/cache-do": ["@gitbook/cache-do@workspace:packages/cache-do"],
629629

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@codemirror/state": "6.4.1",
1313
"react": "18.3.1",
1414
"react-dom": "18.3.1",
15-
"@gitbook/api": "0.106.0"
15+
"@gitbook/api": "0.108.0"
1616
},
1717
"private": true,
1818
"scripts": {

packages/gitbook/e2e/util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ export function getCustomizationURL(partial: DeepPartial<SiteCustomizationSettin
276276
internationalization: {
277277
locale: CustomizationLocale.En,
278278
},
279+
insights: {
280+
trackingCookie: true,
281+
},
279282
favicon: {},
280283
header: {
281284
preset: CustomizationHeaderPreset.Default,

packages/gitbook/src/components/Insights/InsightsProvider.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,27 @@ type TrackEventCallback = <EventName extends InsightsEventName>(
6363
const InsightsContext = React.createContext<TrackEventCallback>(() => {});
6464

6565
interface InsightsProviderProps extends InsightsEventContext {
66+
/** If true, the events will be sent to the server. */
6667
enabled: boolean;
68+
69+
/** If true, the visitor cookie tracking will be used */
70+
visitorCookieTrackingEnabled: boolean;
71+
72+
/** The URL of the app. */
6773
appURL: string;
74+
75+
/** The host of the API. */
6876
apiHost: string;
77+
78+
/** The children of the provider. */
6979
children: React.ReactNode;
7080
}
7181

7282
/**
7383
* Wrap the content of the app with the InsightsProvider to track events.
7484
*/
7585
export function InsightsProvider(props: InsightsProviderProps) {
76-
const { enabled, appURL, apiHost, children, ...context } = props;
86+
const { enabled, appURL, apiHost, children, visitorCookieTrackingEnabled, ...context } = props;
7787

7888
const visitorIdRef = React.useRef<string | null>(null);
7989
const eventsRef = React.useRef<{
@@ -140,7 +150,8 @@ export function InsightsProvider(props: InsightsProviderProps) {
140150
});
141151

142152
const flushBatchedEvents = useDebounceCallback(async () => {
143-
const visitorId = visitorIdRef.current ?? (await getVisitorId(appURL));
153+
const visitorId =
154+
visitorIdRef.current ?? (await getVisitorId(appURL, visitorCookieTrackingEnabled));
144155
visitorIdRef.current = visitorId;
145156

146157
flushEventsSync();
@@ -184,15 +195,15 @@ export function InsightsProvider(props: InsightsProviderProps) {
184195
* Get the visitor ID and store it in a ref.
185196
*/
186197
React.useEffect(() => {
187-
getVisitorId(appURL).then((visitorId) => {
198+
getVisitorId(appURL, visitorCookieTrackingEnabled).then((visitorId) => {
188199
visitorIdRef.current = visitorId;
189200
// When the page is unloaded, flush all events, but only if the visitor ID is set
190201
window.addEventListener('beforeunload', flushEventsSync);
191202
});
192203
return () => {
193204
window.removeEventListener('beforeunload', flushEventsSync);
194205
};
195-
}, [flushEventsSync, appURL]);
206+
}, [flushEventsSync, appURL, visitorCookieTrackingEnabled]);
196207

197208
return (
198209
<InsightsContext.Provider value={trackEvent}>

packages/gitbook/src/components/Insights/visitorId.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ let pendingVisitorId: Promise<string> | null = null;
1313
/**
1414
* Return the current visitor identifier.
1515
*/
16-
export async function getVisitorId(appURL: string): Promise<string> {
16+
export async function getVisitorId(
17+
appURL: string,
18+
visitorCookieTrackingEnabled: boolean
19+
): Promise<string> {
1720
if (!visitorId) {
1821
if (!pendingVisitorId) {
19-
pendingVisitorId = fetchVisitorID(appURL).finally(() => {
22+
pendingVisitorId = fetchVisitorID(appURL, visitorCookieTrackingEnabled).finally(() => {
2023
pendingVisitorId = null;
2124
});
2225
}
@@ -30,10 +33,13 @@ export async function getVisitorId(appURL: string): Promise<string> {
3033
/**
3134
* Propose a visitor identifier to the GitBook.com server and get the devideId back.
3235
*/
33-
async function fetchVisitorID(appURL: string): Promise<string> {
36+
async function fetchVisitorID(
37+
appURL: string,
38+
visitorCookieTrackingEnabled: boolean
39+
): Promise<string> {
3440
const withoutCookies = isCookiesTrackingDisabled();
3541

36-
if (withoutCookies) {
42+
if (withoutCookies || !visitorCookieTrackingEnabled) {
3743
return generateRandomId();
3844
}
3945

packages/gitbook/src/components/SpaceLayout/SpaceLayout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export function SpaceLayout(props: {
6262
revisionId={context.revisionId}
6363
spaceId={context.space.id}
6464
visitorAuthClaims={visitorAuthClaims}
65+
visitorCookieTrackingEnabled={context.customization.insights?.trackingCookie}
6566
>
6667
<Announcement context={context} />
6768
<Header withTopHeader={withTopHeader} context={context} />

packages/gitbook/src/lib/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export function defaultCustomization(): api.SiteCustomizationSettings {
2626
internationalization: {
2727
locale: api.CustomizationLocale.En,
2828
},
29+
insights: {
30+
trackingCookie: true,
31+
},
2932
favicon: {},
3033
header: {
3134
preset: api.CustomizationHeaderPreset.Default,

0 commit comments

Comments
 (0)