diff --git a/CHANGELOG.md b/CHANGELOG.md index f5816f878..29d1902ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ changes. ### Added -- +- Integrate matomo analytics ### Fixed diff --git a/govtool/frontend/src/App.tsx b/govtool/frontend/src/App.tsx index 553d5295f..7745ceefb 100644 --- a/govtool/frontend/src/App.tsx +++ b/govtool/frontend/src/App.tsx @@ -39,8 +39,10 @@ import { import { PublicRoute } from "./pages/PublicRoute"; import { TopBanners } from "./components/organisms/TopBanners"; import { DashboardHome } from "./pages/DashboardHome"; +import { useMatomo } from "./hooks/useMatomo"; export default () => { + useMatomo(); const { isProposalDiscussionForumEnabled } = useFeatureFlag(); const { enable, isEnabled } = useCardano(); const navigate = useNavigate(); diff --git a/govtool/frontend/src/hooks/useMatomo.ts b/govtool/frontend/src/hooks/useMatomo.ts new file mode 100644 index 000000000..381de7105 --- /dev/null +++ b/govtool/frontend/src/hooks/useMatomo.ts @@ -0,0 +1,31 @@ +/* eslint-disable func-names */ +/* eslint-disable no-multi-assign */ +/* eslint-disable prefer-template */ +/* eslint-disable no-underscore-dangle */ +/* eslint-disable wrap-iife */ +import { useEffect } from "react"; + +export const useMatomo = () => { + useEffect(() => { + const env = import.meta.env.VITE_APP_ENV; + if (env !== "prodction" || env !== "staging") { + return; + } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + const _paq = (window._paq = window._pag || []); + _paq.push(["trackPageView"]); + _paq.push(["enableLinkTracking"]); + (function () { + const u = "//analytics.gov.tools/"; + _paq.push(["setTrackerUrl", u + "matomo.php"]); + _paq.push(["setSiteId", env === "production" ? "1" : "2"]); + const d = document; + const g = d.createElement("script"); + const s = d.getElementsByTagName("script")[0]; + g.async = true; + g.src = u + "matomo.js"; + s.parentNode?.insertBefore(g, s); + })(); + }, []); +};