Skip to content

Commit 1e14075

Browse files
committed
feat: customize localStorage key for analytics consents #302
1 parent 1a065b5 commit 1e14075

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/consentManagement/createConsentManagement.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useRerenderOnChange } from "../tools/StatefulObservable/hooks";
99
import { createConsentBannerAndConsentManagement } from "./ConsentBannerAndConsentManagement";
1010
import { isBrowser } from "../tools/isBrowser";
1111

12-
export const localStorageKeyPrefix = "@codegouvfr/react-dsfr finalityConsent";
12+
export const defaultLocalStorageKeyPrefix = "@codegouvfr/react-dsfr finalityConsent";
1313

1414
export function createConsentManagement<
1515
FinalityDescription extends Record<
@@ -21,10 +21,16 @@ export function createConsentManagement<
2121
consentCallback?: ConsentCallback<ExtractFinalityFromFinalityDescription<FinalityDescription>>;
2222
/** Optional: If you have a dedicated page that provides comprehensive information about your website's GDPR policies. */
2323
personalDataPolicyLinkProps?: RegisteredLinkProps;
24+
localStorageKeyPrefix?: string;
2425
}) {
2526
type Finality = ExtractFinalityFromFinalityDescription<FinalityDescription>;
2627

27-
const { finalityDescription, personalDataPolicyLinkProps, consentCallback } = params;
28+
const {
29+
finalityDescription,
30+
personalDataPolicyLinkProps,
31+
consentCallback,
32+
localStorageKeyPrefix
33+
} = params;
2834

2935
const finalities = getFinalitiesFromFinalityDescription({
3036
"finalityDescription":
@@ -33,7 +39,9 @@ export function createConsentManagement<
3339
: finalityDescription
3440
});
3541

36-
const localStorageKey = `${localStorageKeyPrefix} ${finalities.join("-")}`;
42+
const localStorageKey = `${
43+
localStorageKeyPrefix ?? defaultLocalStorageKeyPrefix
44+
} ${finalities.join("-")}`;
3745

3846
const $finalityConsent = createStatefulObservable<FinalityConsent<Finality> | undefined>(() => {
3947
if (!isBrowser) {
@@ -98,7 +106,8 @@ export function createConsentManagement<
98106
useConsent,
99107
ConsentBannerAndConsentManagement,
100108
FooterConsentManagementItem,
101-
FooterPersonalDataPolicyItem
109+
FooterPersonalDataPolicyItem,
110+
consentLocalStorageKey: localStorageKey
102111
};
103112
}
104113

stories/ConsentManagement.stories.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import { sectionName } from "./sectionName";
33
import { getStoryFactory } from "./getStory";
44
import { createConsentManagement } from "../dist/consentManagement";
5-
import { localStorageKeyPrefix } from "../dist/consentManagement/createConsentManagement";
5+
import { defaultLocalStorageKeyPrefix } from "../dist/consentManagement/createConsentManagement";
66
import { Placeholder } from "../dist/consentManagement/Placeholder";
77
import { Footer } from "../dist/Footer";
88
import { Button } from "../dist/Button";
@@ -34,7 +34,8 @@ export const {
3434
ConsentBannerAndConsentManagement,
3535
FooterConsentManagementItem,
3636
FooterPersonalDataPolicyItem,
37-
useConsent
37+
useConsent,
38+
consentLocalStorageKey
3839
} = createConsentManagement({
3940
/*
4041
Can be an object or a function that take the current language as argument.
@@ -121,7 +122,13 @@ export const {
121122
}
122123
*/
123124
124-
}
125+
},
126+
127+
/*
128+
This optional parameter let's you personalise the key that is used to store user's consents in the localStorage.
129+
The default value is "${defaultLocalStorageKeyPrefix}"
130+
*/
131+
"localStorageKeyPrefix": "company-name/app-name"
125132
});
126133
\`\`\`
127134
@@ -213,7 +220,8 @@ const {
213220
ConsentBannerAndConsentManagement,
214221
FooterConsentManagementItem,
215222
FooterPersonalDataPolicyItem,
216-
useConsent
223+
useConsent,
224+
consentLocalStorageKey
217225
} = createConsentManagement({
218226
"finalityDescription": {
219227
"advertising": {
@@ -284,7 +292,7 @@ function Story() {
284292
<Button
285293
onClick={() => {
286294
Object.keys(localStorage)
287-
.filter(key => key.startsWith(localStorageKeyPrefix))
295+
.filter(key => key.startsWith(consentLocalStorageKey))
288296
.forEach(key => localStorage.removeItem(key));
289297

290298
location.reload();

test/integration/vite/src/consentManagement.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export const {
66
ConsentBannerAndConsentManagement,
77
FooterConsentManagementItem,
88
FooterPersonalDataPolicyItem,
9-
useConsent
9+
useConsent,
10+
consentLocalStorageKey
1011
} = createConsentManagement({
1112
"finalityDescription": ({ lang }) => ({
1213
"advertising": {
@@ -41,5 +42,6 @@ export const {
4142
}
4243

4344
console.log("callback from gdpr hook");
44-
}
45+
},
46+
"localStorageKeyPrefix": "company-name/app-name finalityConsent"
4547
});

0 commit comments

Comments
 (0)