File tree Expand file tree Collapse file tree 5 files changed +45
-7
lines changed
Expand file tree Collapse file tree 5 files changed +45
-7
lines changed Original file line number Diff line number Diff 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 ( / \{ T R A C K I N G _ I D \} / 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" ] > = { } ;
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import type { appDataSchema } from "../zod";
99
1010const 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 ) ;
Original file line number Diff line number Diff line change 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 }
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import { eventTypeAppCardZod } from "../eventTypeAppCardZod";
44
55export 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) ;
You can’t perform that action at this time.
0 commit comments