1- import { getCookie , isUndefined } from '@guardian/libs' ;
1+ import { getCookie , isUndefined , storage } from '@guardian/libs' ;
22import { useState } from 'react' ;
33import {
44 hasCmpConsentForBrowserId ,
@@ -293,6 +293,7 @@ const fetchProxyGetTreatments = async (
293293 hasConsented : boolean ,
294294 shouldServeDismissible : boolean ,
295295 showDefaultGate : ShowGateValues ,
296+ gateDisplayCount : number ,
296297) : Promise < AuxiaProxyGetTreatmentsResponse > => {
297298 // pageId example: 'money/2017/mar/10/ministers-to-criminalise-use-of-ticket-tout-harvesting-software'
298299 const articleIdentifier = `www.theguardian.com/${ pageId } ` ;
@@ -317,6 +318,7 @@ const fetchProxyGetTreatments = async (
317318 hasConsented,
318319 shouldServeDismissible,
319320 showDefaultGate,
321+ gateDisplayCount,
320322 } ;
321323 const params = {
322324 method : 'POST' ,
@@ -372,6 +374,26 @@ const decideShowDefaultGate = (): ShowGateValues => {
372374 return undefined ;
373375} ;
374376
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+
375397const buildAuxiaGateDisplayData = async (
376398 contributionsServiceUrl : string ,
377399 pageId : string ,
@@ -410,8 +432,8 @@ const buildAuxiaGateDisplayData = async (
410432 }
411433
412434 const shouldServeDismissible = decideShouldServeDismissible ( ) ;
413-
414435 const showDefaultGate = decideShowDefaultGate ( ) ;
436+ const gateDisplayCount = getGateDisplayCount ( ) ;
415437
416438 const response = await fetchProxyGetTreatments (
417439 contributionsServiceUrl ,
@@ -430,6 +452,7 @@ const buildAuxiaGateDisplayData = async (
430452 readerPersonalData . hasConsented ,
431453 shouldServeDismissible ,
432454 showDefaultGate ,
455+ gateDisplayCount ,
433456 ) ;
434457
435458 if ( response . status && response . data ) {
@@ -724,6 +747,10 @@ const ShowSignInGateAuxia = ({
724747 } ,
725748 renderingTarget ,
726749 ) ;
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 ( ) ;
727754 } , [ componentId ] ) ;
728755
729756 return (
0 commit comments