-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6d31d5
commit 910430e
Showing
16 changed files
with
73 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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,55 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { z } from "zod"; | ||
|
||
const TRACK_EVENT_NAME = "fern-docs-track-analytics"; | ||
const TrackEventSchema = z.object({ | ||
event: z.string(), | ||
properties: z | ||
.record( | ||
z.union([ | ||
z.string(), | ||
z.number(), | ||
z.boolean(), | ||
z.string().array().readonly(), | ||
z.number().array().readonly(), | ||
z.boolean().array().readonly(), | ||
z.undefined(), | ||
]), | ||
) | ||
.optional(), | ||
internal: z.boolean().optional(), | ||
}); | ||
|
||
type TrackEvent = z.infer<typeof TrackEventSchema>; | ||
|
||
/** | ||
* Listen for track events, and emit them to analytics integrations. | ||
* | ||
* @param cb - The callback to emit the event to. | ||
* @param allowInternal - Whether to allow internal events to be emitted. Defaults to false. Only use this for Fern's internal posthog instance. | ||
*/ | ||
export function useSafeListenTrackEvents(cb: (detail: TrackEvent) => void, allowInternal = false): void { | ||
const ref = useRef<(detail: TrackEvent) => void>(cb); | ||
|
||
useEffect(() => { | ||
ref.current = cb; | ||
}); | ||
|
||
useEffect(() => { | ||
const handler = (event: Event) => { | ||
try { | ||
if (event instanceof CustomEvent) { | ||
const detail = TrackEventSchema.safeParse(event.detail); | ||
if (detail.success && (allowInternal || !detail.data.internal)) { | ||
ref.current(detail.data); | ||
} | ||
} | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.warn("Error emitting track event", error, event); | ||
} | ||
}; | ||
window.addEventListener(TRACK_EVENT_NAME, handler); | ||
return () => window.removeEventListener(TRACK_EVENT_NAME, handler); | ||
}, [allowInternal]); | ||
} |