Add Disabled anonymity, improve tests

pull/21833/head
James Salter 2021-07-23 16:47:02 +01:00
parent c34afdb4bd
commit 95f4275807
2 changed files with 45 additions and 10 deletions

View File

@ -12,6 +12,7 @@ interface IEvent {
}
export enum Anonymity {
Disabled,
Anonymous,
Pseudonymous
}
@ -181,12 +182,12 @@ export class PosthogAnalytics {
}
private async capture(eventName: string, properties: posthog.Properties) {
if (!this.enabled) {
return;
}
if (!this.initialised) {
throw Error("Tried to track event before PoshogAnalytics.init has completed");
}
if (!this.enabled) {
return;
}
const { origin, hash, pathname } = window.location;
properties['$redacted_current_url'] = await getRedactedCurrentLocation(
origin, hash, pathname, this.anonymity);
@ -197,7 +198,7 @@ export class PosthogAnalytics {
eventName: E["eventName"],
properties: E["properties"],
) {
if (this.anonymity == Anonymity.Anonymous) return;
if (this.anonymity == Anonymity.Anonymous || this.anonymity == Anonymity.Disabled) return;
await this.capture(eventName, properties);
}
@ -205,6 +206,7 @@ export class PosthogAnalytics {
eventName: E["eventName"],
properties: E["properties"],
) {
if (this.anonymity == Anonymity.Disabled) return;
await this.capture(eventName, properties);
}

View File

@ -1,4 +1,4 @@
import { Anonymity, getRedactedCurrentLocation, IAnonymousEvent, IRoomEvent,
import { Anonymity, getRedactedCurrentLocation, IAnonymousEvent, IPseudonymousEvent, IRoomEvent,
PosthogAnalytics } from '../src/PosthogAnalytics';
import SdkConfig from '../src/SdkConfig';
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 {
key: "jest_test_room_event",
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);
await analytics.trackAnonymousEvent<ITestEvent>("jest_test_event", {
foo: "bar",
@ -96,18 +103,44 @@ describe("PosthogAnalytics", () => {
.toEqual("73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049");
});
it("Should silently not track if not inititalised", async () => {
await analytics.trackAnonymousEvent<ITestEvent>("jest_test_event", {
it("Should pass trackPseudonymousEvent() to posthog", async () => {
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",
});
expect(fakePosthog.capture.mock.calls.length).toBe(0);
});
it("Should not track non-anonymous messages if anonymous", async () => {
analytics.init(Anonymity.Anonymous);
it("Should not track any events if disabled", async () => {
analytics.init(Anonymity.Disabled);
await analytics.trackPseudonymousEvent<ITestEvent>("jest_test_event", {
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);
});