From 83682cdb0dcc5bfba11efca20a26983d57a1e438 Mon Sep 17 00:00:00 2001 From: LJ Date: Sun, 9 Mar 2025 12:44:54 -0700 Subject: [PATCH 1/2] Load posthog and mixpanel API key from env instead of hardcoded. --- docs/docusaurus.config.ts | 32 ++++++++++++++++++++++++-------- docs/src/theme/Root.js | 7 ++++--- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/docs/docusaurus.config.ts b/docs/docusaurus.config.ts index ec0768e..a7212bd 100644 --- a/docs/docusaurus.config.ts +++ b/docs/docusaurus.config.ts @@ -1,3 +1,4 @@ +import webpack from 'webpack'; import { themes as prismThemes } from 'prism-react-renderer'; import type { Config } from '@docusaurus/types'; import type * as Preset from '@docusaurus/preset-classic'; @@ -33,14 +34,17 @@ const config: Config = { }, plugins: [ - [ - "posthog-docusaurus", - { - apiKey: "phc_SgKiQafwZjHu4jQW2q402gbz6FYQ2NJRkcgooZMNNcy", - appUrl: "https://us.i.posthog.com", - enableInDevelopment: false, - }, - ], + () => ({ + name: 'load-env-vars', + configureWebpack: () => ({ + mergeStrategy: { plugins: "append", resolve: "merge" }, + plugins: [ + new webpack.DefinePlugin({ + 'process.env.COCOINDEX_DOCS_MIXPANEL_API_KEY': JSON.stringify(process.env.COCOINDEX_DOCS_MIXPANEL_API_KEY), + }) + ], + }), + }), ], presets: [ @@ -147,4 +151,16 @@ const config: Config = { } satisfies Preset.ThemeConfig, }; + +if (!!process.env.COCOINDEX_DOCS_POSTHOG_API_KEY) { + config.plugins.push([ + "posthog-docusaurus", + { + apiKey: process.env.COCOINDEX_DOCS_POSTHOG_API_KEY, + appUrl: "https://us.i.posthog.com", + enableInDevelopment: false, + }, + ]); +} + export default config; diff --git a/docs/src/theme/Root.js b/docs/src/theme/Root.js index d320314..bfe0847 100644 --- a/docs/src/theme/Root.js +++ b/docs/src/theme/Root.js @@ -2,11 +2,12 @@ import React from 'react'; import mixpanel from 'mixpanel-browser'; // Default implementation, that you can customize -export default function Root({children}) { +export default function Root({ children }) { React.useEffect(() => { - if (typeof window !== 'undefined') { + const mixpanelApiKey = process.env.COCOINDEX_DOCS_MIXPANEL_API_KEY; + if (typeof window !== 'undefined' && !!mixpanelApiKey) { // Initialize Mixpanel with the token - mixpanel.init('46addeb6bedf8684a445aced6e67c76e', { + mixpanel.init(mixpanelApiKey, { track_pageview: true, debug: process.env.NODE_ENV === 'development' }); From f6545b4997a43f53ae0ea516e1c663ffd43d2705 Mon Sep 17 00:00:00 2001 From: LJ Date: Sun, 9 Mar 2025 12:49:27 -0700 Subject: [PATCH 2/2] Set posthog and mixpanel API keys during docs deployment. --- .github/workflows/docs.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 1458117..674ffbd 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -61,6 +61,8 @@ jobs: env: USE_SSH: true run: | + export COCOINDEX_DOCS_POSTHOG_API_KEY=${{ secrets.COCOINDEX_DOCS_POSTHOG_API_KEY }} + export COCOINDEX_DOCS_MIXPANEL_API_KEY=${{ secrets.COCOINDEX_DOCS_MIXPANEL_API_KEY }} git config --global user.email "cocoindex.io@gmail.com" git config --global user.name "CocoIndex" yarn install --frozen-lockfile