feat: add analytics

This commit is contained in:
Derrick Hammer 2023-12-27 12:35:31 -05:00
parent 09ecf1b2d3
commit 6c87defd14
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 34 additions and 0 deletions

26
app/lib/analytics.ts Normal file
View File

@ -0,0 +1,26 @@
const initTracking = (): void => {
const _paq: any[] = ((window as any)._paq = (window as any)._paq || []);
const scriptSource: string =
process.env.TRACKING_SCRIPT_URL || "//piwiki.lumeweb.com/";
const siteId: string = process.env.TRACKING_SITE_ID || "4";
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);
_paq.push(["setTrackerUrl", scriptSource + "matomo.php"]);
_paq.push(["setSiteId", siteId]);
const d: Document = document;
const g: HTMLScriptElement = d.createElement("script");
const s: HTMLScriptElement = d.getElementsByTagName(
"script"
)[0] as HTMLScriptElement;
g.async = true;
g.src = scriptSource + "matomo.js";
s.parentNode?.insertBefore(g, s);
g.onerror = () => {
console.error("Error loading the tracking script.");
};
};
export default initTracking;

View File

@ -13,6 +13,8 @@ import "../styles/global.scss";
import { cssBundleHref } from "@remix-run/css-bundle"; // Adjust the import path as needed
import "unfonts.css";
import initTracking from "@/lib/analytics.js";
import { useEffect } from "react";
export const links: LinksFunction = () => [
...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []),
@ -26,6 +28,12 @@ export const meta: MetaFunction = () => [
];
export default function Root() {
useEffect(() => {
if (process.env.NODE_ENV === "production") {
initTracking();
}
}, []);
return (
<html lang="en">
<head>