Skip to content

Commit 8e544fc

Browse files
committed
Extend expiry of user benefits cookies to 30 days
Previouly 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 the 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 expiry 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.
1 parent 75addc2 commit 8e544fc

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

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

Lines changed: 26 additions & 5 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';
@@ -48,16 +49,36 @@ const requestNewData = async () => {
4849
*/
4950
const persistResponse = (userBenefitsResponse: UserBenefits) => {
5051
createOrRenewCookie(USER_BENEFITS_EXPIRY_COOKIE);
52+
53+
// All of these user benefits cookies are now valid for 30 days. Previously
54+
// they were short lived (1-2 days) but this resulted in edge cases where if
55+
// a signed in user didn't visit the site for more than a couple of days
56+
// when they returned their first page view wouldn't reflect their benefits
57+
// (i.e. they would see ads). This is due to a race condition between the
58+
// user benefits refresh and the ads code. However, we don't want to delay
59+
// ads until after the user benefits have been refreshed as that would
60+
// impact performance. So instead, extend the expiry of the cookie. Note:
61+
// this may result in a user getting benefits they no longer have on the
62+
// first returning pageview, but this will be correct from the second page
63+
// view onwards. We think this is OK. This also means we now remove cookies
64+
// if the user benefits response says they no longer have the benefit,
65+
// rather than simply letting the cookie expire.
5166
if (userBenefitsResponse.hideSupportMessaging) {
52-
createOrRenewCookie(HIDE_SUPPORT_MESSAGING_COOKIE);
67+
createOrRenewCookie(HIDE_SUPPORT_MESSAGING_COOKIE, 30);
68+
} else {
69+
removeCookie({ name: HIDE_SUPPORT_MESSAGING_COOKIE });
5370
}
71+
5472
if (userBenefitsResponse.allowRejectAll) {
55-
createOrRenewCookie(ALLOW_REJECT_ALL_COOKIE);
73+
createOrRenewCookie(ALLOW_REJECT_ALL_COOKIE, 30);
74+
} else {
75+
removeCookie({ name: ALLOW_REJECT_ALL_COOKIE });
5676
}
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
77+
5978
if (userBenefitsResponse.adFree) {
60-
createOrRenewCookie(AD_FREE_USER_COOKIE, 2);
79+
createOrRenewCookie(AD_FREE_USER_COOKIE, 30);
80+
} else {
81+
removeCookie({ name: AD_FREE_USER_COOKIE });
6182
}
6283
};
6384

0 commit comments

Comments
 (0)