Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 0c6e597

Browse files
authored
Merge pull request #5376 from matrix-org/t3chguy/countly
Fix countly method bindings and errors
2 parents e391d5a + 7b625db commit 0c6e597

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/CountlyAnalytics.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,18 @@ const getRoomStats = (roomId: string) => {
343343
}
344344
}
345345

346+
// async wrapper for regex-powered String.prototype.replace
347+
const strReplaceAsync = async (str: string, regex: RegExp, fn: (...args: string[]) => Promise<string>) => {
348+
const promises: Promise<string>[] = [];
349+
// dry-run to calculate the replace values
350+
str.replace(regex, (...args: string[]) => {
351+
promises.push(fn(...args));
352+
return "";
353+
});
354+
const values = await Promise.all(promises);
355+
return str.replace(regex, () => values.shift());
356+
};
357+
346358
export default class CountlyAnalytics {
347359
private baseUrl: URL = null;
348360
private appKey: string = null;
@@ -495,7 +507,7 @@ export default class CountlyAnalytics {
495507
return this.lastMsTs;
496508
}
497509

498-
public recordError(err: Error | string, fatal = false) {
510+
public async recordError(err: Error | string, fatal = false) {
499511
if (this.disabled || this.anonymous) return;
500512

501513
let error = "";
@@ -523,6 +535,11 @@ export default class CountlyAnalytics {
523535
error = err + "";
524536
}
525537

538+
// sanitize the error from identifiers
539+
error = await strReplaceAsync(error, /([!@+#]).+?:[\w:.]+/g, async (substring: string, glyph: string) => {
540+
return glyph + await hashHex(substring.substring(1));
541+
});
542+
526543
const metrics = this.getMetrics();
527544
const ob: ICrash = {
528545
_resolution: metrics?._resolution,
@@ -666,11 +683,11 @@ export default class CountlyAnalytics {
666683
return window.innerWidth > window.innerHeight ? Orientation.Landscape : Orientation.Portrait;
667684
};
668685

669-
private reportOrientation() {
686+
private reportOrientation = () => {
670687
this.track<IOrientationEvent>("[CLY]_orientation", {
671688
mode: this.getOrientation(),
672689
});
673-
}
690+
};
674691

675692
private startTime() {
676693
if (!this.trackTime) {
@@ -754,7 +771,7 @@ export default class CountlyAnalytics {
754771
}
755772
}
756773

757-
private endSession() {
774+
private endSession = () => {
758775
if (this.sessionStarted) {
759776
window.removeEventListener("resize", this.reportOrientation)
760777

@@ -765,7 +782,7 @@ export default class CountlyAnalytics {
765782
});
766783
}
767784
this.sessionStarted = false;
768-
}
785+
};
769786

770787
private onVisibilityChange = () => {
771788
if (document.hidden) {

0 commit comments

Comments
 (0)