mirror of https://github.com/vector-im/riot-web
Add Disabled anonymity, improve tests
parent
c34afdb4bd
commit
95f4275807
|
@ -12,6 +12,7 @@ interface IEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum Anonymity {
|
export enum Anonymity {
|
||||||
|
Disabled,
|
||||||
Anonymous,
|
Anonymous,
|
||||||
Pseudonymous
|
Pseudonymous
|
||||||
}
|
}
|
||||||
|
@ -181,12 +182,12 @@ export class PosthogAnalytics {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async capture(eventName: string, properties: posthog.Properties) {
|
private async capture(eventName: string, properties: posthog.Properties) {
|
||||||
if (!this.enabled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!this.initialised) {
|
if (!this.initialised) {
|
||||||
throw Error("Tried to track event before PoshogAnalytics.init has completed");
|
throw Error("Tried to track event before PoshogAnalytics.init has completed");
|
||||||
}
|
}
|
||||||
|
if (!this.enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const { origin, hash, pathname } = window.location;
|
const { origin, hash, pathname } = window.location;
|
||||||
properties['$redacted_current_url'] = await getRedactedCurrentLocation(
|
properties['$redacted_current_url'] = await getRedactedCurrentLocation(
|
||||||
origin, hash, pathname, this.anonymity);
|
origin, hash, pathname, this.anonymity);
|
||||||
|
@ -197,7 +198,7 @@ export class PosthogAnalytics {
|
||||||
eventName: E["eventName"],
|
eventName: E["eventName"],
|
||||||
properties: E["properties"],
|
properties: E["properties"],
|
||||||
) {
|
) {
|
||||||
if (this.anonymity == Anonymity.Anonymous) return;
|
if (this.anonymity == Anonymity.Anonymous || this.anonymity == Anonymity.Disabled) return;
|
||||||
await this.capture(eventName, properties);
|
await this.capture(eventName, properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,6 +206,7 @@ export class PosthogAnalytics {
|
||||||
eventName: E["eventName"],
|
eventName: E["eventName"],
|
||||||
properties: E["properties"],
|
properties: E["properties"],
|
||||||
) {
|
) {
|
||||||
|
if (this.anonymity == Anonymity.Disabled) return;
|
||||||
await this.capture(eventName, properties);
|
await this.capture(eventName, properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Anonymity, getRedactedCurrentLocation, IAnonymousEvent, IRoomEvent,
|
import { Anonymity, getRedactedCurrentLocation, IAnonymousEvent, IPseudonymousEvent, IRoomEvent,
|
||||||
PosthogAnalytics } from '../src/PosthogAnalytics';
|
PosthogAnalytics } from '../src/PosthogAnalytics';
|
||||||
import SdkConfig from '../src/SdkConfig';
|
import SdkConfig from '../src/SdkConfig';
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
@ -22,6 +22,13 @@ export interface ITestEvent extends IAnonymousEvent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ITestPseudonymousEvent extends IPseudonymousEvent {
|
||||||
|
key: "jest_test_pseudo_event",
|
||||||
|
properties: {
|
||||||
|
foo: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export interface ITestRoomEvent extends IRoomEvent {
|
export interface ITestRoomEvent extends IRoomEvent {
|
||||||
key: "jest_test_room_event",
|
key: "jest_test_room_event",
|
||||||
properties: {
|
properties: {
|
||||||
|
@ -75,7 +82,7 @@ describe("PosthogAnalytics", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should pass track() to posthog", async () => {
|
it("Should pass trackAnonymousEvent() to posthog", async () => {
|
||||||
analytics.init(Anonymity.Pseudonymous);
|
analytics.init(Anonymity.Pseudonymous);
|
||||||
await analytics.trackAnonymousEvent<ITestEvent>("jest_test_event", {
|
await analytics.trackAnonymousEvent<ITestEvent>("jest_test_event", {
|
||||||
foo: "bar",
|
foo: "bar",
|
||||||
|
@ -96,18 +103,44 @@ describe("PosthogAnalytics", () => {
|
||||||
.toEqual("73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049");
|
.toEqual("73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should silently not track if not inititalised", async () => {
|
it("Should pass trackPseudonymousEvent() to posthog", async () => {
|
||||||
await analytics.trackAnonymousEvent<ITestEvent>("jest_test_event", {
|
analytics.init(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 blow up if not inititalised prior to tracking", async () => {
|
||||||
|
const fn = () => {
|
||||||
|
return analytics.trackAnonymousEvent<ITestEvent>("jest_test_event", {
|
||||||
|
foo: "bar",
|
||||||
|
});
|
||||||
|
};
|
||||||
|
await expect(fn()).rejects.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should not track pseudonymous messages if anonymous", async () => {
|
||||||
|
analytics.init(Anonymity.Anonymous);
|
||||||
|
await analytics.trackPseudonymousEvent<ITestEvent>("jest_test_event", {
|
||||||
foo: "bar",
|
foo: "bar",
|
||||||
});
|
});
|
||||||
expect(fakePosthog.capture.mock.calls.length).toBe(0);
|
expect(fakePosthog.capture.mock.calls.length).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should not track non-anonymous messages if anonymous", async () => {
|
it("Should not track any events if disabled", async () => {
|
||||||
analytics.init(Anonymity.Anonymous);
|
analytics.init(Anonymity.Disabled);
|
||||||
await analytics.trackPseudonymousEvent<ITestEvent>("jest_test_event", {
|
await analytics.trackPseudonymousEvent<ITestEvent>("jest_test_event", {
|
||||||
foo: "bar",
|
foo: "bar",
|
||||||
});
|
});
|
||||||
|
await analytics.trackAnonymousEvent<ITestEvent>("jest_test_event", {
|
||||||
|
foo: "bar",
|
||||||
|
});
|
||||||
|
await analytics.trackRoomEvent<ITestRoomEvent>("room id", "jest_test_room_event", {
|
||||||
|
foo: "bar",
|
||||||
|
});
|
||||||
|
await analytics.trackPageView(200);
|
||||||
expect(fakePosthog.capture.mock.calls.length).toBe(0);
|
expect(fakePosthog.capture.mock.calls.length).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue