Skip to content

Commit dde1bb7

Browse files
Merge pull request #14496 from guardian/jr/soi-recaptcha-newsleter-single
Soft Opt-in of Similar Guardian Product for the Single and embedded Newsletter
2 parents 838fb88 + 307020e commit dde1bb7

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { css } from '@emotion/react';
22
import { isString } from '@guardian/libs';
33
import type { ComponentEvent, TAction } from '@guardian/ophan-tracker-js';
4-
import { space, until } from '@guardian/source/foundations';
4+
import { space, textSans14, until } from '@guardian/source/foundations';
55
import {
66
Button,
7+
Checkbox,
8+
CheckboxGroup,
79
InlineError,
810
InlineSuccess,
911
Link,
@@ -19,6 +21,7 @@ import { useEffect, useRef, useState } from 'react';
1921
import ReactGoogleRecaptcha from 'react-google-recaptcha';
2022
import { submitComponentEvent } from '../client/ophan/ophan';
2123
import { lazyFetchEmailWithTimeout } from '../lib/fetchEmail';
24+
import { useIsSignedIn } from '../lib/useAuthStatus';
2225
import { palette } from '../palette';
2326
import type { RenderingTarget } from '../types/renderingTarget';
2427
import { useConfig } from './ConfigContext';
@@ -96,6 +99,13 @@ const errorContainerStyles = css`
9699
}
97100
`;
98101

102+
const optInCheckboxTextSmall = css`
103+
label > div {
104+
${textSans14};
105+
line-height: 16px;
106+
}
107+
`;
108+
99109
const ErrorMessageWithAdvice = ({ text }: { text?: string }) => (
100110
<InlineError>
101111
<span>
@@ -124,6 +134,7 @@ const buildFormData = (
124134
emailAddress: string,
125135
newsletterId: string,
126136
token: string,
137+
marketingOptIn?: boolean,
127138
): FormData => {
128139
const pageRef = window.location.origin + window.location.pathname;
129140
const refViewId = window.guardian.ophan?.pageViewId ?? '';
@@ -139,6 +150,10 @@ const buildFormData = (
139150
formData.append('g-recaptcha-response', token); // TO DO - PR on form handlers - is the token verified?
140151
}
141152

153+
if (marketingOptIn !== undefined) {
154+
formData.append('marketing', marketingOptIn ? 'true' : 'false');
155+
}
156+
142157
return formData;
143158
};
144159

@@ -267,6 +282,16 @@ export const SecureSignup = ({
267282
const [errorMessage, setErrorMessage] = useState<string | undefined>(
268283
undefined,
269284
);
285+
const [marketingOptIn, setMarketingOptIn] = useState<boolean | undefined>(
286+
undefined,
287+
);
288+
const isSignedIn = useIsSignedIn();
289+
290+
useEffect(() => {
291+
if (isSignedIn !== 'Pending' && !isSignedIn) {
292+
setMarketingOptIn(true);
293+
}
294+
}, [isSignedIn]);
270295

271296
useEffect(() => {
272297
setCaptchaSiteKey(window.guardian.config.page.googleRecaptchaSiteKey);
@@ -282,9 +307,17 @@ export const SecureSignup = ({
282307
const emailAddress: string = input?.value ?? '';
283308

284309
sendTracking(newsletterId, 'form-submission', renderingTarget, abTest);
310+
311+
const formData = buildFormData(
312+
emailAddress,
313+
newsletterId,
314+
token,
315+
marketingOptIn,
316+
);
317+
285318
const response = await postFormData(
286319
window.guardian.config.page.ajaxUrl + '/email',
287-
buildFormData(emailAddress, newsletterId, token),
320+
formData,
288321
);
289322

290323
// The response body could be accessed with await response.text()
@@ -383,6 +416,23 @@ export const SecureSignup = ({
383416
type="email"
384417
value={signedInUserEmail}
385418
/>
419+
{isSignedIn === false && (
420+
<CheckboxGroup
421+
name="marketing-preferences"
422+
label="Marketing preferences"
423+
hideLabel={true}
424+
cssOverrides={optInCheckboxTextSmall}
425+
>
426+
<Checkbox
427+
label="Get updates about our journalism and ways to support and enjoy our work."
428+
value="marketing-opt-in"
429+
checked={marketingOptIn}
430+
onChange={(e) =>
431+
setMarketingOptIn(e.target.checked)
432+
}
433+
/>
434+
</CheckboxGroup>
435+
)}
386436
<Button onClick={handleClick} size="small" type="submit">
387437
Sign up
388438
</Button>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ export const requestSingleSignUp = async (
8585
emailAddress: string,
8686
newsletterId: string,
8787
recaptchaToken: string,
88+
marketingOptIn?: boolean,
8889
): Promise<Response> => {
8990
const data = buildNewsletterSignUpFormData(
9091
emailAddress,
9192
newsletterId,
9293
recaptchaToken,
94+
marketingOptIn,
9395
);
9496

9597
return await postFormData(

0 commit comments

Comments
 (0)