From ed87b4526f9f56278b2e52f7f1c627d6781191f6 Mon Sep 17 00:00:00 2001 From: Rupert Bates Date: Thu, 19 Dec 2024 12:55:57 +0000 Subject: [PATCH 1/2] We don't need one-time cookie to work out whether to hide support messaging --- .../src/client/userFeatures/user-features.ts | 2 - .../src/lib/contributions.test.ts | 85 ++----------------- dotcom-rendering/src/lib/contributions.ts | 68 +-------------- 3 files changed, 9 insertions(+), 146 deletions(-) diff --git a/dotcom-rendering/src/client/userFeatures/user-features.ts b/dotcom-rendering/src/client/userFeatures/user-features.ts index 333c9a2424..e226000d6a 100644 --- a/dotcom-rendering/src/client/userFeatures/user-features.ts +++ b/dotcom-rendering/src/client/userFeatures/user-features.ts @@ -51,8 +51,6 @@ const validateResponse = ( isObject(response) && isBoolean(response.showSupportMessaging) && isObject(response.contentAccess) && - isBoolean(response.contentAccess.paidMember) && - isBoolean(response.contentAccess.recurringContributor) && isBoolean(response.contentAccess.digitalPack) ); }; diff --git a/dotcom-rendering/src/lib/contributions.test.ts b/dotcom-rendering/src/lib/contributions.test.ts index dcd998d15a..0a5590ccef 100644 --- a/dotcom-rendering/src/lib/contributions.test.ts +++ b/dotcom-rendering/src/lib/contributions.test.ts @@ -1,14 +1,10 @@ import { setCookie, storage } from '@guardian/libs'; -import MockDate from 'mockdate'; import { - getLastOneOffContributionTimestamp, - hasSupporterCookie, HIDE_SUPPORT_MESSAGING_COOKIE, - isRecentOneOffContributor, NO_RR_BANNER_KEY, recentlyClosedBanner, setLocalNoBannerCachePeriod, - SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, + shouldHideSupportMessaging, withinLocalNoBannerCachePeriod, } from './contributions'; @@ -21,75 +17,6 @@ const clearAllCookies = () => { } }; -describe('getLastOneOffContributionTimestamp', () => { - beforeEach(clearAllCookies); - - it('returns a support cookie date if found', () => { - const somePastDate = 1582567969093; - setCookie({ - name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, - value: String(somePastDate), - }); - const lastOneOffContributionDate = getLastOneOffContributionTimestamp(); - expect(lastOneOffContributionDate).toBe(somePastDate); - }); - - it('returns undefined if the date cannot be parsed correctly', () => { - setCookie({ - name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, - value: 'NOT_A_DATE', - }); - - const lastOneOffContributionDate = getLastOneOffContributionTimestamp(); - expect(lastOneOffContributionDate).toBeUndefined(); - }); - - it('returns an empty string if no one-off contribution found', () => { - const lastOneOffContributionDate = getLastOneOffContributionTimestamp(); - expect(lastOneOffContributionDate).toBeUndefined(); - }); -}); - -describe('isRecentOneOffContributor', () => { - beforeEach(clearAllCookies); - afterEach(() => { - MockDate.reset(); - }); - - it('returns false if there is no one-off contribution cookie', () => { - expect(isRecentOneOffContributor()).toBe(false); - }); - - it('returns true if there are 5 days between the last contribution date and now', () => { - setCookie({ - name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, - value: Date.parse('2018-08-01').toString(), - }); - - MockDate.set(Date.parse('2018-08-07T10:50:34')); - expect(isRecentOneOffContributor()).toBe(true); - }); - - it('returns true if there are 0 days between the last contribution date and now', () => { - const theDate = Date.parse('2018-08-01T13:00:30'); - setCookie({ - name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, - value: theDate.toString(), - }); - MockDate.set(theDate); - expect(isRecentOneOffContributor()).toBe(true); - }); - - it('returns false if the one-off contribution was more than 3 months ago', () => { - setCookie({ - name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, - value: Date.parse('2018-08-01').toString(), - }); - MockDate.set(Date.parse('2019-08-01T13:00:30')); - expect(isRecentOneOffContributor()).toBe(false); - }); -}); - describe('getPurchaseInfo', () => { let getPurchaseInfo: () => any; @@ -176,7 +103,7 @@ describe('withinLocalNoBannerCachePeriod', () => { }); }); -describe('hasSupporterCookie', () => { +describe('shouldHideSupportMessaging', () => { beforeEach(clearAllCookies); it('returns false if cookie exists and is set to false', () => { @@ -184,7 +111,7 @@ describe('hasSupporterCookie', () => { name: HIDE_SUPPORT_MESSAGING_COOKIE, value: 'false', }); - expect(hasSupporterCookie(true)).toEqual(false); + expect(shouldHideSupportMessaging(true)).toEqual(false); }); it('returns true if cookie exists and is set to true', () => { @@ -192,14 +119,14 @@ describe('hasSupporterCookie', () => { name: HIDE_SUPPORT_MESSAGING_COOKIE, value: 'true', }); - expect(hasSupporterCookie(true)).toEqual(true); + expect(shouldHideSupportMessaging(true)).toEqual(true); }); it('returns false if cookie does not exist and user is signed out', () => { - expect(hasSupporterCookie(false)).toEqual(false); + expect(shouldHideSupportMessaging(false)).toEqual(false); }); it('returns Pending if cookie does not exist and user is signed in', () => { - expect(hasSupporterCookie(true)).toEqual('Pending'); + expect(shouldHideSupportMessaging(true)).toEqual('Pending'); }); }); diff --git a/dotcom-rendering/src/lib/contributions.ts b/dotcom-rendering/src/lib/contributions.ts index 59c0365996..f5024a2b58 100644 --- a/dotcom-rendering/src/lib/contributions.ts +++ b/dotcom-rendering/src/lib/contributions.ts @@ -1,9 +1,4 @@ -import { - getCookie, - isUndefined, - onConsentChange, - storage, -} from '@guardian/libs'; +import { getCookie, onConsentChange, storage } from '@guardian/libs'; import type { HeaderPayload } from '@guardian/support-dotcom-components/dist/dotcom/types'; import { useEffect, useState } from 'react'; import type { ArticleDeprecated } from '../types/article'; @@ -16,10 +11,6 @@ export const HIDE_SUPPORT_MESSAGING_COOKIE = 'gu_hide_support_messaging'; export const RECURRING_CONTRIBUTOR_COOKIE = 'gu_recurring_contributor'; export const OPT_OUT_OF_ARTICLE_COUNT_COOKIE = 'gu_article_count_opt_out'; -// Support Frontend cookie (created when a contribution is made) -export const SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE = - 'gu.contributions.contrib-timestamp'; - // Local storage keys const WEEKLY_ARTICLE_COUNT_KEY = 'gu.history.weeklyArticleCount'; export const NO_RR_BANNER_KEY = 'gu.noRRBanner'; @@ -28,12 +19,8 @@ export const NO_RR_BANNER_KEY = 'gu.noRRBanner'; export const MODULES_VERSION = 'v3'; // Returns true if we should hide support messaging because the user is a supporter. -// Checks the cookie that is set by the User Attributes API upon signing in. -// Value computed server-side and looks at all of the user's active products, -// including but not limited to recurring & one-off contributions, -// paper & digital subscriptions, as well as user tiers (GU supporters/staff/partners/patrons). -// https://github.com/guardian/members-data-api/blob/3a72dc00b9389968d91e5930686aaf34d8040c52/membership-attribute-service/app/models/Attributes.scala -export const hasSupporterCookie = ( +// Checks the cookie that is set by support-frontend on checkout and the User Attributes API upon signing in. +export const shouldHideSupportMessaging = ( isSignedIn: boolean, ): boolean | 'Pending' => { const cookie = getCookie({ name: HIDE_SUPPORT_MESSAGING_COOKIE }); @@ -56,55 +43,6 @@ export const hasSupporterCookie = ( } }; -// looks at the SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE (set by support-frontend when making one-off contribution) -// and returns a Unix epoch int of the date if it exists. -export const getLastOneOffContributionTimestamp = (): number | undefined => { - // Support cookies - expects Unix epoch - const contributionDateFromSupport = getCookie({ - name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, - }); - - if (!contributionDateFromSupport) { - return undefined; - } - - // Parse dates into common a number - const parsedDateFromSupport = contributionDateFromSupport - ? parseInt(contributionDateFromSupport, 10) - : 0; - - return parsedDateFromSupport || undefined; // This guards against 'parsedDateFromSupport' being NaN -}; - -const dateDiffDays = (from: number, to: number): number => { - const oneDayMs = 1000 * 60 * 60 * 24; - const diffMs = to - from; - return Math.floor(diffMs / oneDayMs); -}; - -const AskPauseDays = 90; - -export const isRecentOneOffContributor = (): boolean => { - const lastContributionDate = getLastOneOffContributionTimestamp(); - if (!isUndefined(lastContributionDate)) { - const now = Date.now(); - return dateDiffDays(lastContributionDate, now) <= AskPauseDays; - } - - return false; -}; - -export const shouldHideSupportMessaging = ( - isSignedIn: boolean, -): boolean | 'Pending' => { - const hasCookie = hasSupporterCookie(isSignedIn); - if (hasCookie === 'Pending') { - return 'Pending'; - } else { - return hasCookie || isRecentOneOffContributor(); - } -}; - export const REQUIRED_CONSENTS_FOR_ARTICLE_COUNT = [1, 3, 7]; const REQUIRED_CONSENTS_FOR_BROWSER_ID = [1, 3, 5, 7]; From 2ccdae123b98f1783187fa85ba315f581534df94 Mon Sep 17 00:00:00 2001 From: Rupert Bates Date: Thu, 19 Dec 2024 12:59:30 +0000 Subject: [PATCH 2/2] We don't need recurring contributor cookie either --- dotcom-rendering/src/lib/contributions.ts | 4 ++-- .../src/lib/readerRevenueDevUtils.ts | 22 ++----------------- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/dotcom-rendering/src/lib/contributions.ts b/dotcom-rendering/src/lib/contributions.ts index f5024a2b58..fc7276ab32 100644 --- a/dotcom-rendering/src/lib/contributions.ts +++ b/dotcom-rendering/src/lib/contributions.ts @@ -6,9 +6,9 @@ import type { DCRFrontType } from '../types/front'; import type { DCRNewslettersPageType } from '../types/newslettersPage'; import type { DCRTagPageType } from '../types/tagPage'; -// User Attributes API cookies (created on sign-in) +// User Attributes API cookie (created on sign-in) export const HIDE_SUPPORT_MESSAGING_COOKIE = 'gu_hide_support_messaging'; -export const RECURRING_CONTRIBUTOR_COOKIE = 'gu_recurring_contributor'; + export const OPT_OUT_OF_ARTICLE_COUNT_COOKIE = 'gu_article_count_opt_out'; // Local storage keys diff --git a/dotcom-rendering/src/lib/readerRevenueDevUtils.ts b/dotcom-rendering/src/lib/readerRevenueDevUtils.ts index 53f2975ab7..5d4053b873 100644 --- a/dotcom-rendering/src/lib/readerRevenueDevUtils.ts +++ b/dotcom-rendering/src/lib/readerRevenueDevUtils.ts @@ -1,16 +1,8 @@ import { getCookie, removeCookie, setCookie, storage } from '@guardian/libs'; -import { - HIDE_SUPPORT_MESSAGING_COOKIE, - RECURRING_CONTRIBUTOR_COOKIE, - SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, -} from './contributions'; +import { HIDE_SUPPORT_MESSAGING_COOKIE } from './contributions'; import { getLocaleCode } from './getCountryCode'; -const readerRevenueCookies = [ - HIDE_SUPPORT_MESSAGING_COOKIE, - RECURRING_CONTRIBUTOR_COOKIE, - SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, -]; +const readerRevenueCookies = [HIDE_SUPPORT_MESSAGING_COOKIE]; const clearEpicViewLog = (): void => storage.local.remove('gu.contributions.views'); @@ -22,12 +14,6 @@ const clearBannerLastClosedAt = (): void => { storage.local.remove('gu.noRRBannerTimestamp'); }; -const fakeOneOffContributor = (): void => - setCookie({ - name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, - value: Date.now().toString(), - }); - const MULTIVARIATE_ID_COOKIE = 'GU_mvt_id'; const MAX_CLIENT_MVT_ID = 1000000; const incrementMvtCookie = (): void => { @@ -76,10 +62,6 @@ const clearCommonReaderRevenueStateAndReload = ( for (const cookie of readerRevenueCookies) removeCookie({ name: cookie }); clearEpicViewLog(); - if (asExistingSupporter) { - fakeOneOffContributor(); - } - window.location.reload(); };