Skip to content

Commit

Permalink
Check consents for gu.alreadyVisited (#26762)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrf1 authored Dec 13, 2023
1 parent 11e9b12 commit ad19da2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
24 changes: 24 additions & 0 deletions static/src/javascripts/bootstraps/standard/alreadyVisited.ts
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);
}
};
9 changes: 2 additions & 7 deletions static/src/javascripts/bootstraps/standard/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
import fastdom from 'fastdom';
import raven from 'lib/raven';
import userPrefs from 'common/modules/user-prefs';
import { storage } from '@guardian/libs';
import { fetchJson } from 'lib/fetch-json';
import { mediator } from 'lib/mediator';
import { addEventListener } from 'lib/events';
import { addCookie } from 'lib/cookies';
import { catchErrorsWithContext } from 'lib/robust';
import { markTime } from 'lib/user-timing';
import { isBreakpoint } from 'lib/detect';
Expand All @@ -35,6 +32,7 @@ import ophan from 'ophan/ng';
import { initAtoms } from './atoms';
import { initEmbedResize } from "./emailEmbeds";
import { setAdFreeCookie } from 'common/modules/commercial/user-features';
import { incrementAlreadyVisited } from "bootstraps/standard/alreadyVisited";

const showHiringMessage = () => {
try {
Expand Down Expand Up @@ -159,11 +157,8 @@ const bootStandard = () => {
setAdFreeCookie(1);
}

// set local storage: gu.alreadyVisited
if (window.guardian.isEnhanced) {
const key = 'gu.alreadyVisited';
const alreadyVisited = parseInt(storage.local.getRaw(key), 10) || 0;
storage.local.setRaw(key, alreadyVisited + 1);
void incrementAlreadyVisited();
}

ophan.setEventEmitter(mediator);
Expand Down

0 comments on commit ad19da2

Please sign in to comment.