-
Notifications
You must be signed in to change notification settings - Fork 85
Fides JS Code Snippet - Include wrapper script; add text field to set Privacy Center host URL #7081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jjdaurora
wants to merge
8
commits into
main
Choose a base branch
from
jjdaurora/ENG-1944-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aace664
add text field for privacy center URL entry;
jjdaurora a119a94
update comments;
jjdaurora 098443d
fidesJsScriptTemplate; add property ID removal function;
jjdaurora d01e51f
Remove gvl.json and run_migrations.py from version control
jjdaurora 452c3d0
Merge branch 'main' into jjdaurora/ENG-1944-v2
jjdaurora 5331233
fidesJsScriptTemplate; refactor logic;
jjdaurora 1e1cedb
fidesJsScriptTemplate; comment updates;
jjdaurora 9128e57
update template variable syntax;
jjdaurora File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
77 changes: 77 additions & 0 deletions
77
clients/admin-ui/src/features/privacy-experience/fidesJsScriptTemplate.ts
This file contains hidden or 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,77 @@ | ||
| import { Property } from "~/types/api"; | ||
|
|
||
| export const PRIVACY_CENTER_HOSTNAME_TEMPLATE = "{privacy-center-hostname-and-path}"; | ||
| /** | ||
| * Generates the property ID query parameter string for the Fides.js script URL | ||
| */ | ||
| const getPropertyIdTemplate = (propertyId?: string | null): string => { | ||
| if (propertyId) { | ||
| return propertyId; | ||
| } | ||
| // when there is no property id, we use a placeholder | ||
| return "{property-unique-id}"; | ||
| }; | ||
| /** | ||
| * Generates the complete Fides.js script tag with privacy center hostname and property ID | ||
| */ | ||
| export const getFidesJsScriptTemplate = (privacyCenterHostname?: string, property?: Property): string => { | ||
| const propertyId = property?.id; | ||
| const propertyIdDeclaration = propertyId ? `var fidesPropertyId = "${getPropertyIdTemplate(propertyId)}"; // Example Property ID\n ` : ""; | ||
| const propertyIdQueryParam = propertyId ? ` + "property_id=" + fidesPropertyId` : ""; | ||
|
|
||
| let script = `<script> | ||
| (function () { | ||
| var fidesHost = ${privacyCenterHostname ? `"${privacyCenterHostname}"` : `"${PRIVACY_CENTER_HOSTNAME_TEMPLATE}"`}; // This should be the CDN url. | ||
| ${propertyIdDeclaration}var fidesSrc = fidesHost + "/fides.js?"${propertyIdQueryParam}; | ||
|
|
||
| function insertFidesScript() { | ||
| addEventListener("FidesInitializing", function () { | ||
| // pre-initialization code goes here. e.g., tag manager integrations | ||
| // example: window.Fides.gtm(); | ||
| }); | ||
|
|
||
| addEventListener("FidesInitialized", function () { | ||
| // example: addExperienceIdToBody(); | ||
| }); | ||
|
|
||
| function addExperienceIdToBody() { | ||
| try { | ||
| var experience = window.Fides.experience || {}; | ||
| var config = experience.experience_config || {}; | ||
| var id = config.id; | ||
| if (id) { | ||
| window.document.body.classList.add(id); | ||
| } | ||
| } catch { | ||
| console.warn("Couldn't apply Fides experience ID to body."); | ||
| } | ||
| } | ||
|
|
||
| var fidesPrefix = "fides_"; | ||
| var searchParams = new URLSearchParams(location.search); | ||
| var fidesSearchParams = new URLSearchParams(); | ||
| searchParams.forEach(function (value, key) { | ||
| if (key.startsWith(fidesPrefix)) { | ||
| fidesSearchParams.set( | ||
| key.replace(fidesPrefix, ""), | ||
| key === fidesPrefix + "cache_bust" ? Date.now().toString() + "&refresh=true" : value | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| var src = fidesSrc + fidesSearchParams.toString(); | ||
| var script = document.createElement("script"); | ||
| script.async = false; | ||
| script.defer = false; | ||
| script.setAttribute("src", src); | ||
| script.setAttribute("id", "fides-js"); | ||
| script.setAttribute("type", "text/javascript"); | ||
| document.head.appendChild(script); | ||
| } | ||
|
|
||
| insertFidesScript(); | ||
| })(); | ||
| </script>`; | ||
|
|
||
| return script; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this return value ever used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it will return
"{property-unique-id}"when there isn't a property in scope, which means this version will be rendered when accessing the script from the Experiences screen. I can add a comment that explains this.