Skip to content

Commit

Permalink
bugfix: safer checks on auto metrics (#471)
Browse files Browse the repository at this point in the history
Report in slack:

```
:wave: Hi team!
We are seeing a sentry pop up with Cannot read properties of null (reading 'scrollHeight')
In the source maps it is being traced to StatsigLogger:
../../node_modules/statsig-js/dist/StatsigLogger.js in <anonymous> at line 270:1
This sentry started popping up 4 days ago. We are wondering if potentially anything was updated on Statsig’s side that would cause this Sentry!
```

There was a null safe check here above, but clearly that wasnt
sufficient
  • Loading branch information
tore-statsig authored Apr 1, 2024
1 parent c9b7bd3 commit cb9ee74
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/StatsigLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,26 +358,38 @@ export default class StatsigLogger {

if (typeof window?.addEventListener === 'function' && document?.body) {
let deepestScroll = 0;
let canLogScrollEvent = false;
window.addEventListener('scroll', () => {
const scrollHeight = document.body.scrollHeight || 1;
const scrollDepth = Math.min(
100,
Math.round(
((window.scrollY + window.innerHeight) / scrollHeight) * 100,
),
);
if (scrollDepth > deepestScroll) {
deepestScroll = scrollDepth;
if (document?.body == null) {
return;
}
try {
const scrollHeight = document.body.scrollHeight || 1;
const scrollDepth = Math.min(
100,
Math.round(
((window.scrollY + window.innerHeight) / scrollHeight) * 100,
),
);
if (scrollDepth > deepestScroll) {
deepestScroll = scrollDepth;
}
canLogScrollEvent = true;
} catch (_) {
// best effort for auto metrics
}
});

window.addEventListener('beforeunload', () => {
this.logGenericEvent(
APP_METRICS_SCROLL_DEPTH_EVENT,
user,
deepestScroll,
metadata,
);
if (canLogScrollEvent) {
this.logGenericEvent(
APP_METRICS_SCROLL_DEPTH_EVENT,
user,
deepestScroll,
metadata,
);
}

this.logGenericEvent(
APP_METRICS_SESSION_LENGTH_EVENT,
user,
Expand Down

0 comments on commit cb9ee74

Please sign in to comment.