Skip to content

Commit c4fe69d

Browse files
Hybeshariombalhara
andauthored
Add Plausible (Custom) App (#8189)
Co-authored-by: Hariom Balhara <[email protected]>
1 parent 6fd64b2 commit c4fe69d

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

packages/app-store/BookingPageTagManager.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,33 @@ export default function BookingPageTagManager({
1717
if (!tag) {
1818
return null;
1919
}
20-
const trackingId = getEventTypeAppData(eventType, appId as keyof typeof appDataSchemas)?.trackingId;
21-
if (!trackingId) {
20+
21+
const appData = getEventTypeAppData(eventType, appId as keyof typeof appDataSchemas);
22+
23+
if (!appData) {
2224
return null;
2325
}
24-
const parseValue = <T extends string | undefined>(val: T): T =>
25-
//TODO: Support more template variables.
26-
val ? (val.replace(/\{TRACKING_ID\}/g, trackingId) as T) : val;
26+
27+
const parseValue = <T extends string | undefined>(val: T): T => {
28+
if (!val) {
29+
return val;
30+
}
31+
32+
// Only support UpperCase,_and numbers in template variables. This prevents accidental replacement of other strings.
33+
const regex = /\{([A-Z_\d]+)\}/g;
34+
let matches;
35+
while ((matches = regex.exec(val))) {
36+
const variableName = matches[1];
37+
if (appData[variableName]) {
38+
// Replace if value is available. It can possible not be a template variable that just matches the regex.
39+
val = val.replace(
40+
new RegExp(`{${variableName}}`, "g"),
41+
appData[variableName]
42+
) as NonNullable<T>;
43+
}
44+
}
45+
return val;
46+
};
2747

2848
return tag.scripts.map((script, index) => {
2949
const parsedAttributes: NonNullable<(typeof tag.scripts)[number]["attrs"]> = {};

packages/app-store/_utils/getEventTypeAppData.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ export const getEventTypeAppData = <T extends EventTypeAppsList>(
1515
const appMetadata = metadata?.apps && metadata.apps[appId];
1616
if (appMetadata) {
1717
const allowDataGet = forcedGet ? true : appMetadata.enabled;
18-
return allowDataGet ? appMetadata : null;
18+
return allowDataGet
19+
? {
20+
...appMetadata,
21+
// trackingId is legacy way to store value for TRACKING_ID. So, we need to support both.
22+
TRACKING_ID: appMetadata.TRACKING_ID || appMetadata.trackingId,
23+
}
24+
: null;
1925
}
2026

2127
// Backward compatibility for existing event types.

packages/app-store/plausible/components/EventTypeAppCardInterface.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { appDataSchema } from "../zod";
99

1010
const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({ app }) {
1111
const [getAppData, setAppData] = useAppContextWithSchema<typeof appDataSchema>();
12+
const plausibleUrl = getAppData("PLAUSIBLE_URL");
1213
const trackingId = getAppData("trackingId");
1314
const [enabled, setEnabled] = useState(getAppData("enabled"));
1415

@@ -24,8 +25,18 @@ const EventTypeAppCard: EventTypeAppCardComponent = function EventTypeAppCard({
2425
}
2526
}}
2627
switchChecked={enabled}>
28+
<TextField
29+
name="Plausible URL"
30+
defaultValue="https://plausible.io/js/script.js"
31+
placeholder="https://plausible.io/js/script.js"
32+
value={plausibleUrl}
33+
onChange={(e) => {
34+
setAppData("PLAUSIBLE_URL", e.target.value);
35+
}}
36+
/>
2737
<TextField
2838
name="Tracked Domain"
39+
placeholder="yourdomain.com"
2940
value={trackingId}
3041
onChange={(e) => {
3142
setAppData("trackingId", e.target.value);

packages/app-store/plausible/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"tag": {
1616
"scripts": [
1717
{
18-
"src": "https://plausible.io/js/script.js",
18+
"src": "{PLAUSIBLE_URL}",
1919
"attrs": {
2020
"data-domain": "{TRACKING_ID}"
2121
}

packages/app-store/plausible/zod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { eventTypeAppCardZod } from "../eventTypeAppCardZod";
44

55
export const appDataSchema = eventTypeAppCardZod.merge(
66
z.object({
7+
PLAUSIBLE_URL: z.string().optional().default("https://plausible.io/js/script.js").or(z.undefined()),
78
trackingId: z.string().default("").optional(),
89
})
910
);

0 commit comments

Comments
 (0)