production analytics with panel bear

This commit is contained in:
m5r 2021-08-29 06:14:18 +08:00
parent 93e71d3f59
commit ba876d0205
5 changed files with 66 additions and 24123 deletions

View File

@ -0,0 +1,21 @@
import { useEffect } from "react";
import { useRouter } from "next/router";
import * as Panelbear from "@panelbear/panelbear-js";
import type { PanelbearConfig } from "@panelbear/panelbear-js";
export const usePanelbear = (siteId?: string, config: PanelbearConfig = {}) => {
const router = useRouter();
useEffect(() => {
if (!siteId) {
return;
}
Panelbear.load(siteId, { scriptSrc: "/bear.js", ...config });
Panelbear.trackPageview();
const handleRouteChange = () => Panelbear.trackPageview();
router.events.on("routeChangeComplete", handleRouteChange);
return () => router.events.off("routeChangeComplete", handleRouteChange);
}, [siteId]);
};

View File

@ -7,13 +7,19 @@ import {
AuthorizationError, AuthorizationError,
ErrorFallbackProps, ErrorFallbackProps,
useQueryErrorResetBoundary, useQueryErrorResetBoundary,
getConfig,
} from "blitz"; } from "blitz";
import LoginForm from "../auth/components/login-form"; import LoginForm from "../auth/components/login-form";
import { usePanelbear } from "../core/hooks/use-panelbear";
import "app/core/styles/index.css"; import "app/core/styles/index.css";
const { publicRuntimeConfig } = getConfig();
export default function App({ Component, pageProps }: AppProps) { export default function App({ Component, pageProps }: AppProps) {
usePanelbear(publicRuntimeConfig.panelBear.siteId);
const getLayout = Component.getLayout || ((page) => page); const getLayout = Component.getLayout || ((page) => page);
return ( return (

View File

@ -19,6 +19,14 @@ type Module = Omit<NodeModule, "exports"> & { exports: BlitzConfig };
}, },
]; ];
}, },
async rewrites() {
return [
{
source: "/bear.js",
destination: "https://cdn.panelbear.com/analytics.js",
},
];
},
middleware: [ middleware: [
sessionMiddleware({ sessionMiddleware({
cookiePrefix: "shellphone", cookiePrefix: "shellphone",
@ -59,6 +67,9 @@ type Module = Omit<NodeModule, "exports"> & { exports: BlitzConfig };
webPush: { webPush: {
publicKey: process.env.WEB_PUSH_VAPID_PUBLIC_KEY, publicKey: process.env.WEB_PUSH_VAPID_PUBLIC_KEY,
}, },
panelBear: {
siteId: process.env.PANELBEAR_SITE_ID,
},
}, },
/* Uncomment this to customize the webpack config /* Uncomment this to customize the webpack config
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => { webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {

24150
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -46,6 +46,7 @@
"@headlessui/react": "1.4.0", "@headlessui/react": "1.4.0",
"@heroicons/react": "1.0.4", "@heroicons/react": "1.0.4",
"@hookform/resolvers": "2.8.0", "@hookform/resolvers": "2.8.0",
"@panelbear/panelbear-js": "1.2.0",
"@prisma/client": "2.30.0", "@prisma/client": "2.30.0",
"@react-aria/interactions": "3.5.1", "@react-aria/interactions": "3.5.1",
"@tailwindcss/forms": "0.3.3", "@tailwindcss/forms": "0.3.3",