1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-08-09 08:42:50 +03:00

Integrate analytics stubs (#7186)

* Add matrix-analytics-events as a dependency
* Make IEvent look like a stub definition
* Update pageview tracking to track screens, using a hypothetical definition of a screen event
* Remove distinction between pseudo and anon tracking, will need to rework it considering stubs
This commit is contained in:
James Salter
2021-12-06 21:43:42 +11:00
committed by GitHub
parent 684b0617ae
commit 43f264ccfc
7 changed files with 101 additions and 148 deletions

View File

@@ -39,7 +39,7 @@ describe('DecryptionFailureTracker', function() {
const failedDecryptionEvent = createFailedDecryptionEvent();
let count = 0;
const tracker = new DecryptionFailureTracker((total) => count += total);
const tracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
const err = new MockDecryptionError();
tracker.eventDecrypted(failedDecryptionEvent, err);
@@ -59,7 +59,7 @@ describe('DecryptionFailureTracker', function() {
const decryptedEvent = createFailedDecryptionEvent();
const tracker = new DecryptionFailureTracker((total) => {
expect(true).toBe(false, 'should not track an event that has since been decrypted correctly');
});
}, () => "UnknownError");
const err = new MockDecryptionError();
tracker.eventDecrypted(decryptedEvent, err);
@@ -81,7 +81,7 @@ describe('DecryptionFailureTracker', function() {
const decryptedEvent2 = createFailedDecryptionEvent();
let count = 0;
const tracker = new DecryptionFailureTracker((total) => count += total);
const tracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
// Arbitrary number of failed decryptions for both events
const err = new MockDecryptionError();
@@ -112,7 +112,7 @@ describe('DecryptionFailureTracker', function() {
const decryptedEvent = createFailedDecryptionEvent();
let count = 0;
const tracker = new DecryptionFailureTracker((total) => count += total);
const tracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
// Indicate decryption
const err = new MockDecryptionError();
@@ -140,7 +140,7 @@ describe('DecryptionFailureTracker', function() {
const decryptedEvent = createFailedDecryptionEvent();
let count = 0;
const tracker = new DecryptionFailureTracker((total) => count += total);
const tracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
// Indicate decryption
const err = new MockDecryptionError();
@@ -153,7 +153,7 @@ describe('DecryptionFailureTracker', function() {
tracker.trackFailures();
// Simulate the browser refreshing by destroying tracker and creating a new tracker
const secondTracker = new DecryptionFailureTracker((total) => count += total);
const secondTracker = new DecryptionFailureTracker((total) => count += total, () => "UnknownError");
//secondTracker.loadTrackedEventHashMap();
@@ -170,28 +170,29 @@ describe('DecryptionFailureTracker', function() {
const counts = {};
const tracker = new DecryptionFailureTracker(
(total, errorCode) => counts[errorCode] = (counts[errorCode] || 0) + total,
(error) => error === "UnknownError" ? "UnknownError" : "OlmKeysNotSentError",
);
// One failure of ERROR_CODE_1, and effectively two for ERROR_CODE_2
tracker.addDecryptionFailure(new DecryptionFailure('$event_id1', 'ERROR_CODE_1'));
tracker.addDecryptionFailure(new DecryptionFailure('$event_id2', 'ERROR_CODE_2'));
tracker.addDecryptionFailure(new DecryptionFailure('$event_id2', 'ERROR_CODE_2'));
tracker.addDecryptionFailure(new DecryptionFailure('$event_id3', 'ERROR_CODE_2'));
tracker.addDecryptionFailure(new DecryptionFailure('$event_id1', 'UnknownError'));
tracker.addDecryptionFailure(new DecryptionFailure('$event_id2', 'OlmKeysNotSentError'));
tracker.addDecryptionFailure(new DecryptionFailure('$event_id2', 'OlmKeysNotSentError'));
tracker.addDecryptionFailure(new DecryptionFailure('$event_id3', 'OlmKeysNotSentError'));
// Pretend "now" is Infinity
tracker.checkFailures(Infinity);
tracker.trackFailures();
expect(counts['ERROR_CODE_1']).toBe(1, 'should track one ERROR_CODE_1');
expect(counts['ERROR_CODE_2']).toBe(2, 'should track two ERROR_CODE_2');
expect(counts['UnknownError']).toBe(1, 'should track one UnknownError');
expect(counts['OlmKeysNotSentError']).toBe(2, 'should track two OlmKeysNotSentError');
});
it('should map error codes correctly', () => {
const counts = {};
const tracker = new DecryptionFailureTracker(
(total, errorCode) => counts[errorCode] = (counts[errorCode] || 0) + total,
(errorCode) => 'MY_NEW_ERROR_CODE',
(errorCode) => 'OlmUnspecifiedError',
);
// One failure of ERROR_CODE_1, and effectively two for ERROR_CODE_2
@@ -204,7 +205,7 @@ describe('DecryptionFailureTracker', function() {
tracker.trackFailures();
expect(counts['MY_NEW_ERROR_CODE'])
.toBe(3, 'should track three MY_NEW_ERROR_CODE, got ' + counts['MY_NEW_ERROR_CODE']);
expect(counts['OlmUnspecifiedError'])
.toBe(3, 'should track three OlmUnspecifiedError, got ' + counts['OlmUnspecifiedError']);
});
});

View File

@@ -17,9 +17,7 @@ limitations under the License.
import {
Anonymity,
getRedactedCurrentLocation,
IAnonymousEvent,
IPseudonymousEvent,
IRoomEvent,
IEvent,
PosthogAnalytics,
} from '../src/PosthogAnalytics';
@@ -41,25 +39,9 @@ class FakePosthog {
}
}
export interface ITestEvent extends IAnonymousEvent {
key: "jest_test_event";
properties: {
foo: string;
};
}
export interface ITestPseudonymousEvent extends IPseudonymousEvent {
key: "jest_test_pseudo_event";
properties: {
foo: string;
};
}
export interface ITestRoomEvent extends IRoomEvent {
key: "jest_test_room_event";
properties: {
foo: string;
};
export interface ITestEvent extends IEvent {
eventName: "JestTestEvents";
foo: string;
}
describe("PosthogAnalytics", () => {
@@ -127,27 +109,20 @@ describe("PosthogAnalytics", () => {
analytics = new PosthogAnalytics(fakePosthog);
});
it("Should pass trackAnonymousEvent() to posthog", async () => {
it("Should pass event to posthog", () => {
analytics.setAnonymity(Anonymity.Pseudonymous);
await analytics.trackAnonymousEvent<ITestEvent>("jest_test_event", {
analytics.trackEvent<ITestEvent>({
eventName: "JestTestEvents",
foo: "bar",
});
expect(fakePosthog.capture.mock.calls[0][0]).toBe("jest_test_event");
expect(fakePosthog.capture.mock.calls[0][0]).toBe("JestTestEvents");
expect(fakePosthog.capture.mock.calls[0][1]["foo"]).toEqual("bar");
});
it("Should pass trackPseudonymousEvent() to posthog", async () => {
analytics.setAnonymity(Anonymity.Pseudonymous);
await analytics.trackPseudonymousEvent<ITestEvent>("jest_test_pseudo_event", {
foo: "bar",
});
expect(fakePosthog.capture.mock.calls[0][0]).toBe("jest_test_pseudo_event");
expect(fakePosthog.capture.mock.calls[0][1]["foo"]).toEqual("bar");
});
it("Should not track pseudonymous messages if anonymous", async () => {
it("Should not track events if anonymous", async () => {
analytics.setAnonymity(Anonymity.Anonymous);
await analytics.trackPseudonymousEvent<ITestEvent>("jest_test_event", {
await analytics.trackEvent<ITestEvent>({
eventName: "JestTestEvents",
foo: "bar",
});
expect(fakePosthog.capture.mock.calls.length).toBe(0);
@@ -155,43 +130,25 @@ describe("PosthogAnalytics", () => {
it("Should not track any events if disabled", async () => {
analytics.setAnonymity(Anonymity.Disabled);
await analytics.trackPseudonymousEvent<ITestEvent>("jest_test_event", {
analytics.trackEvent<ITestEvent>({
eventName: "JestTestEvents",
foo: "bar",
});
await analytics.trackAnonymousEvent<ITestEvent>("jest_test_event", {
foo: "bar",
});
await analytics.trackPageView(200);
expect(fakePosthog.capture.mock.calls.length).toBe(0);
});
it("Should pseudonymise a location of a known screen", async () => {
const location = await getRedactedCurrentLocation(
"https://foo.bar", "#/register/some/pii", "/", Anonymity.Pseudonymous);
it("Should anonymise location of a known screen", async () => {
const location = getRedactedCurrentLocation("https://foo.bar", "#/register/some/pii", "/");
expect(location).toBe("https://foo.bar/#/register/<redacted>");
});
it("Should anonymise a location of a known screen", async () => {
const location = await getRedactedCurrentLocation(
"https://foo.bar", "#/register/some/pii", "/", Anonymity.Anonymous);
expect(location).toBe("https://foo.bar/#/register/<redacted>");
});
it("Should pseudonymise a location of an unknown screen", async () => {
const location = await getRedactedCurrentLocation(
"https://foo.bar", "#/not_a_screen_name/some/pii", "/", Anonymity.Pseudonymous);
expect(location).toBe("https://foo.bar/#/<redacted_screen_name>/<redacted>");
});
it("Should anonymise a location of an unknown screen", async () => {
const location = await getRedactedCurrentLocation(
"https://foo.bar", "#/not_a_screen_name/some/pii", "/", Anonymity.Anonymous);
it("Should anonymise location of an unknown screen", async () => {
const location = getRedactedCurrentLocation("https://foo.bar", "#/not_a_screen_name/some/pii", "/");
expect(location).toBe("https://foo.bar/#/<redacted_screen_name>/<redacted>");
});
it("Should handle an empty hash", async () => {
const location = await getRedactedCurrentLocation(
"https://foo.bar", "", "/", Anonymity.Anonymous);
const location = getRedactedCurrentLocation("https://foo.bar", "", "/");
expect(location).toBe("https://foo.bar/");
});