Skip to content

Commit e6eb850

Browse files
committed
Get bwid client side
1 parent dfcaab8 commit e6eb850

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

dotcom-rendering/src/components/Metrics.importable.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import {
99
bypassCoreWebVitalsSampling,
1010
initCoreWebVitals,
1111
} from '@guardian/core-web-vitals';
12-
import { getCookie, isString, isUndefined } from '@guardian/libs';
12+
import { isUndefined } from '@guardian/libs';
1313
import { useCallback, useEffect, useState } from 'react';
14+
import { useBrowserId } from '../lib/metrics';
1415
import { useAB, useBetaAB } from '../lib/useAB';
1516
import { useAdBlockInUse } from '../lib/useAdBlockInUse';
1617
import { useDetectAdBlock } from '../lib/useDetectAdBlock';
@@ -41,20 +42,6 @@ const shouldCollectMetricsForBetaTests = (userTestParticipations: string[]) => {
4142
);
4243
};
4344

44-
const useBrowserId = () => {
45-
const [browserId, setBrowserId] = useState<string>();
46-
47-
useEffect(() => {
48-
const cookie = getCookie({ name: 'bwid', shouldMemoize: true });
49-
50-
const id = isString(cookie) ? cookie : 'no-browser-id-available';
51-
52-
setBrowserId(id);
53-
}, []);
54-
55-
return browserId;
56-
};
57-
5845
const useDev = () => {
5946
const [isDev, setIsDev] = useState<boolean>();
6047

dotcom-rendering/src/components/SecureSignup.importable.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { useAuthStatus, useIsSignedIn } from '../lib/useAuthStatus';
2626
import { palette } from '../palette';
2727
import type { RenderingTarget } from '../types/renderingTarget';
2828
import { useConfig } from './ConfigContext';
29+
import { useBrowserId } from '../lib/metrics';
2930

3031
// The Google documentation specifies that if the 'recaptcha-badge' is hidden,
3132
// their T+C's must be displayed instead. While this component hides the
@@ -136,6 +137,7 @@ const buildFormData = (
136137
newsletterId: string,
137138
token: string,
138139
marketingOptIn?: boolean,
140+
browserId?: string,
139141
): FormData => {
140142
const pageRef = window.location.origin + window.location.pathname;
141143
const refViewId = window.guardian.ophan?.pageViewId ?? '';
@@ -155,6 +157,10 @@ const buildFormData = (
155157
formData.append('marketing', marketingOptIn ? 'true' : 'false');
156158
}
157159

160+
if (browserId !== undefined) {
161+
formData.append('browserId', browserId);
162+
}
163+
158164
return formData;
159165
};
160166

@@ -304,6 +310,7 @@ export const SecureSignup = ({
304310
});
305311
}, []);
306312
const { renderingTarget } = useConfig();
313+
const browserId = useBrowserId();
307314

308315
const hasResponse = typeof responseOk === 'boolean';
309316

@@ -319,6 +326,7 @@ export const SecureSignup = ({
319326
newsletterId,
320327
token,
321328
marketingOptIn,
329+
browserId,
322330
);
323331

324332
const response = await postFormData(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getCookie, isString } from '@guardian/libs';
2+
import { useEffect, useState } from 'react';
3+
4+
export const useBrowserId = (): string | undefined => {
5+
const [browserId, setBrowserId] = useState<string>();
6+
7+
useEffect(() => {
8+
const cookie = getCookie({ name: 'bwid', shouldMemoize: true });
9+
10+
const id = isString(cookie) ? cookie : 'no-browser-id-available';
11+
12+
setBrowserId(id);
13+
}, []);
14+
15+
return browserId;
16+
};

dotcom-rendering/src/lib/newsletter-sign-up-requests.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getCookie } from '@guardian/libs';
21
import type { TAction } from '@guardian/ophan-tracker-js';
32
import { submitComponentEvent } from '../client/ophan/ophan';
43
import type { RenderingTarget } from '../types/renderingTarget';
@@ -11,8 +10,6 @@ const buildNewsletterSignUpFormData = (
1110
): FormData => {
1211
const pageRef = window.location.origin + window.location.pathname;
1312
const refViewId = window.guardian.ophan?.pageViewId ?? '';
14-
const browserId =
15-
getCookie({ name: 'bwid', shouldMemoize: true }) ?? undefined;
1613

1714
const formData = new FormData();
1815
formData.append('email', emailAddress);
@@ -37,10 +34,6 @@ const buildNewsletterSignUpFormData = (
3734
formData.append('marketing', marketingOptIn ? 'true' : 'false');
3835
}
3936

40-
if (browserId !== undefined) {
41-
formData.append('browserId', browserId);
42-
}
43-
4437
return formData;
4538
};
4639

0 commit comments

Comments
 (0)