Add identifyUser

pull/21833/head
James Salter 2021-07-21 08:38:58 +01:00
parent 74b0e52f9a
commit 4b0cb409a0
2 changed files with 22 additions and 1 deletions

View File

@ -64,6 +64,11 @@ export class PosthogAnalytics {
} }
} }
public async identifyUser(userId: string) {
if (this.onlyTrackAnonymousEvents) return;
this.posthog.identify(await hashHex(userId));
}
public isInitialised(): boolean { public isInitialised(): boolean {
return this.initialised; return this.initialised;
} }

View File

@ -5,10 +5,12 @@ const crypto = require('crypto');
class FakePosthog { class FakePosthog {
public capture; public capture;
public init; public init;
public identify;
constructor() { constructor() {
this.capture = jest.fn(); this.capture = jest.fn();
this.init = jest.fn(); this.init = jest.fn();
this.identify = jest.fn();
} }
} }
@ -75,7 +77,7 @@ describe("PosthogAnalytics", () => {
expect(fakePosthog.capture.mock.calls[0][1]).toEqual({ foo: "bar" }); expect(fakePosthog.capture.mock.calls[0][1]).toEqual({ foo: "bar" });
}); });
it("Should pass trackRoomEvent to posthog", () => { it("Should pass trackRoomEvent to posthog", async () => {
analytics.init(false); analytics.init(false);
const roomId = "42"; const roomId = "42";
return analytics.trackRoomEvent<IRoomEvent>("jest_test_event", roomId, { return analytics.trackRoomEvent<IRoomEvent>("jest_test_event", roomId, {
@ -102,4 +104,18 @@ describe("PosthogAnalytics", () => {
foo: "bar", foo: "bar",
}); });
}); });
it("Should identify the user to posthog if onlyTrackAnonymousEvents is false", async () => {
analytics.init(false);
await analytics.identifyUser("foo");
expect(fakePosthog.identify.mock.calls[0][0])
.toBe("2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae");
});
it("Should not identify the user to posthog if onlyTrackAnonymousEvents is true", async () => {
analytics.init(true);
return analytics.identifyUser("foo").then(() => {
expect(fakePosthog.identify.mock.calls.length).toBe(0);
});
});
}); });