Skip to content

Commit

Permalink
add google analytics (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored May 3, 2024
1 parent 869796d commit abd5aef
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/ui/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@fern-ui/loadable": "workspace:*",
"@fern-ui/react-commons": "workspace:*",
"@headlessui/react": "^1.7.18",
"@next/third-parties": "^14.2.3",
"@radix-ui/colors": "^3.0.0",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-checkbox": "^1.0.4",
Expand Down
47 changes: 47 additions & 0 deletions packages/ui/app/src/analytics/CustomerAnalytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { GoogleAnalytics, GoogleTagManager } from "@next/third-parties/google";
import { ReactElement } from "react";

interface Analytics {
ga4?: {
id: string;
dataLayerName?: string;
};
gtm?: {
id: string;
dataLayer?: string[];
dataLayerName?: string;
auth?: string;
preview?: string;
};
}

const CUSTOMER_ANALYTICS: Record<string, Analytics> = {
devrev: {
gtm: {
id: "GTM-P859DNW4",
},
},
};

export function CustomerAnalytics({ domain }: { domain: string }): ReactElement | null {
const analytics = Object.entries(CUSTOMER_ANALYTICS).find(([key]) => domain.includes(key))?.[1];

if (analytics == null) {
return null;
}

return (
<>
{analytics.ga4 && <GoogleAnalytics gaId={analytics.ga4.id} dataLayerName={analytics.ga4.dataLayerName} />}
{analytics.gtm && (
<GoogleTagManager
gtmId={analytics.gtm.id}
dataLayer={analytics.gtm.dataLayer}
dataLayerName={analytics.gtm.dataLayerName}
auth={analytics.gtm.auth}
preview={analytics.gtm.preview}
/>
)}
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useTheme } from "next-themes";
import Head from "next/head";
import Script from "next/script";
import { PropsWithChildren, ReactNode, useCallback, useMemo } from "react";
import { CustomerAnalytics } from "../../analytics/CustomerAnalytics";
import { DocsPage } from "../../next-app/DocsPage";
import { getThemeColor } from "../../next-app/utils/getColorVariables";
import { getFontExtension } from "../../next-app/utils/getFontVariables";
Expand Down Expand Up @@ -134,6 +135,8 @@ export const DocsContextProvider: React.FC<DocsContextProvider.Props> = ({ child
/>
))}
{js?.remote?.map((remote) => <Script key={remote.url} src={remote.url} strategy={remote.strategy} />)}

<CustomerAnalytics domain={baseUrl.domain} />
</DocsContext.Provider>
);
};
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit abd5aef

Please sign in to comment.