Skip to content

Commit

Permalink
move GPC preferences before we send fidesInitialized event (#3561)
Browse files Browse the repository at this point in the history
  • Loading branch information
eastandwestwind authored Jun 26, 2023
1 parent a081a87 commit 3a4798f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ The types of changes are:

### Added
- Empty state for when there are no relevant privacy notices in the privacy center [#3640](https://github.com/ethyca/fides/pull/3640)
### Fixed

### Fixed
- Render linebreaks in the Fides.js overlay descriptions, etc. [#3665](https://github.com/ethyca/fides/pull/3665)

### Changed
- Moved GPC preferences slightly earlier in Fides.js lifecycle [#3561](https://github.com/ethyca/fides/pull/3561)


## [2.15.0](https://github.com/ethyca/fides/compare/2.14.1...2.15.0)

### Added
Expand Down
25 changes: 12 additions & 13 deletions clients/fides-js/src/fides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ const init = async ({
_Fides.geolocation = geolocation;
_Fides.options = options;
_Fides.initialized = true;
dispatchFidesEvent("FidesInitialized", cookie);
dispatchFidesEvent("FidesUpdated", cookie);
dispatchFidesEvent("FidesInitialized", cookie, options.debug);
dispatchFidesEvent("FidesUpdated", cookie, options.debug);
}

let shouldInitOverlay: boolean = options.isOverlayEnabled;
Expand Down Expand Up @@ -255,6 +255,14 @@ const init = async ({
}
}
}
if (shouldInitOverlay) {
automaticallyApplyGPCPreferences(
cookie,
fidesRegionString,
options.fidesApiUrl,
effectiveExperience
);
}

// Initialize the window.Fides object
_Fides.consent = cookie.consent;
Expand All @@ -270,18 +278,9 @@ const init = async ({
// For convenience, also dispatch the "FidesUpdated" event; this allows
// listeners to ignore the initialization event if they prefer
if (!hasExistingCookie) {
dispatchFidesEvent("FidesInitialized", cookie);
}
dispatchFidesEvent("FidesUpdated", cookie);

if (shouldInitOverlay) {
automaticallyApplyGPCPreferences(
cookie,
fidesRegionString,
options.fidesApiUrl,
effectiveExperience
);
dispatchFidesEvent("FidesInitialized", cookie, options.debug);
}
dispatchFidesEvent("FidesUpdated", cookie, options.debug);
};

// The global Fides object; this is bound to window.Fides if available
Expand Down
10 changes: 9 additions & 1 deletion clients/fides-js/src/lib/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FidesCookie } from "./cookie";
import { debugLog } from "./consent-utils";

/**
* Defines the available event names:
Expand Down Expand Up @@ -40,13 +41,20 @@ export type FidesEvent = CustomEvent<FidesEventDetail>;
*/
export const dispatchFidesEvent = (
type: FidesEventType,
cookie: FidesCookie
cookie: FidesCookie,
debug: boolean
) => {
if (typeof window !== "undefined" && typeof CustomEvent !== "undefined") {
// Pick a subset of the Fides cookie properties to provide as event detail
const { consent }: FidesEventDetail = cookie;
const detail: FidesEventDetail = { consent };
const event = new CustomEvent(type, { detail });
debugLog(
debug,
`Dispatching event type ${type} with cookie consent ${JSON.stringify(
cookie?.consent
)}`
);
window.dispatchEvent(event);
}
};
2 changes: 1 addition & 1 deletion clients/fides-js/src/lib/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ export const updateConsentPreferences = ({
});

// 5. Dispatch a "FidesUpdated" event
dispatchFidesEvent("FidesUpdated", cookie);
dispatchFidesEvent("FidesUpdated", cookie, debug);
};
2 changes: 1 addition & 1 deletion clients/privacy-center/cypress/e2e/consent-banner.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe("Consent banner", () => {
});

describe("when user has no saved consent cookie", () => {
describe("when banner is not disabled", () => {
describe("when overlay is enabled", () => {
beforeEach(() => {
cy.getCookie(CONSENT_COOKIE_NAME).should("not.exist");
stubConfig({
Expand Down

0 comments on commit 3a4798f

Please sign in to comment.