Add new user signup event tracking in PostHog (#8412)

This commit is contained in:
Germain 2022-04-28 11:46:02 +01:00 committed by GitHub
parent 1127db0514
commit 1ed68a718f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 14 deletions

View file

@ -18,6 +18,7 @@ import posthog, { PostHog } from 'posthog-js';
import { MatrixClient } from "matrix-js-sdk/src/client";
import { logger } from "matrix-js-sdk/src/logger";
import { UserProperties } from "matrix-analytics-events/types/typescript/UserProperties";
import { Signup } from 'matrix-analytics-events/types/typescript/Signup';
import PlatformPeg from './PlatformPeg';
import SdkConfig from './SdkConfig';
@ -50,6 +51,10 @@ export interface IPosthogEvent {
"$set_once"?: void;
}
export interface IPostHogEventOptions {
timestamp?: Date;
}
export enum Anonymity {
Disabled,
Anonymous,
@ -117,6 +122,7 @@ export class PosthogAnalytics {
private static ANALYTICS_EVENT_TYPE = "im.vector.analytics";
private propertiesForNextEvent: Partial<Record<"$set" | "$set_once", UserProperties>> = {};
private userPropertyCache: UserProperties = {};
private authenticationType: Signup["authenticationType"] = "Other";
public static get instance(): PosthogAnalytics {
if (!this._instance) {
@ -200,16 +206,20 @@ export class PosthogAnalytics {
};
}
private capture(eventName: string, properties: posthog.Properties) {
// eslint-disable-nextline no-unused-varsx
private capture(eventName: string, properties: posthog.Properties, options?: IPostHogEventOptions) {
if (!this.enabled) {
return;
}
const { origin, hash, pathname } = window.location;
properties["redactedCurrentUrl"] = getRedactedCurrentLocation(origin, hash, pathname);
this.posthog.capture(eventName, {
...this.propertiesForNextEvent,
...properties,
});
this.posthog.capture(
eventName,
{ ...this.propertiesForNextEvent, ...properties },
// TODO: Uncomment below once https://github.com/PostHog/posthog-js/pull/391
// gets merged
/* options as any, */ // No proper type definition in the posthog library
);
this.propertiesForNextEvent = {};
}
@ -272,9 +282,12 @@ export class PosthogAnalytics {
this.setAnonymity(Anonymity.Disabled);
}
public trackEvent<E extends IPosthogEvent>({ eventName, ...properties }: E): void {
public trackEvent<E extends IPosthogEvent>(
{ eventName, ...properties }: E,
options?: IPostHogEventOptions,
): void {
if (this.anonymity == Anonymity.Disabled || this.anonymity == Anonymity.Anonymous) return;
this.capture(eventName, properties);
this.capture(eventName, properties, options);
}
public setProperty<K extends keyof UserProperties>(key: K, value: UserProperties[K]): void {
@ -313,6 +326,9 @@ export class PosthogAnalytics {
this.setAnonymity(anonymity);
if (anonymity === Anonymity.Pseudonymous) {
await this.identifyUser(MatrixClientPeg.get(), PosthogAnalytics.getRandomAnalyticsId);
if (MatrixClientPeg.currentUserIsJustRegistered()) {
this.trackNewUserEvent();
}
}
if (anonymity !== Anonymity.Disabled) {
@ -334,4 +350,25 @@ export class PosthogAnalytics {
this.updateAnonymityFromSettings(!!newValue);
});
}
public setAuthenticationType(authenticationType: Signup["authenticationType"]): void {
this.authenticationType = authenticationType;
}
private trackNewUserEvent(): void {
// This is the only event that could have occured before analytics opt-in
// that we want to accumulate before the user has given consent
// All other scenarios should not track a user before they have given
// explicit consent that they are ok with their analytics data being collected
const options: IPostHogEventOptions = {};
const registrationTime = parseInt(window.localStorage.getItem("mx_registration_time"), 10);
if (!isNaN(registrationTime)) {
options.timestamp = new Date(registrationTime);
}
return this.trackEvent<Signup>({
eventName: "Signup",
authenticationType: this.authenticationType,
}, options);
}
}