2021-08-28 22:14:18 +00:00
|
|
|
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);
|
2021-08-29 23:13:06 +00:00
|
|
|
// eslint-disable-next-line
|
2021-08-28 22:14:18 +00:00
|
|
|
}, [siteId]);
|
|
|
|
};
|