Skip to content

Commit

Permalink
Merge pull request #310 from ocruze/feat/personalise-analytics-locals…
Browse files Browse the repository at this point in the history
…torage-key

feat: customize localStorage key for analytics consents #302
  • Loading branch information
garronej authored Sep 17, 2024
2 parents 0459355 + e89a22e commit bb2e83e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 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 = defaultLocalStorageKeyPrefix
} = params;

const finalities = getFinalitiesFromFinalityDescription({
"finalityDescription":
Expand Down
4 changes: 2 additions & 2 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 @@ -284,7 +284,7 @@ function Story() {
<Button
onClick={() => {
Object.keys(localStorage)
.filter(key => key.startsWith(localStorageKeyPrefix))
.filter(key => key.startsWith(defaultLocalStorageKeyPrefix))
.forEach(key => localStorage.removeItem(key));

location.reload();
Expand Down

0 comments on commit bb2e83e

Please sign in to comment.