Skip to content

Commit 5360d12

Browse files
achen2401Amy ChenAmy Chen
authored
add matomo tracking (#148)
* add matomo tracking * fix based on feedback * use user info from access token * add comment * fix typo per feedback * fix typo per feedback --------- Co-authored-by: Amy Chen <[email protected]> Co-authored-by: Amy Chen <[email protected]>
1 parent b75f3fc commit 5360d12

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

public/index.html

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="utf-8">
4+
<meta charset="utf-8" />
55
<!-- Use Internet Explorer Edge Standards mode -->
6-
<meta http-equiv="x-ua-compatible" content="IE=edge" >
7-
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
8-
<meta name="theme-color" content="#000000">
9-
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
10-
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
6+
<meta http-equiv="x-ua-compatible" content="IE=edge" />
7+
<meta
8+
name="viewport"
9+
content="width=device-width, initial-scale=1, shrink-to-fit=no"
10+
/>
11+
<meta name="theme-color" content="#000000" />
12+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
13+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
1114
<title>Washington Opioid Clinical Assessment</title>
1215
</head>
1316
<body>
14-
<noscript>
15-
You need to enable JavaScript to run this app.
16-
</noscript>
17+
<noscript> You need to enable JavaScript to run this app. </noscript>
1718
<div id="root"></div>
1819
</body>
1920
</html>

src/components/Landing/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import executeElm from "../../utils/executeELM";
66
import * as landingUtils from "./utility";
77
import { datishFormat } from "../../helpers/formatit";
88
import {
9+
addMatomoTracking,
910
getEnvSystemType,
1011
getEPICPatientIdFromSource,
1112
getPatientNameFromSource,
@@ -98,6 +99,8 @@ export default class Landing extends Component {
9899
});
99100
return;
100101
}
102+
// add PIWIK tracking
103+
addMatomoTracking();
101104
writeToLog("application loaded", "info", this.getPatientLogParams());
102105
//set FHIR results
103106
let result = {};

src/helpers/utility.js

+43
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import moment from "moment";
22
import { toBlob, toJpeg } from "html-to-image";
33
import { getEnv, ENV_VAR_PREFIX } from "../utils/envConfig";
44
import reportSummarySections from "../config/report_config";
5+
import { getTokenInfoFromStorage } from "./timeout";
56

67
/*
78
* return number of days between two dates
@@ -707,3 +708,45 @@ export function dedupArrObjects(arr, key) {
707708
return acc;
708709
}, []);
709710
}
711+
712+
export function getMatomoTrackingSiteId() {
713+
return getEnv(`${ENV_VAR_PREFIX}_MATOMO_SITE_ID`);
714+
}
715+
716+
export function getUserIdFromAccessToken() {
717+
const accessToken = getTokenInfoFromStorage();
718+
if (!accessToken) return null;
719+
if (accessToken.profile) return accessToken.profile;
720+
if (accessToken.fhirUser) return accessToken.fhirUser;
721+
return accessToken["preferred_username"];
722+
}
723+
724+
export function addMatomoTracking() {
725+
// already generated script, return
726+
if (document.querySelector("#matomoScript")) return;
727+
const userId = getUserIdFromAccessToken();
728+
// no user Id return
729+
if (!userId) return;
730+
const siteId = getMatomoTrackingSiteId();
731+
// no site Id return
732+
if (!siteId) return;
733+
// init global piwik tracking object
734+
window._paq = [];
735+
window._paq.push(["trackPageView"]);
736+
window._paq.push(["enableLinkTracking"]);
737+
window._paq.push(["setSiteId", siteId]);
738+
window._paq.push(["setUserId", userId]);
739+
740+
let u = "https://piwik.cirg.washington.edu/";
741+
window._paq.push(["setTrackerUrl", u + "matomo.php"]);
742+
let d = document,
743+
g = d.createElement("script"),
744+
headElement = document.querySelector("head");
745+
g.type = "text/javascript";
746+
g.async = true;
747+
g.defer = true;
748+
g.setAttribute("src", u + "matomo.js");
749+
g.setAttribute("id", "matomoScript");
750+
headElement.appendChild(g);
751+
}
752+

0 commit comments

Comments
 (0)