Skip to content

Commit

Permalink
fix: ga metrics not showing up in relatime user dashboards
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Feb 10, 2024
1 parent de87c83 commit 5f5485d
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/desktop-metrics.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@
<meta name="robots" content="noindex, nofollow">
<title>Desktop Metrics logger with Google Analytics</title>
<script>
let gaReady = false;
function installGoogleAnalytics(analyticsID) {
if(!analyticsID || gaReady){
const SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY = "SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY";
let gaReady = false, loadedGoogleAnalyticsID;

const url = window.location.href;
const urlParams = new URLSearchParams(window.location.search);
const reloadedOnce = urlParams.get('reloadedOnce');
function installGoogleAnalytics() {
if(gaReady){
return ;
}
const analyticsID = localStorage.getItem(SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY);
if(!analyticsID) {
console.error("No analytics id found to init ga, waiting for phcode to send ids to init...");
return;
}
loadedGoogleAnalyticsID = analyticsID;
// ga primer scripts
window.dataLayer = window.dataLayer || [];
window.gtag = function() {
Expand All @@ -29,10 +40,24 @@
gaReady = true;
}

installGoogleAnalytics();

function reloadOnce() {
// we have to do this as ga seems to miss points in real time analytics dashboard if ga is not inited
// at page load time. So we reload once to make metrics show up in ga real time dashboard.
let currentUrl = new URL(window.location);
if (!currentUrl.searchParams.has('reloadedOnce')) {
currentUrl.searchParams.set('reloadedOnce', 'true');
window.location.href = currentUrl.href;
}
}


async function processRequest(event) {
const payload = event.payload;
if(!gaReady) {
installGoogleAnalytics(payload.analyticsID);
if(payload.analyticsID && loadedGoogleAnalyticsID !== payload.analyticsID) {
localStorage.setItem(SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY, payload.analyticsID);
installGoogleAnalytics();
}

for(let eventPayload of payload.events) {
Expand All @@ -42,6 +67,9 @@
'value': eventPayload.count
});
}
if(!reloadedOnce) {
setTimeout(reloadOnce, 1000);
}
}
window.__TAURI__.event.listen("health", processRequest);
setInterval(async ()=>{
Expand Down

0 comments on commit 5f5485d

Please sign in to comment.