diff --git a/public/index.html b/public/index.html index c425b5fcf..fb64b1bd2 100644 --- a/public/index.html +++ b/public/index.html @@ -1,19 +1,20 @@ - + - - - - - + + + + + Washington Opioid Clinical Assessment - +
diff --git a/src/components/Landing/index.js b/src/components/Landing/index.js index 75ce6e6d1..64d184904 100644 --- a/src/components/Landing/index.js +++ b/src/components/Landing/index.js @@ -6,6 +6,7 @@ import executeElm from "../../utils/executeELM"; import * as landingUtils from "./utility"; import { datishFormat } from "../../helpers/formatit"; import { + addMatomoTracking, getEnvSystemType, getEPICPatientIdFromSource, getPatientNameFromSource, @@ -98,6 +99,8 @@ export default class Landing extends Component { }); return; } + // add PIWIK tracking + addMatomoTracking(); writeToLog("application loaded", "info", this.getPatientLogParams()); //set FHIR results let result = {}; diff --git a/src/helpers/utility.js b/src/helpers/utility.js index 402afb00e..5258675dd 100644 --- a/src/helpers/utility.js +++ b/src/helpers/utility.js @@ -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 @@ -707,3 +708,45 @@ export function dedupArrObjects(arr, key) { return acc; }, []); } + +export function getMatomoTrackingSiteId() { + 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 addMatomoTracking() { + // already generated script, return + if (document.querySelector("#matomoScript")) return; + const userId = getUserIdFromAccessToken(); + // no user Id return + if (!userId) return; + const siteId = getMatomoTrackingSiteId(); + // 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); +} +