Skip to content

Commit b1c2335

Browse files
authored
Extend expiration of user benefits cookies to 30 days (#14810)
This PR extends the expiration of all user benefits cookies to 30 days - GU_AF1, gu_allow_reject_all, gu_hide_support_messaging. The gu_user_benefits_expiry cookie continues to have an expiry of 1 day, so we'll recheck every day while the user is signed in. Previously they were short lived (1-2 days) but this resulted in edge cases where if a signed-in user didn't visit the site for more than a couple of days, when they returned their first page view wouldn't reflect their benefits (i.e. they would see ads). This is due to a race condition between the user benefits refresh and ads code. However, we don't want to delay ads until after the user benefits have been refreshed as that would impact performance. So instead, extend the expiration of the cookie. Note: this may result in a user getting benefits they no longer have on the first returning pageview, but this will be correct from the second page view onwards. We think this is OK. This also means we now remove cookies if the user benefits response says they no longer have the benefit, rather than simply letting the cookie expire. Note: this logic also exists in frontend and gateway, so has been updated in both those repos too: guardian/frontend#28352 guardian/gateway#3285
1 parent da75f6c commit b1c2335

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

dotcom-rendering/src/client/userFeatures/user-features.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* https://github.com/guardian/commercial/blob/1a429d6be05657f20df4ca909df7d01a5c3d7402/src/lib/user-features.ts
55
*/
66

7+
import { removeCookie } from '@guardian/libs';
78
import { getAuthStatus, isUserLoggedIn } from '../../lib/identity';
89
import { AD_FREE_USER_COOKIE } from './cookies/adFree';
910
import { ALLOW_REJECT_ALL_COOKIE } from './cookies/allowRejectAll';
@@ -36,28 +37,59 @@ const requestNewData = async () => {
3637
return syncDataFromUserBenefitsApi(authStatus).then(persistResponse);
3738
};
3839

40+
const USER_BENEFITS_COOKIE_EXPIRATION_IN_DAYS = 30;
41+
3942
/**
4043
* Persist the user benefits response to cookies
4144
*
42-
* If new cookies are added/removed/edited, update the persistUserBenefitsCookie function in Gateway
43-
* https://github.com/guardian/gateway/blob/252b2b2f24be826da42c6e7c1b1e202594184023/src/server/lib/user-features.ts#L88
44-
*
45+
* If new cookies are added/removed/edited, update the persistUserBenefitsCookies function in Gateway.
4546
* In gateway, the cookies are set after authentication.
4647
*
48+
* This code also exist in frontend, so any changes here should be mirrored there.
49+
*
4750
* @param {UserBenefits} userBenefitsResponse
4851
*/
4952
const persistResponse = (userBenefitsResponse: UserBenefits) => {
5053
createOrRenewCookie(USER_BENEFITS_EXPIRY_COOKIE);
54+
55+
// All of these user benefits cookies are now valid for 30 days. Previously
56+
// they were short lived (1-2 days) but this resulted in edge cases where if
57+
// a signed in user didn't visit the site for more than a couple of days
58+
// when they returned their first page view wouldn't reflect their benefits
59+
// (i.e. they would see ads). This is due to a race condition between the
60+
// user benefits refresh and the ads code. However, we don't want to delay
61+
// ads until after the user benefits have been refreshed as that would
62+
// impact performance. So instead, extend the expiry of the cookie. Note:
63+
// this may result in a user getting benefits they no longer have on the
64+
// first returning pageview, but this will be correct from the second page
65+
// view onwards. We think this is OK. This also means we now remove cookies
66+
// if the user benefits response says they no longer have the benefit,
67+
// rather than simply letting the cookie expire.
5168
if (userBenefitsResponse.hideSupportMessaging) {
52-
createOrRenewCookie(HIDE_SUPPORT_MESSAGING_COOKIE);
69+
createOrRenewCookie(
70+
HIDE_SUPPORT_MESSAGING_COOKIE,
71+
USER_BENEFITS_COOKIE_EXPIRATION_IN_DAYS,
72+
);
73+
} else {
74+
removeCookie({ name: HIDE_SUPPORT_MESSAGING_COOKIE });
5375
}
76+
5477
if (userBenefitsResponse.allowRejectAll) {
55-
createOrRenewCookie(ALLOW_REJECT_ALL_COOKIE);
78+
createOrRenewCookie(
79+
ALLOW_REJECT_ALL_COOKIE,
80+
USER_BENEFITS_COOKIE_EXPIRATION_IN_DAYS,
81+
);
82+
} else {
83+
removeCookie({ name: ALLOW_REJECT_ALL_COOKIE });
5684
}
57-
// Ad free cookie has an expiry of 2 days from now
58-
// See https://github.com/guardian/gateway/blob/52f810a88fa9ce23c6a794916251748718742c3d/src/server/lib/user-features.ts#L111-L115
85+
5986
if (userBenefitsResponse.adFree) {
60-
createOrRenewCookie(AD_FREE_USER_COOKIE, 2);
87+
createOrRenewCookie(
88+
AD_FREE_USER_COOKIE,
89+
USER_BENEFITS_COOKIE_EXPIRATION_IN_DAYS,
90+
);
91+
} else {
92+
removeCookie({ name: AD_FREE_USER_COOKIE });
6193
}
6294
};
6395

0 commit comments

Comments
 (0)