Add analytics integration with Horchposten backend
Implements session-based analytics tracking that sends gameplay
stats to the Horchposten API. Adds stats.{c,h} for accumulating
per-session metrics (kills, deaths, shots, dashes, jumps, pickups,
damage, time) and analytics.{c,h} with EM_JS bridge for fetch()
calls to the backend. Client ID is persisted in localStorage.
Session start/end hooks are wired into all game lifecycle events
(level transitions, restart, quit, tab close via sendBeacon).
Analytics URL/key are configured via data attributes on the canvas
container. Non-WASM builds compile with no-op stubs.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -107,7 +107,9 @@
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="canvas-container">
|
||||
<div id="canvas-container"
|
||||
data-analytics-url=""
|
||||
data-analytics-key="">
|
||||
<canvas class="emscripten" id="canvas" tabindex="1"
|
||||
width="640" height="360"></canvas>
|
||||
</div>
|
||||
@@ -282,6 +284,27 @@
|
||||
document.title = 'Jump \'n Run - Level Editor';
|
||||
}
|
||||
}
|
||||
|
||||
/* ── Analytics: end session on tab close ────────────── */
|
||||
window.addEventListener('beforeunload', function() {
|
||||
if (typeof Module !== 'undefined' && Module._analyticsSessionId &&
|
||||
Module._analyticsUrl) {
|
||||
/* Use sendBeacon for reliability during page unload */
|
||||
var sid = Module._analyticsSessionId;
|
||||
var body = JSON.stringify({
|
||||
score: 0,
|
||||
level_reached: 1,
|
||||
lives_used: 0,
|
||||
duration_seconds: 0,
|
||||
end_reason: 'quit'
|
||||
});
|
||||
var blob = new Blob([body], { type: 'application/json' });
|
||||
navigator.sendBeacon(
|
||||
Module._analyticsUrl + '/api/analytics/session/' + sid + '/end/',
|
||||
blob
|
||||
);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{{{ SCRIPT }}}
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user