-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check consents for gu.alreadyVisited (#26762)
- Loading branch information
Showing
2 changed files
with
26 additions
and
7 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
static/src/javascripts/bootstraps/standard/alreadyVisited.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { onConsent } from '@guardian/consent-management-platform'; | ||
|
||
/** | ||
* This local storage item is used to target ads if a user has the correct consents | ||
*/ | ||
const AlreadyVisitedKey = 'gu.alreadyVisited'; | ||
|
||
const getAlreadyVisitedCount = (): number => { | ||
const alreadyVisited = parseInt( | ||
localStorage.getItem(AlreadyVisitedKey) ?? '', | ||
10, | ||
); | ||
return !Number.isNaN(alreadyVisited) ? alreadyVisited : 0; | ||
}; | ||
|
||
export const incrementAlreadyVisited = async (): Promise<void> => { | ||
const { canTarget } = await onConsent(); | ||
if (canTarget) { | ||
const alreadyVisited = getAlreadyVisitedCount() + 1; | ||
localStorage.setItem(AlreadyVisitedKey, alreadyVisited.toString()); | ||
} else { | ||
localStorage.removeItem(AlreadyVisitedKey); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters