Skip to content

Commit cb7aff4

Browse files
authored
Load posthog and mixpanel API key from env / repo secret keys instead of hardcoded. (#79)
* Load posthog and mixpanel API key from env instead of hardcoded. * Set posthog and mixpanel API keys during docs deployment.
1 parent 38e07d1 commit cb7aff4

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

.github/workflows/docs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ jobs:
6161
env:
6262
USE_SSH: true
6363
run: |
64+
export COCOINDEX_DOCS_POSTHOG_API_KEY=${{ secrets.COCOINDEX_DOCS_POSTHOG_API_KEY }}
65+
export COCOINDEX_DOCS_MIXPANEL_API_KEY=${{ secrets.COCOINDEX_DOCS_MIXPANEL_API_KEY }}
6466
git config --global user.email "cocoindex.io@gmail.com"
6567
git config --global user.name "CocoIndex"
6668
yarn install --frozen-lockfile

docs/docusaurus.config.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import webpack from 'webpack';
12
import { themes as prismThemes } from 'prism-react-renderer';
23
import type { Config } from '@docusaurus/types';
34
import type * as Preset from '@docusaurus/preset-classic';
@@ -33,14 +34,17 @@ const config: Config = {
3334
},
3435

3536
plugins: [
36-
[
37-
"posthog-docusaurus",
38-
{
39-
apiKey: "phc_SgKiQafwZjHu4jQW2q402gbz6FYQ2NJRkcgooZMNNcy",
40-
appUrl: "https://us.i.posthog.com",
41-
enableInDevelopment: false,
42-
},
43-
],
37+
() => ({
38+
name: 'load-env-vars',
39+
configureWebpack: () => ({
40+
mergeStrategy: { plugins: "append", resolve: "merge" },
41+
plugins: [
42+
new webpack.DefinePlugin({
43+
'process.env.COCOINDEX_DOCS_MIXPANEL_API_KEY': JSON.stringify(process.env.COCOINDEX_DOCS_MIXPANEL_API_KEY),
44+
})
45+
],
46+
}),
47+
}),
4448
],
4549

4650
presets: [
@@ -147,4 +151,16 @@ const config: Config = {
147151
} satisfies Preset.ThemeConfig,
148152
};
149153

154+
155+
if (!!process.env.COCOINDEX_DOCS_POSTHOG_API_KEY) {
156+
config.plugins.push([
157+
"posthog-docusaurus",
158+
{
159+
apiKey: process.env.COCOINDEX_DOCS_POSTHOG_API_KEY,
160+
appUrl: "https://us.i.posthog.com",
161+
enableInDevelopment: false,
162+
},
163+
]);
164+
}
165+
150166
export default config;

docs/src/theme/Root.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import React from 'react';
22
import mixpanel from 'mixpanel-browser';
33

44
// Default implementation, that you can customize
5-
export default function Root({children}) {
5+
export default function Root({ children }) {
66
React.useEffect(() => {
7-
if (typeof window !== 'undefined') {
7+
const mixpanelApiKey = process.env.COCOINDEX_DOCS_MIXPANEL_API_KEY;
8+
if (typeof window !== 'undefined' && !!mixpanelApiKey) {
89
// Initialize Mixpanel with the token
9-
mixpanel.init('46addeb6bedf8684a445aced6e67c76e', {
10+
mixpanel.init(mixpanelApiKey, {
1011
track_pageview: true,
1112
debug: process.env.NODE_ENV === 'development'
1213
});

0 commit comments

Comments
 (0)