-
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
6506692
commit d951b9a
Showing
21 changed files
with
299 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import { DocsV1Read } from "@fern-api/fdr-sdk"; | ||
import { ReactElement } from "react"; | ||
import { useInitializePosthog } from "./posthog"; | ||
import { | ||
capturePosthogEventCustomer, | ||
capturePosthogEventInternal, | ||
useInitializePosthog, | ||
} from "./posthog"; | ||
import { useSafeListenTrackEvents } from "./use-track"; | ||
|
||
export function Posthog(props: { | ||
customerConfig?: DocsV1Read.PostHogConfig; | ||
}): ReactElement { | ||
useInitializePosthog(props.customerConfig); | ||
useSafeListenTrackEvents(({ event, properties }) => { | ||
capturePosthogEventCustomer(event, properties); | ||
}); | ||
useSafeListenTrackEvents(({ event, properties }) => { | ||
capturePosthogEventInternal(event, properties); | ||
}, true); | ||
return <></>; | ||
} |
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 @@ | ||
export const TRACK_EVENT_NAME = "fern-docs-track-analytics"; |
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,70 @@ | ||
import Head from "next/head"; | ||
import Script from "next/script"; | ||
import { ReactNode, useEffect } from "react"; | ||
import { useSafeListenTrackEvents } from "./use-track"; | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style | ||
interface Window { | ||
[key: string]: any; | ||
} | ||
} | ||
|
||
type GAParams = { | ||
gaId: string; | ||
dataLayerName?: string; | ||
debugMode?: boolean; | ||
nonce?: string; | ||
}; | ||
|
||
export default function GoogleAnalytics(props: GAParams): ReactNode { | ||
const { gaId, debugMode, dataLayerName = "dataLayer", nonce } = props; | ||
useEffect(() => { | ||
// performance.mark is being used as a feature use signal. While it is traditionally used for performance | ||
// benchmarking it is low overhead and thus considered safe to use in production and it is a widely available | ||
// existing API. | ||
// The performance measurement will be handled by Chrome Aurora | ||
|
||
performance.mark("mark_feature_usage", { | ||
detail: { | ||
feature: "fern-analytics-ga", | ||
}, | ||
}); | ||
}, []); | ||
|
||
useSafeListenTrackEvents(({ event, properties }) => { | ||
sendGAEvent(dataLayerName, { event, properties }); | ||
}); | ||
|
||
return ( | ||
<> | ||
<Head> | ||
<script | ||
key="ga" | ||
id="_fern-ga-init" | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
window['${dataLayerName}'] = window['${dataLayerName}'] || []; | ||
function gtag(){window['${dataLayerName}'].push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${gaId}' ${debugMode ? ",{ 'debug_mode': true }" : ""});`, | ||
}} | ||
nonce={nonce} | ||
/> | ||
</Head> | ||
<Script | ||
id="_fern-ga" | ||
src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`} | ||
nonce={nonce} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
function sendGAEvent(dataLayer: string, ...args: unknown[]): void { | ||
if (window[dataLayer]) { | ||
window[dataLayer].push(...args); | ||
} else { | ||
console.warn(`GA dataLayer ${dataLayer} does not exist`); | ||
} | ||
} |
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,73 @@ | ||
import Head from "next/head"; | ||
import { ReactNode, useEffect } from "react"; | ||
import { useSafeListenTrackEvents } from "./use-track"; | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style | ||
interface Window { | ||
[key: string]: any; | ||
} | ||
} | ||
|
||
type GTMParams = { | ||
gtmId: string; | ||
dataLayerName?: string; | ||
nonce?: string; | ||
}; | ||
|
||
export default function GoogleTagManager(props: GTMParams): ReactNode { | ||
const { gtmId, dataLayerName = "dataLayer", nonce } = props; | ||
|
||
useEffect(() => { | ||
// performance.mark is being used as a feature use signal. While it is traditionally used for performance | ||
// benchmarking it is low overhead and thus considered safe to use in production and it is a widely available | ||
// existing API. | ||
// The performance measurement will be handled by Chrome Aurora | ||
|
||
performance.mark("mark_feature_usage", { | ||
detail: { | ||
feature: "fern-analytics-gtm", | ||
}, | ||
}); | ||
}, []); | ||
|
||
useSafeListenTrackEvents(({ event, properties }) => { | ||
sendGTMEvent(dataLayerName, { event, properties }); | ||
}); | ||
|
||
return ( | ||
<> | ||
<Head> | ||
<script | ||
key="ga" | ||
id="_fern-gtm" | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | ||
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | ||
j=d.createElement(s),dl=l!='${dataLayerName}'?'&l='+l:'';j.async=true;j.src= | ||
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); | ||
})(window,document,'script','${dataLayerName}','${gtmId}'); | ||
`, | ||
}} | ||
nonce={nonce} | ||
/> | ||
</Head> | ||
<noscript nonce={nonce} id="_fern-gtm-noscript"> | ||
<iframe | ||
src={`https://www.googletagmanager.com/ns.html?id=${gtmId}`} | ||
height="0" | ||
width="0" | ||
style={{ display: "none", visibility: "hidden" }} | ||
nonce={nonce} | ||
/> | ||
</noscript> | ||
</> | ||
); | ||
} | ||
|
||
export const sendGTMEvent = (dataLayer: string, data: unknown): void => { | ||
// define dataLayer so we can still queue up events before GTM init | ||
window[dataLayer] = window[dataLayer] || []; | ||
window[dataLayer].push(data); | ||
}; |
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 @@ | ||
export { track, trackInternal } from "./track"; |
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,41 @@ | ||
import { TRACK_EVENT_NAME } from "./constants"; | ||
|
||
/** | ||
* Track an event. | ||
* | ||
* @param event - The event name. | ||
* @param properties - The event properties. | ||
*/ | ||
export function track( | ||
event: string, | ||
properties?: Record<string, unknown> | ||
): void { | ||
if (typeof window === "undefined") { | ||
return; | ||
} | ||
|
||
window.dispatchEvent( | ||
new CustomEvent(TRACK_EVENT_NAME, { detail: { event, properties } }) | ||
); | ||
} | ||
|
||
/** | ||
* Track an event that is only for internal use. | ||
* | ||
* @param event - The event name. | ||
* @param properties - The event properties. | ||
*/ | ||
export function trackInternal( | ||
event: string, | ||
properties?: Record<string, unknown> | ||
): void { | ||
if (typeof window === "undefined") { | ||
return; | ||
} | ||
|
||
window.dispatchEvent( | ||
new CustomEvent(TRACK_EVENT_NAME, { | ||
detail: { event, properties, internal: true }, | ||
}) | ||
); | ||
} |
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,57 @@ | ||
import React from "react"; | ||
import { z } from "zod"; | ||
import { TRACK_EVENT_NAME } from "./constants"; | ||
|
||
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 = React.useRef<(detail: TrackEvent) => void>(cb); | ||
|
||
React.useEffect(() => { | ||
ref.current = cb; | ||
}); | ||
|
||
React.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) { | ||
console.warn("Error emitting track event", error, event); | ||
} | ||
}; | ||
window.addEventListener(TRACK_EVENT_NAME, handler); | ||
return () => window.removeEventListener(TRACK_EVENT_NAME, handler); | ||
}, [allowInternal]); | ||
} |
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
Oops, something went wrong.