-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dispatch Fides.js lifecycle events on window (FidesInitialized, Fides…
…Updated) and cross-publish to Fides.gtm() integration (#3454)
- Loading branch information
Showing
14 changed files
with
394 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,56 @@ | ||
import { CookieKeyConsent } from "../lib/cookie"; | ||
import { FidesEventDetail } from "../lib/events"; | ||
|
||
declare global { | ||
interface Window { | ||
dataLayer?: any[]; | ||
} | ||
} | ||
|
||
/** | ||
* Call Fides.gtm to configure Google Tag Manager. The user's consent choices will be | ||
* pushed into GTM's `dataLayer` under `Fides.consent`. | ||
* Defines the structure of the Fides variable pushed to the GTM data layer | ||
*/ | ||
export const gtm = () => { | ||
interface FidesVariable { | ||
consent: CookieKeyConsent; | ||
} | ||
|
||
// Helper function to push the Fides variable to the GTM data layer from a FidesEvent | ||
const pushFidesVariableToGTM = (fidesEvent: { | ||
type: string; | ||
detail: FidesEventDetail; | ||
}) => { | ||
// Initialize the dataLayer object, just in case we run before GTM is initialized | ||
const dataLayer = window.dataLayer ?? []; | ||
window.dataLayer = dataLayer; | ||
dataLayer.push({ | ||
Fides: { | ||
consent: window.Fides.consent, | ||
}, | ||
}); | ||
|
||
// Construct the Fides variable that will be pushed to GTM | ||
const Fides: FidesVariable = { | ||
consent: fidesEvent.detail.consent, | ||
}; | ||
|
||
// Push to the GTM dataLayer | ||
dataLayer.push({ event: fidesEvent.type, Fides }); | ||
}; | ||
|
||
/** | ||
* Call Fides.gtm() to configure the Fides <> Google Tag Manager integration. | ||
* The user's consent choices will automatically be pushed into GTM's | ||
* `dataLayer` under `Fides.consent` variable. | ||
*/ | ||
export const gtm = () => { | ||
// Listen for Fides events and cross-publish them to GTM | ||
window.addEventListener("FidesInitialized", (event) => | ||
pushFidesVariableToGTM(event) | ||
); | ||
window.addEventListener("FidesUpdated", (event) => | ||
pushFidesVariableToGTM(event) | ||
); | ||
|
||
// If Fides was already initialized, publish a synthetic event immediately | ||
if (window.Fides?.initialized) { | ||
pushFidesVariableToGTM({ | ||
type: "FidesInitialized", | ||
detail: { consent: window.Fides.consent }, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { FidesCookie } from "./cookie"; | ||
|
||
/** | ||
* Defines the available event names: | ||
* - FidesInitialized: dispatched when initialization is complete, from Fides.init() | ||
* - FidesUpdated: dispatched when preferences are updated, from updateConsentPreferences() or Fides.init() | ||
*/ | ||
export type FidesEventType = "FidesInitialized" | "FidesUpdated"; | ||
|
||
// Bonus points: update the WindowEventMap interface with our custom event types | ||
declare global { | ||
interface WindowEventMap { | ||
FidesInitialized: FidesEvent; | ||
FidesUpdated: FidesEvent; | ||
} | ||
} | ||
|
||
/** | ||
* Defines the properties available on event.detail. As of now, these are an | ||
* explicit subset of properties from the Fides cookie | ||
* TODO: add identity and meta? | ||
*/ | ||
export type FidesEventDetail = Pick<FidesCookie, "consent">; | ||
|
||
export type FidesEvent = CustomEvent<FidesEventDetail>; | ||
|
||
/** | ||
* Dispatch a custom event on the window object, providing the current Fides | ||
* state on the "detail" property of the event. | ||
* | ||
* Example usage: | ||
* ``` | ||
* window.addEventListener("FidesUpdated", (evt) => console.log("Fides.consent updated:", evt.detail.consent)); | ||
* ``` | ||
* | ||
* The snippet above will print a console log whenever consent preferences are initialized/updated, like: | ||
* ``` | ||
* Fides.consent updated: { data_sales_and_sharing: true } | ||
* ``` | ||
*/ | ||
export const dispatchFidesEvent = ( | ||
type: FidesEventType, | ||
cookie: FidesCookie | ||
) => { | ||
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 }); | ||
window.dispatchEvent(event); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.