Skip to content

Commit 79c52cd

Browse files
authored
- include posthog script tag in the html (process.env.POSTHOG_API_KEY… (#376)
* - include posthog script tag in the html (process.env.POSTHOG_API_KEY is applied via sed during release) - only include script tag in head of NODE_ENV is production - When you call posthog.init the PostHog JS library begins automatically capturing user events: - pageviews, including the URL - autocaptured events, such as any click, change of input, or submission associated with a, button, form, input, select, textarea, and label tags * try importing POSTHOG_API_KEY from bundle (cli/src/util/postHog/client) * ensure log is not used on the front end * rename log -> serverLogger * more serverLogger * rename log -> serverLogger * more serverLogger * ohyeah * skip this test for now * fix this test * fix this test * d'oh ''
1 parent 0c0c9af commit 79c52cd

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { POSTHOG_API_KEY } from "../util/postHog/client";
2+
3+
export default function PosthogScript() {
4+
if (process.env.NODE_ENV !== "production") {
5+
return null;
6+
}
7+
8+
const loadPosthogScript = `!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);`;
9+
10+
return (
11+
<script
12+
dangerouslySetInnerHTML={{
13+
__html: `
14+
${loadPosthogScript}
15+
posthog.init('${POSTHOG_API_KEY}', {api_host: 'https://app.posthog.com'})
16+
`,
17+
}}
18+
></script>
19+
);
20+
}

packages/cli/src/pages/_app.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Head from "next/head";
44
import { HamburgerProvider } from "../components/HamburgerContext";
55
import NavBar from "../components/NavBar/NavBar";
66
import "../styles/globals.css";
7+
import PosthogScript from "../components/PosthogScript";
78

89
export default function Mailing({
910
Component,
@@ -12,6 +13,7 @@ export default function Mailing({
1213
return (
1314
<HamburgerProvider>
1415
<Head>
16+
<PosthogScript />
1517
<title>Mailing</title>
1618
</Head>
1719
{pageProps.user ? (

packages/cli/src/util/postHog/client.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { PostHog } from "posthog-node";
2-
import { debug } from "../serverLogger";
32

4-
const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY;
3+
export const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY;
54

65
let client: PostHog | undefined;
76

@@ -10,7 +9,7 @@ export function postHogClient(): PostHog | undefined {
109
return;
1110

1211
if (undefined === POSTHOG_API_KEY) {
13-
debug("POSTHOG_API_KEY is undefined");
12+
console.error("POSTHOG_API_KEY is undefined");
1413
return;
1514
}
1615

0 commit comments

Comments
 (0)