Skip to content

Commit a5d15fa

Browse files
committed
chore: Only initialize FedCM if user is in test
1 parent 709eda7 commit a5d15fa

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { log } from '@guardian/libs';
22
import { useEffect } from 'react';
3+
import { useAB } from '../lib/useAB';
34
import { useIsSignedIn } from '../lib/useAuthStatus';
45
import type { StageType } from '../types/config';
56

@@ -50,8 +51,18 @@ type CredentialsProvider = {
5051

5152
export const GoogleOneTap = () => {
5253
const isSignedIn = useIsSignedIn();
54+
const abTests = useAB();
55+
const isUserInTest = abTests?.api.isUserInVariant(
56+
'GoogleOneTap',
57+
'variant',
58+
);
5359

5460
useEffect(() => {
61+
// Only initialize Google One Tap if the user is in the AB test. Currently 0% of users are in the test.
62+
if (!isUserInTest) return;
63+
64+
// FedCM has no knowledge of the user's auth state, so we need to check
65+
// if the user is already signed in before initializing it.
5566
if (isSignedIn === true) {
5667
log(
5768
'identity',
@@ -67,6 +78,13 @@ export const GoogleOneTap = () => {
6778
return;
6879
}
6980

81+
/**
82+
* Typescripts built-in DOM types do not include the full `CredentialsProvider`
83+
* interface, so we need to cast `window.navigator.credentials` to our own
84+
* `CredentialsProvider` type which includes the FedCM API.
85+
*
86+
* Related issue: https://github.com/microsoft/TypeScript/issues/60641
87+
*/
7088
const credentialsProvider = window.navigator
7189
.credentials as unknown as CredentialsProvider;
7290

@@ -94,6 +112,8 @@ export const GoogleOneTap = () => {
94112
'identity',
95113
'FedCM prompt failed, potentially due to user declining',
96114
);
115+
} else {
116+
throw error;
97117
}
98118
})
99119
.then((credentials) => {
@@ -105,7 +125,7 @@ export const GoogleOneTap = () => {
105125
log('identity', 'No FedCM credentials received');
106126
}
107127
});
108-
}, [isSignedIn]);
128+
}, [isSignedIn, isUserInTest]);
109129

110130
return <></>;
111131
};

0 commit comments

Comments
 (0)