Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add matomo tracking #148

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta charset="utf-8" />
<!-- Use Internet Explorer Edge Standards mode -->
<meta http-equiv="x-ua-compatible" content="IE=edge" >
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<meta http-equiv="x-ua-compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<title>Washington Opioid Clinical Assessment</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<noscript> You need to enable JavaScript to run this app. </noscript>
<div id="root"></div>
</body>
</html>
3 changes: 3 additions & 0 deletions src/components/Landing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import executeElm from "../../utils/executeELM";
import * as landingUtils from "./utility";
import { datishFormat } from "../../helpers/formatit";
import {
addMamotoTracking,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@achen2401 it's Matomo, not Mamoto - can you please edit so it's easier to find :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you @mcjustin for noticing the typo! I fixed it now.

getEnvSystemType,
getEPICPatientIdFromSource,
getPatientNameFromSource,
Expand Down Expand Up @@ -98,6 +99,8 @@ export default class Landing extends Component {
});
return;
}
// add PIWIK tracking
addMamotoTracking();
writeToLog("application loaded", "info", this.getPatientLogParams());
//set FHIR results
let result = {};
Expand Down
43 changes: 43 additions & 0 deletions src/helpers/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from "moment";
import { toBlob, toJpeg } from "html-to-image";
import { getEnv, ENV_VAR_PREFIX } from "../utils/envConfig";
import reportSummarySections from "../config/report_config";
import { getTokenInfoFromStorage } from "./timeout";

/*
* return number of days between two dates
Expand Down Expand Up @@ -707,3 +708,45 @@ export function dedupArrObjects(arr, key) {
return acc;
}, []);
}

export function getMamotoTrackingSiteId() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@achen2401 another Mamoto gremlin here :) - and a call to it below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you @mcjustin! This should be fixed too.

return getEnv(`${ENV_VAR_PREFIX}_MATOMO_SITE_ID`);
}

export function getUserIdFromAccessToken() {
const accessToken = getTokenInfoFromStorage();
if (!accessToken) return null;
if (accessToken.profile) return accessToken.profile;
if (accessToken.fhirUser) return accessToken.fhirUser;
return accessToken["preferred_username"];
}

export function addMamotoTracking() {
// already generated script, return
if (document.querySelector("#matomoScript")) return;
const userId = getUserIdFromAccessToken();
// no user Id return
if (!userId) return;
const siteId = getMamotoTrackingSiteId();
// no site Id return
if (!siteId) return;
// init global piwik tracking object
window._paq = [];
window._paq.push(["trackPageView"]);
window._paq.push(["enableLinkTracking"]);
window._paq.push(["setSiteId", siteId]);
window._paq.push(["setUserId", userId]);

let u = "https://piwik.cirg.washington.edu/";
window._paq.push(["setTrackerUrl", u + "matomo.php"]);
let d = document,
g = d.createElement("script"),
headElement = document.querySelector("head");
g.type = "text/javascript";
g.async = true;
g.defer = true;
g.setAttribute("src", u + "matomo.js");
g.setAttribute("id", "matomoScript");
headElement.appendChild(g);
}

Loading