Skip to content

Commit

Permalink
add analytics to docs definition (#1250)
Browse files Browse the repository at this point in the history
  • Loading branch information
abarrell authored Aug 8, 2024
1 parent 80fdece commit ff8e48d
Show file tree
Hide file tree
Showing 29 changed files with 88 additions and 9 deletions.
8 changes: 8 additions & 0 deletions fern/apis/fdr/definition/docs/v1/commons/commons.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ types:
- ts
- js

AnalyticsConfig:
properties:
segment: optional<SegmentConfig>

SegmentConfig:
properties:
writeKey: string

DocsLayoutConfig:
properties:
pageWidth: optional<PageWidthSizeConfig>
Expand Down
2 changes: 2 additions & 0 deletions fern/apis/fdr/definition/docs/v1/db/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ types:
title: optional<string>
defaultLanguage: optional<commons.ProgrammingLanguage>

analyticsConfig: optional<commons.AnalyticsConfig>

# navigation
navigation: NavigationConfig
navbarLinks: optional<list<commons.NavbarLink>>
Expand Down
2 changes: 2 additions & 0 deletions fern/apis/fdr/definition/docs/v1/read/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ types:
title: optional<string>
defaultLanguage: optional<commons.ProgrammingLanguage>

analyticsConfig: optional<commons.AnalyticsConfig>

# navigation
navigation: NavigationConfig
navbarLinks: optional<list<commons.NavbarLink>>
Expand Down
2 changes: 2 additions & 0 deletions fern/apis/fdr/definition/docs/v1/write/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ types:
title: optional<string>
defaultLanguage: optional<commons.ProgrammingLanguage>

analyticsConfig: optional<commons.AnalyticsConfig>

# navigation
navigation: NavigationConfig
navbarLinks: optional<list<commons.NavbarLink>>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export function convertDocsDefinitionToDb({
redirects: writeShape.config.redirects,
integrations: writeShape.config.integrations,
defaultLanguage: writeShape.config.defaultLanguage,
analyticsConfig: writeShape.config.analyticsConfig,
},
pages: writeShape.pages,
jsFiles: writeShape.jsFiles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function convertDbDocsConfigToRead({
redirects: dbShape.redirects,
integrations: dbShape.integrations,
defaultLanguage: dbShape.defaultLanguage,
analyticsConfig: dbShape.analyticsConfig,
};
}

Expand Down
14 changes: 13 additions & 1 deletion packages/ui/app/src/analytics/CustomerAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ import { GoogleAnalytics } from "@next/third-parties/google";
import { useAtomValue } from "jotai";
import { selectAtom } from "jotai/utils";
import { isEqual } from "lodash-es";
import Script from "next/script";
import { ReactElement, memo } from "react";
import { DOCS_ATOM } from "../atoms";
import { DOCS_ATOM, DOMAIN_ATOM } from "../atoms";
import { GoogleTagManager } from "./GoogleTagManager";
import { renderSegmentSnippet } from "./segment";

const ANALYTICS_ATOM = selectAtom(DOCS_ATOM, (docs) => docs.analytics ?? {}, isEqual);
const ANALYTICS_CONFIG_ATOM = selectAtom(DOCS_ATOM, (docs) => docs.analyticsConfig ?? {}, isEqual);

export const CustomerAnalytics = memo(function CustomerAnalytics(): ReactElement | null {
const domain = useAtomValue(DOMAIN_ATOM);
const { ga4, gtm } = useAtomValue(ANALYTICS_ATOM);
const { segment } = useAtomValue(ANALYTICS_CONFIG_ATOM);
return (
<>
{/* renders either segment with our write key or segment with the customer's write key */}
<Script
id="segment-script"
dangerouslySetInnerHTML={{
__html: renderSegmentSnippet(domain, segment?.writeKey),
}}
/>
{ga4 != null && <GoogleAnalytics gaId={ga4.measurementId} />}
{gtm != null && <GoogleTagManager {...gtm} />}
</>
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/app/src/analytics/segment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as snippet from "@segment/snippet";

const DOMAINS_TO_SKIP = ["privategpt.docs.buildwithfern.com"];

export function renderSegmentSnippet(domain: string): string {
export function renderSegmentSnippet(domain: string, customSegmentWriteKey?: string): string {
const apiKey = process.env.NEXT_PUBLIC_SEGMENT_API_KEY?.trim();
const opts = {
apiKey,
apiKey: customSegmentWriteKey ? customSegmentWriteKey : apiKey,
page: true,
};
// Skip rendering the snippet for certain domains
Expand Down
1 change: 1 addition & 0 deletions packages/ui/app/src/atoms/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const EMPTY_DOCS_STATE: DocsProps = {
theme: "default",
user: undefined,
defaultLang: "curl",
analyticsConfig: {},
};

export const DOCS_ATOM = atomWithReducer<DocsProps, DocsProps>(EMPTY_DOCS_STATE, (_, next) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/app/src/atoms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export interface DocsProps {
featureFlags: FeatureFlags;
apis: FdrAPI.ApiDefinitionId[];
seo: NextSeoProps;
analytics: CustomerAnalytics | undefined;
analytics: CustomerAnalytics | undefined; // deprecated
analyticsConfig: DocsV1Read.AnalyticsConfig | undefined;
fallback: Record<string, any>;
theme: FernTheme;
user: FernUser | undefined;
Expand Down
5 changes: 1 addition & 4 deletions packages/ui/app/src/next-app/utils/JavascriptProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import Head from "next/head";
import Script from "next/script";
import { memo } from "react";
import { CustomerAnalytics } from "../../analytics/CustomerAnalytics";
import { renderSegmentSnippet } from "../../analytics/segment";
import { DOCS_ATOM, DOMAIN_ATOM, FILES_ATOM } from "../../atoms";
import { DOCS_ATOM, FILES_ATOM } from "../../atoms";

const JS_ATOM = atom((get) => get(DOCS_ATOM).js);

export const JavascriptProvider = memo(() => {
const files = useAtomValue(FILES_ATOM);
const js = useAtomValue(JS_ATOM);
const domain = useAtomValue(DOMAIN_ATOM);

return (
<>
Expand All @@ -37,7 +35,6 @@ export const JavascriptProvider = memo(() => {
/>
))}
{js?.remote?.map((remote) => <Script key={remote.url} src={remote.url} strategy={remote.strategy} />)}
<Script id="segment-script" dangerouslySetInnerHTML={{ __html: renderSegmentSnippet(domain) }} />
<CustomerAnalytics />
</>
);
Expand Down
1 change: 1 addition & 0 deletions packages/ui/docs-bundle/src/utils/getDocsPageProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ async function convertDocsToDocsPageProps({
fallback: {},
analytics: await getCustomerAnalytics(docs.baseUrl.domain, docs.baseUrl.basePath),
theme: docs.baseUrl.domain.includes("cohere") ? "cohere" : "default",
analyticsConfig: docs.definition.config.analyticsConfig,
defaultLang: docs.definition.config.defaultLanguage ?? "curl",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export async function getDocsPageProps(
),
fallback: {},
analytics: undefined,
analyticsConfig: docs.definition.config.analyticsConfig,
theme: docs.baseUrl.domain.includes("cohere") ? "cohere" : "default",
user: undefined,
defaultLang: docs.definition.config.defaultLanguage ?? "curl",
Expand Down
2 changes: 1 addition & 1 deletion servers/fdr/fern/fern.config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"organization": "fern",
"version": "0.16.36"
"version": "0.37.14"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ff8e48d

Please sign in to comment.