Skip to content

Commit

Permalink
Implement Matomo tracking
Browse files Browse the repository at this point in the history
- Track navigation to external websites
- Track navigation to interal pages
- Track changing of district type

closes #38
  • Loading branch information
dhochbaum-dcp authored and TangoYankee committed Sep 23, 2024
1 parent 13bc332 commit bce9648
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ node_modules
/blob-report/
/playwright/.cache/
.vscode
.DS_Store
6 changes: 6 additions & 0 deletions app/components/AdminDropdown/DistrictTypeDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AdminDropdownProps, AdminDropdown } from ".";
import { analytics } from "../../utils/analytics";

export interface DistrictTypeDropdownProps
extends Pick<AdminDropdownProps, "selectValue"> {
Expand All @@ -12,6 +13,11 @@ export function DistrictTypeDropdown({
const updateDistrictType = (nextDistrictType: string | null) => {
const nextSearchParams: Record<string, string> =
nextDistrictType === null ? {} : { districtType: nextDistrictType };
analytics({
category: "Dropdown Menu",
action: "Change District Type",
name: nextDistrictType,
});
updateSearchParams(nextSearchParams);
};

Expand Down
5 changes: 5 additions & 0 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import {
} from "./components/GoToDistrictBtn";
import { GoToCommunityDistrictBtn } from "./components/GoToDistrictBtn/GoToCommunityDistrictBtn";
import { WelcomePanel } from "./components/WelcomePanel";
import { useEffect } from "react";
import { initializeMatomoTagManager } from "./utils/analytics";

export type BoroughId = null | string;
export type DistrictType = null | "cd" | "ccd";
Expand Down Expand Up @@ -142,6 +144,9 @@ function Document({
}

export default function App() {
useEffect(() => {
initializeMatomoTagManager("SmoWWpiD");
}, []);
const navigate = useNavigate();
const { pathname } = useLocation();
const [searchParams, setSearchParams] = useSearchParams();
Expand Down
44 changes: 44 additions & 0 deletions app/utils/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export function analytics(eventData) {
if (eventData.value) {
window._paq.push([
"trackEvent",
eventData.category,
eventData.action,
eventData.name,
eventData.value,
]);
} else {
window._paq.push([
"trackEvent",
eventData.category,
eventData.action,
eventData.name,
]);
}
}

export function initializeMatomoTagManager(containerID) {
// From the initial script they provide for setup
var _mtm = (window._mtm = window._mtm || []);
_mtm.push({ "mtm.startTime": new Date().getTime(), event: "mtm.Start" });
var d = document,
g = d.createElement("script"),
s = d.getElementsByTagName("script")[0];
g.type = "text/javascript";
g.id = "matomo-tag-manager";
g.async = true;
g.src = `https://cdn.matomo.cloud/nycplanning.matomo.cloud/container_${containerID}.js`;
s.parentNode.insertBefore(g, s);

// To track pageviews, watching for changes to location.pathname
let previousUrlPath = location.pathname;
const observer = new MutationObserver(function () {
if (location.pathname !== previousUrlPath) {
previousUrlPath = location.pathname;
window._paq.push(["setCustomUrl", location.href]);
window._paq.push(["trackPageView"]);
}
});
const config = { subtree: true, childList: true };
observer.observe(document, config);
}
5 changes: 5 additions & 0 deletions setup-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Object.defineProperty(window, "scrollTo", {
value: vi.fn(),
});

Object.defineProperty(window, "_paq", {
writable: true,
value: [],
});

/**
* Mock Service Worker can be configured once the Handlers are generated
*/
Expand Down

0 comments on commit bce9648

Please sign in to comment.