Skip to content

Commit

Permalink
feat: customize localStorage key for analytics consents #302
Browse files Browse the repository at this point in the history
  • Loading branch information
ocruze committed Sep 16, 2024
1 parent 1a065b5 commit 1e14075
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
17 changes: 13 additions & 4 deletions src/consentManagement/createConsentManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useRerenderOnChange } from "../tools/StatefulObservable/hooks";
import { createConsentBannerAndConsentManagement } from "./ConsentBannerAndConsentManagement";
import { isBrowser } from "../tools/isBrowser";

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

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

const { finalityDescription, personalDataPolicyLinkProps, consentCallback } = params;
const {
finalityDescription,
personalDataPolicyLinkProps,
consentCallback,
localStorageKeyPrefix
} = params;

const finalities = getFinalitiesFromFinalityDescription({
"finalityDescription":
Expand All @@ -33,7 +39,9 @@ export function createConsentManagement<
: finalityDescription
});

const localStorageKey = `${localStorageKeyPrefix} ${finalities.join("-")}`;
const localStorageKey = `${
localStorageKeyPrefix ?? defaultLocalStorageKeyPrefix
} ${finalities.join("-")}`;

const $finalityConsent = createStatefulObservable<FinalityConsent<Finality> | undefined>(() => {
if (!isBrowser) {
Expand Down Expand Up @@ -98,7 +106,8 @@ export function createConsentManagement<
useConsent,
ConsentBannerAndConsentManagement,
FooterConsentManagementItem,
FooterPersonalDataPolicyItem
FooterPersonalDataPolicyItem,
consentLocalStorageKey: localStorageKey
};
}

Expand Down
18 changes: 13 additions & 5 deletions stories/ConsentManagement.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { sectionName } from "./sectionName";
import { getStoryFactory } from "./getStory";
import { createConsentManagement } from "../dist/consentManagement";
import { localStorageKeyPrefix } from "../dist/consentManagement/createConsentManagement";
import { defaultLocalStorageKeyPrefix } from "../dist/consentManagement/createConsentManagement";
import { Placeholder } from "../dist/consentManagement/Placeholder";
import { Footer } from "../dist/Footer";
import { Button } from "../dist/Button";
Expand Down Expand Up @@ -34,7 +34,8 @@ export const {
ConsentBannerAndConsentManagement,
FooterConsentManagementItem,
FooterPersonalDataPolicyItem,
useConsent
useConsent,
consentLocalStorageKey
} = createConsentManagement({
/*
Can be an object or a function that take the current language as argument.
Expand Down Expand Up @@ -121,7 +122,13 @@ export const {
}
*/
}
},
/*
This optional parameter let's you personalise the key that is used to store user's consents in the localStorage.
The default value is "${defaultLocalStorageKeyPrefix}"
*/
"localStorageKeyPrefix": "company-name/app-name"
});
\`\`\`
Expand Down Expand Up @@ -213,7 +220,8 @@ const {
ConsentBannerAndConsentManagement,
FooterConsentManagementItem,
FooterPersonalDataPolicyItem,
useConsent
useConsent,
consentLocalStorageKey
} = createConsentManagement({
"finalityDescription": {
"advertising": {
Expand Down Expand Up @@ -284,7 +292,7 @@ function Story() {
<Button
onClick={() => {
Object.keys(localStorage)
.filter(key => key.startsWith(localStorageKeyPrefix))
.filter(key => key.startsWith(consentLocalStorageKey))
.forEach(key => localStorage.removeItem(key));

location.reload();
Expand Down
6 changes: 4 additions & 2 deletions test/integration/vite/src/consentManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const {
ConsentBannerAndConsentManagement,
FooterConsentManagementItem,
FooterPersonalDataPolicyItem,
useConsent
useConsent,
consentLocalStorageKey
} = createConsentManagement({
"finalityDescription": ({ lang }) => ({
"advertising": {
Expand Down Expand Up @@ -41,5 +42,6 @@ export const {
}

console.log("callback from gdpr hook");
}
},
"localStorageKeyPrefix": "company-name/app-name finalityConsent"
});

0 comments on commit 1e14075

Please sign in to comment.