Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
AntButton as Button,
Code,
FormControl,
FormLabel,
Input,
Link,
Modal,
ModalBody,
Expand All @@ -11,38 +14,24 @@ import {
Text,
useDisclosure,
} from "fidesui";
import { useMemo, useRef } from "react";
import { useMemo, useRef, useState } from "react";

import ClipboardButton from "~/features/common/ClipboardButton";
import { useFeatures } from "~/features/common/features";
import { CopyIcon } from "~/features/common/Icon";
import { useGetFidesCloudConfigQuery } from "~/features/plus/plus.slice";

const PRIVACY_CENTER_HOSTNAME_TEMPLATE = "{privacy-center-hostname-and-path}";
const FIDES_JS_SCRIPT_TEMPLATE = `<script src="https://${PRIVACY_CENTER_HOSTNAME_TEMPLATE}/fides.js"></script>`;
const FIDES_GTM_SCRIPT_TAG = "<script>Fides.gtm()</script>";
import {
getFidesJsScriptTemplate,
} from "~/features/privacy-experience/fidesJsScriptTemplate";

const JavaScriptTag = () => {
const modal = useDisclosure();
const initialRef = useRef(null);
const { fidesCloud: isFidesCloud } = useFeatures();

const { data: fidesCloudConfig, isSuccess } = useGetFidesCloudConfigQuery(
undefined,
{
skip: !isFidesCloud,
},
);
const [privacyCenterHostname, setPrivacyCenterHostname] = useState("");

const fidesJsScriptTag = useMemo(
() =>
isFidesCloud && isSuccess && fidesCloudConfig?.privacy_center_url
? FIDES_JS_SCRIPT_TEMPLATE.replace(
PRIVACY_CENTER_HOSTNAME_TEMPLATE,
fidesCloudConfig.privacy_center_url,
)
: FIDES_JS_SCRIPT_TEMPLATE,
[fidesCloudConfig?.privacy_center_url, isFidesCloud, isSuccess],
() => {
return getFidesJsScriptTemplate(privacyCenterHostname);
},
[privacyCenterHostname],
);

return (
Expand Down Expand Up @@ -74,33 +63,40 @@ const JavaScriptTag = () => {
<Stack spacing={3}>
<Text>
Copy the code below and paste it onto every page of your
website, as high up in the &lt;head&gt; as possible. Replace the
bracketed component with your privacy center&apos;s hostname and
path.
website, as high up in the &lt;head&gt; as possible. Enter your
privacy center&apos;s hostname and path below to generate the
complete script.
</Text>
<FormControl>
<FormLabel>Privacy Center Hostname</FormLabel>
<Input
value={privacyCenterHostname}
onChange={(e) => setPrivacyCenterHostname(e.target.value)}
placeholder="e.g.,<your-domain>.fides-cdn.ethyca.com"
data-testid="privacy-center-hostname-input"
/>
</FormControl>
<Code
display="flex"
justifyContent="space-between"
alignItems="center"
alignItems="flex-start"
p={0}
position="relative"
>
<Text p={4}>{fidesJsScriptTag}</Text>
<Text
p={4}
whiteSpace="pre"
fontFamily="mono"
fontSize="sm"
flex={1}
maxH="400px"
overflowX="auto"
overflowY="auto"
>
{fidesJsScriptTag}
</Text>
<ClipboardButton copyText={fidesJsScriptTag} />
</Code>
<Text>
Optionally, you can enable Google Tag Manager for managing tags
on your website by including the script tag below along with the
Fides.js tag. Place it below the Fides.js script tag.
</Text>
<Code
display="flex"
justifyContent="space-between"
alignItems="center"
p={0}
>
<Text p={4}>{FIDES_GTM_SCRIPT_TAG}</Text>
<ClipboardButton copyText={FIDES_GTM_SCRIPT_TAG} />
</Code>
<Text>
For more information about adding a JavaScript tag to your
website, please visit{" "}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
AntButton as Button,
Code,
FormControl,
FormLabel,
Input,
Link,
Modal,
ModalBody,
Expand All @@ -11,18 +14,12 @@ import {
Text,
useDisclosure,
} from "fidesui";
import { useMemo, useRef } from "react";
import { useMemo, useRef, useState } from "react";

import ClipboardButton from "~/features/common/ClipboardButton";
import { useFeatures } from "~/features/common/features";
import { GearLightIcon } from "~/features/common/Icon";
import { useGetFidesCloudConfigQuery } from "~/features/plus/plus.slice";
import { Property } from "~/types/api";

const PRIVACY_CENTER_HOSTNAME_TEMPLATE = "{privacy-center-hostname-and-path}";
const PROPERTY_UNIQUE_ID_TEMPLATE = "{property-unique-id}";
const FIDES_JS_SCRIPT_TEMPLATE = `<script src="https://${PRIVACY_CENTER_HOSTNAME_TEMPLATE}/fides.js?property_id=${PROPERTY_UNIQUE_ID_TEMPLATE}"></script>`;
const FIDES_GTM_SCRIPT_TAG = "<script>Fides.gtm()</script>";
import { getFidesJsScriptTemplate} from "./fidesJsScriptTemplate";

interface Props {
property: Property;
Expand All @@ -31,29 +28,13 @@ interface Props {
const NewJavaScriptTag = ({ property }: Props) => {
const modal = useDisclosure();
const initialRef = useRef(null);
const { fidesCloud: isFidesCloud } = useFeatures();
const [privacyCenterHostname, setPrivacyCenterHostname] = useState("");

const { data: fidesCloudConfig, isSuccess } = useGetFidesCloudConfigQuery(
undefined,
{
skip: !isFidesCloud,
},
const fidesJsScriptTag = useMemo(
() => getFidesJsScriptTemplate(privacyCenterHostname, property),
[privacyCenterHostname, property],
);

const fidesJsScriptTag = useMemo(() => {
const script = FIDES_JS_SCRIPT_TEMPLATE.replace(
PROPERTY_UNIQUE_ID_TEMPLATE,
property.id!.toString(),
);
if (isFidesCloud && isSuccess && fidesCloudConfig?.privacy_center_url) {
script.replace(
PRIVACY_CENTER_HOSTNAME_TEMPLATE,
fidesCloudConfig.privacy_center_url,
);
}
return script;
}, [fidesCloudConfig?.privacy_center_url, isFidesCloud, isSuccess, property]);

return (
<>
<Button
Expand Down Expand Up @@ -88,32 +69,39 @@ const NewJavaScriptTag = ({ property }: Props) => {
{property.name} property.
</Text>
<Text>
1. Paste this code as high in the <b>&lt;head&gt;</b> of the
Paste this code as high in the <b>&lt;head&gt;</b> of the
page as possible:
</Text>
<FormControl>
<FormLabel>Privacy Center Hostname</FormLabel>
<Input
value={privacyCenterHostname}
onChange={(e) => setPrivacyCenterHostname(e.target.value)}
placeholder="e.g.,<your-domain>.fides-cdn.ethyca.com"
data-testid="privacy-center-hostname-input"
/>
</FormControl>
<Code
display="flex"
justifyContent="space-between"
alignItems="top"
alignItems="flex-start"
p={0}
position="relative"
>
<Text p={4}>{fidesJsScriptTag}</Text>
<Text
p={4}
whiteSpace="pre"
fontFamily="mono"
fontSize="sm"
flex={1}
maxH="400px"
overflowX="auto"
overflowY="auto"
>
{fidesJsScriptTag}
</Text>
<ClipboardButton copyText={fidesJsScriptTag} />
</Code>
<Text>
2. Optionally, you can enable Google Tag Manager for managing
tags on your website by including the script tag below along
with the Fides.js tag. Place it below the Fides.js script tag.
</Text>
<Code
display="flex"
justifyContent="space-between"
alignItems="center"
p={0}
>
<Text p={4}>{FIDES_GTM_SCRIPT_TAG}</Text>
<ClipboardButton copyText={FIDES_GTM_SCRIPT_TAG} />
</Code>
<Text>
For more information about adding a JavaScript tag to your
website, please visit{" "}
Expand Down
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}";
Copy link
Contributor

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?

Copy link
Contributor Author

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.

};
/**
* 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;
};
Loading