1
- import { getCookie , isUndefined } from '@guardian/libs' ;
1
+ import { getCookie , isUndefined , storage } from '@guardian/libs' ;
2
2
import { useState } from 'react' ;
3
3
import {
4
4
hasCmpConsentForBrowserId ,
@@ -293,6 +293,7 @@ const fetchProxyGetTreatments = async (
293
293
hasConsented : boolean ,
294
294
shouldServeDismissible : boolean ,
295
295
showDefaultGate : ShowGateValues ,
296
+ gateDisplayCount : number ,
296
297
) : Promise < AuxiaProxyGetTreatmentsResponse > => {
297
298
// pageId example: 'money/2017/mar/10/ministers-to-criminalise-use-of-ticket-tout-harvesting-software'
298
299
const articleIdentifier = `www.theguardian.com/${ pageId } ` ;
@@ -317,6 +318,7 @@ const fetchProxyGetTreatments = async (
317
318
hasConsented,
318
319
shouldServeDismissible,
319
320
showDefaultGate,
321
+ gateDisplayCount,
320
322
} ;
321
323
const params = {
322
324
method : 'POST' ,
@@ -372,6 +374,26 @@ const decideShowDefaultGate = (): ShowGateValues => {
372
374
return undefined ;
373
375
} ;
374
376
377
+ const getGateDisplayCount = ( ) : number => {
378
+ const rawValue = storage . local . getRaw ( 'gate_display_count' ) ;
379
+ const count = parseInt ( rawValue ?? '0' , 10 ) ;
380
+ return count ;
381
+ } ;
382
+
383
+ const incrementGateDisplayCount = ( ) => {
384
+ const count = getGateDisplayCount ( ) ;
385
+ const now = new Date ( ) ;
386
+ const oneYearFromNow = new Date ( now . getTime ( ) + 365 * 86400 ) ;
387
+ const newCount = count + 1 ;
388
+ // Using `storage.local.set`, instead of `storage.local.setRaw`
389
+ // because `setRaw` doesn't allow for specifying the duration.
390
+ storage . local . set (
391
+ 'gate_display_count' ,
392
+ newCount . toString ( ) ,
393
+ oneYearFromNow ,
394
+ ) ;
395
+ } ;
396
+
375
397
const buildAuxiaGateDisplayData = async (
376
398
contributionsServiceUrl : string ,
377
399
pageId : string ,
@@ -410,8 +432,8 @@ const buildAuxiaGateDisplayData = async (
410
432
}
411
433
412
434
const shouldServeDismissible = decideShouldServeDismissible ( ) ;
413
-
414
435
const showDefaultGate = decideShowDefaultGate ( ) ;
436
+ const gateDisplayCount = getGateDisplayCount ( ) ;
415
437
416
438
const response = await fetchProxyGetTreatments (
417
439
contributionsServiceUrl ,
@@ -430,6 +452,7 @@ const buildAuxiaGateDisplayData = async (
430
452
readerPersonalData . hasConsented ,
431
453
shouldServeDismissible ,
432
454
showDefaultGate ,
455
+ gateDisplayCount ,
433
456
) ;
434
457
435
458
if ( response . status && response . data ) {
@@ -724,6 +747,10 @@ const ShowSignInGateAuxia = ({
724
747
} ,
725
748
renderingTarget ,
726
749
) ;
750
+
751
+ // Once the gate is being displayed we need to update
752
+ // the tracking of the number of times the gate has been displayed
753
+ incrementGateDisplayCount ( ) ;
727
754
} , [ componentId ] ) ;
728
755
729
756
return (
0 commit comments