Add Plausible (Custom) App (#8189)

Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
pull/8344/head
Ben Hybert 2023-04-18 14:54:51 +01:00 committed by GitHub
parent 6fd64b2209
commit c4fe69d826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 7 deletions

View File

@ -17,13 +17,33 @@ export default function BookingPageTagManager({
if (!tag) {
return null;
}
const trackingId = getEventTypeAppData(eventType, appId as keyof typeof appDataSchemas)?.trackingId;
if (!trackingId) {
const appData = getEventTypeAppData(eventType, appId as keyof typeof appDataSchemas);
if (!appData) {
return null;
}
const parseValue = <T extends string | undefined>(val: T): T =>
//TODO: Support more template variables.
val ? (val.replace(/\{TRACKING_ID\}/g, trackingId) as T) : val;
const parseValue = <T extends string | undefined>(val: T): T => {
if (!val) {
return val;
}
// Only support UpperCase,_and numbers in template variables. This prevents accidental replacement of other strings.
const regex = /\{([A-Z_\d]+)\}/g;
let matches;
while ((matches = regex.exec(val))) {
const variableName = matches[1];
if (appData[variableName]) {
// Replace if value is available. It can possible not be a template variable that just matches the regex.
val = val.replace(
new RegExp(`{${variableName}}`, "g"),
appData[variableName]
) as NonNullable<T>;
}
}
return val;
};
return tag.scripts.map((script, index) => {
const parsedAttributes: NonNullable<(typeof tag.scripts)[number]["attrs"]> = {};

View File

@ -15,7 +15,13 @@ export const getEventTypeAppData = <T extends EventTypeAppsList>(
const appMetadata = metadata?.apps && metadata.apps[appId];
if (appMetadata) {
const allowDataGet = forcedGet ? true : appMetadata.enabled;
return allowDataGet ? appMetadata : null;
return allowDataGet
? {
...appMetadata,
// trackingId is legacy way to store value for TRACKING_ID. So, we need to support both.
TRACKING_ID: appMetadata.TRACKING_ID || appMetadata.trackingId,
}
: null;
}
// Backward compatibility for existing event types.

View File

@ -9,6 +9,7 @@ import type { appDataSchema } from "../zod";
const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({ app }) {
const [getAppData, setAppData] = useAppContextWithSchema<typeof appDataSchema>();
const plausibleUrl = getAppData("PLAUSIBLE_URL");
const trackingId = getAppData("trackingId");
const [enabled, setEnabled] = useState(getAppData("enabled"));
@ -24,8 +25,18 @@ const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({
}
}}
switchChecked={enabled}>
<TextField
name="Plausible URL"
defaultValue="https://plausible.io/js/script.js"
placeholder="https://plausible.io/js/script.js"
value={plausibleUrl}
onChange={(e) => {
setAppData("PLAUSIBLE_URL", e.target.value);
}}
/>
<TextField
name="Tracked Domain"
placeholder="yourdomain.com"
value={trackingId}
onChange={(e) => {
setAppData("trackingId", e.target.value);

View File

@ -15,7 +15,7 @@
"tag": {
"scripts": [
{
"src": "https://plausible.io/js/script.js",
"src": "{PLAUSIBLE_URL}",
"attrs": {
"data-domain": "{TRACKING_ID}"
}

View File

@ -4,6 +4,7 @@ import { eventTypeAppCardZod } from "../eventTypeAppCardZod";
export const appDataSchema = eventTypeAppCardZod.merge(
z.object({
PLAUSIBLE_URL: z.string().optional().default("https://plausible.io/js/script.js").or(z.undefined()),
trackingId: z.string().default("").optional(),
})
);