-
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.
- Loading branch information
1 parent
15b79ee
commit e40b6aa
Showing
8 changed files
with
99 additions
and
36 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,16 +1,71 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
/** | ||
* Extension for GPP | ||
* | ||
* Usage: | ||
* Include as a script tag as early as possible (even before fides.js) | ||
*/ | ||
|
||
import { | ||
CmpApi, | ||
CmpDisplayStatus, | ||
CmpStatus, | ||
SignalStatus, | ||
} from "@iabgpp/cmpapi"; | ||
import { makeStub } from "../lib/gpp/stub"; | ||
import { fidesEventToTcString } from "../lib/tcf/events"; | ||
import { | ||
isPrivacyExperience, | ||
shouldResurfaceConsent, | ||
} from "../lib/consent-utils"; | ||
import { ETHYCA_CMP_ID } from "../lib/tcf/constants"; | ||
|
||
const CMP_VERSION = 1; | ||
|
||
const TCF_SECTION_ID = 2; | ||
|
||
export const initializeGppCmpApi = () => { | ||
makeStub(); | ||
|
||
// TODO: instantiate a real (non-stubbed) GPP CMP API and set up listeners | ||
const cmpApi = new CmpApi(ETHYCA_CMP_ID, CMP_VERSION); | ||
cmpApi.setApplicableSections([TCF_SECTION_ID]); | ||
cmpApi.setCmpStatus(CmpStatus.LOADED); | ||
|
||
// If consent does not need to be resurfaced, then we can set the signal to Ready here | ||
window.addEventListener("FidesInitialized", (event) => { | ||
const { experience } = window.Fides; | ||
if ( | ||
isPrivacyExperience(experience) && | ||
!shouldResurfaceConsent(experience, event.detail) | ||
) { | ||
cmpApi.setSignalStatus(SignalStatus.READY); | ||
} | ||
}); | ||
|
||
window.addEventListener("FidesUIShown", () => { | ||
cmpApi.setSignalStatus(SignalStatus.NOT_READY); | ||
cmpApi.setCmpDisplayStatus(CmpDisplayStatus.VISIBLE); | ||
}); | ||
|
||
window.addEventListener("FidesModalClosed", (event) => { | ||
cmpApi.setCmpDisplayStatus(CmpDisplayStatus.HIDDEN); | ||
// If the modal was closed without the user saving, set signal status back to Ready | ||
if ( | ||
event.detail.extraDetails && | ||
event.detail.extraDetails.saved === false | ||
) { | ||
cmpApi.setSignalStatus(SignalStatus.READY); | ||
} | ||
}); | ||
|
||
window.addEventListener("FidesUpdated", (event) => { | ||
const tcString = fidesEventToTcString(event); | ||
// Workaround for bug in base library https://github.com/IABTechLab/iabgpp-es/issues/35 | ||
cmpApi.setFieldValueBySectionId(TCF_SECTION_ID, "CmpId", CMP_ID); | ||
cmpApi.setSectionStringById(TCF_SECTION_ID, tcString ?? ""); | ||
cmpApi.fireSectionChange("tcfeuv2"); | ||
cmpApi.setSignalStatus(SignalStatus.READY); | ||
}); | ||
}; | ||
|
||
initializeGppCmpApi(); |
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
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,21 @@ | ||
import { FidesEvent } from "../events"; | ||
import { FIDES_SEPARATOR } from "./constants"; | ||
|
||
/** | ||
* Extract just the TC string from a FidesEvent. This will also remove parts of the | ||
* TC string that we do not want to surface with our CMP API events, such as | ||
* `vendors_disclosed` and our own AC string addition. | ||
*/ | ||
export const fidesEventToTcString = (event: FidesEvent) => { | ||
const { fides_string: cookieString } = event.detail; | ||
if (cookieString) { | ||
// Remove the AC portion which is separated by FIDES_SEPARATOR | ||
const [tcString] = cookieString.split(FIDES_SEPARATOR); | ||
// We only want to return the first part of the tcString, which is separated by '.' | ||
// This means Publisher TC is not sent either, which is okay for now since we do not set it. | ||
// However, if we do one day set it, we would have to decode the string and encode it again | ||
// without vendorsDisclosed | ||
return tcString.split(".")[0]; | ||
} | ||
return cookieString; | ||
}; |