Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): Events tracking using Posthog #646

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ S3_ACCESS_KEY_ID=
S3_SECRET_ACCESS_KEY=
S3_ENDPOINT=
S3_BUCKET=

# PostHog
POSTHOG_API_KEY=
POSTHOG_HOST=
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"objection-unique": "^1.2.2",
"plaid": "^10.3.0",
"pluralize": "^8.0.0",
"posthog-node": "^4.2.0",
"pug": "^3.0.2",
"puppeteer": "^10.2.0",
"qim": "0.0.52",
Expand Down
8 changes: 8 additions & 0 deletions packages/server/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,12 @@ module.exports = {
enable: parseBoolean(process.env.ONE_CLICK_DEMO_ACCOUNTS, false),
demoUrl: process.env.ONE_CLICK_DEMO_ACCOUNTS_URL || '',
},

/**
* PostHog
*/
posthog: {
apiKey: process.env.POSTHOG_API_KEY,
host: process.env.POSTHOG_HOST
}
};
43 changes: 43 additions & 0 deletions packages/server/src/constants/event-tracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export const SALE_INVOICE_CREATED = 'Sale invoice created';
export const SALE_INVOICE_EDITED = 'Sale invoice d';
export const SALE_INVOICE_DELETED = 'Sale invoice deleted';
export const SALE_INVOICE_MAIL_DELIVERED = 'Sale invoice mail delivered';

export const SALE_ESTIMATE_CREATED = 'Sale estimate created';
export const SALE_ESTIMATE_EDITED = 'Sale estimate edited';
export const SALE_ESTIMATE_DELETED = 'Sale estimate deleted';

export const PAYMENT_RECEIVED_CREATED = 'Payment received created';
export const PAYMENT_RECEIVED_EDITED = 'payment received edited';
export const PAYMENT_RECEIVED_DELETED = 'Payment received deleted';

export const BILL_CREATED = 'Bill created';
export const BILL_EDITED = 'Bill edited';
export const BILL_DELETED = 'Bill deleted';

export const PAYMENT_MADE_CREATED = 'Payment made created';
export const PAYMENT_MADE_EDITED = 'Payment made edited';
export const PAYMENT_MADE_DELETED = 'Payment made deleted';

export const EXPENSE_CREATED = 'Expense created';
export const EXPENSE_EDITED = 'Expense edited';
export const EXPENSE_DELETED = 'Expense deleted';

export const ACCOUNT_CREATED = 'Account created';
export const ACCOUNT_EDITED = 'Account Edited';
export const ACCOUNT_DELETED = 'Account deleted';

export const ITEM_EVENT_CREATED = 'Item created';
export const ITEM_EVENT_EDITED = 'Item edited';
export const ITEM_EVENT_DELETED = 'Item deleted';

export const AUTH_SIGNED_UP = 'Auth Signed-up';
export const AUTH_RESET_PASSWORD = 'Auth reset password';

export const ACCOUNT_GROUP = 'Account';
export const ITEM_GROUP = 'Item';
export const AUTH_GROUP = 'Auth';
export const SALE_GROUP = 'Sale';
export const PAYMENT_GROUP = 'Payment';
export const BILL_GROUP = 'Bill';
export const EXPENSE_GROUP = 'Expense';
3 changes: 3 additions & 0 deletions packages/server/src/loaders/eventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ import { LoopsEventsSubscriber } from '@/services/Loops/LoopsEventsSubscriber';
import { DeleteUncategorizedTransactionsOnAccountDeleting } from '@/services/Banking/BankAccounts/events/DeleteUncategorizedTransactionsOnAccountDeleting';
import { SeedInitialDemoAccountDataOnOrgBuild } from '@/services/OneClickDemo/events/SeedInitialDemoAccountData';
import { TriggerInvalidateCacheOnSubscriptionChange } from '@/services/Subscription/events/TriggerInvalidateCacheOnSubscriptionChange';
import { EventsTrackerListeners } from '@/services/EventsTracker/events/events';

export default () => {
return new EventPublisher();
Expand Down Expand Up @@ -289,5 +290,7 @@ export const susbcribers = () => {

// Demo Account
SeedInitialDemoAccountDataOnOrgBuild,

...EventsTrackerListeners
];
};
24 changes: 24 additions & 0 deletions packages/server/src/services/EventsTracker/PostHog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PostHog } from 'posthog-node';
import { Service } from 'typedi';
import { EventMessage } from 'posthog-node/src/types';
import config from '@/config';

@Service()
export class PosthogService {
public posthog: PostHog;

constructor() {
if (config.posthog.apiKey && config.posthog.host) {
this.posthog = new PostHog(config.posthog.apiKey, {
host: config.posthog.host,
});
}
}

public trackEvent(event: EventMessage) {
// Cannot continue if the Posthog not configured.
if (!this.posthog) return;

this.posthog.capture(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import {
IAccountEventCreatedPayload,
IAccountEventEditedPayload,
IAccountEventDeletedPayload,
} from '@/interfaces';
import { PosthogService } from '../PostHog';
import events from '@/subscribers/events';
import {
ACCOUNT_CREATED,
ACCOUNT_EDITED,
ACCOUNT_DELETED,
} from '@/constants/event-tracker';

@Service()
export class AccountEventsTracker extends EventSubscriber {
@Inject()
private posthog: PosthogService;

/**
* Constructor method.
*/
public attach(bus) {
bus.subscribe(
events.accounts.onCreated,
this.handleTrackAccountCreatedEvent
);
bus.subscribe(events.accounts.onEdited, this.handleTrackEditedAccountEvent);
bus.subscribe(
events.accounts.onDeleted,
this.handleTrackDeletedAccountEvent
);
}

private handleTrackAccountCreatedEvent = ({
tenantId,
}: IAccountEventCreatedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: ACCOUNT_CREATED,
properties: {},
});
};

private handleTrackEditedAccountEvent = ({
tenantId,
}: IAccountEventEditedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: ACCOUNT_EDITED,
properties: {},
});
};

private handleTrackDeletedAccountEvent = ({
tenantId,
}: IAccountEventDeletedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: ACCOUNT_DELETED,
properties: {},
});
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import { IAuthSignedUpEventPayload } from '@/interfaces';
import { PosthogService } from '../PostHog';
import { AUTH_SIGNED_UP } from '@/constants/event-tracker';
import events from '@/subscribers/events';

@Service()
export class AuthenticationEventsTracker extends EventSubscriber {
@Inject()
private posthog: PosthogService;

/**
* Constructor method.
*/
public attach(bus) {
bus.subscribe(events.auth.signUp, this.handleTrackSignUpEvent);
}

private handleTrackSignUpEvent = ({
signupDTO,
user,
tenant,
}: IAuthSignedUpEventPayload) => {
this.posthog.trackEvent({
distinctId: user.email,
event: AUTH_SIGNED_UP,
properties: {
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
},
});
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import {
IBillPaymentEventCreatedPayload,
IBillPaymentEventEditedPayload,
IBillPaymentEventDeletedPayload,
} from '@/interfaces';
import { PosthogService } from '../PostHog';
import events from '@/subscribers/events';
import {
BILL_CREATED,
BILL_EDITED,
BILL_DELETED,
} from '@/constants/event-tracker';

@Service()
export class BillEventsTracker extends EventSubscriber {
@Inject()
private posthog: PosthogService;

/**
* Constructor method.
*/
public attach(bus) {
bus.subscribe(events.bill.onCreated, this.handleTrackBillCreatedEvent);
bus.subscribe(events.bill.onEdited, this.handleTrackEditedBillEvent);
bus.subscribe(events.bill.onDeleted, this.handleTrackDeletedBillEvent);
}

private handleTrackBillCreatedEvent = ({
tenantId,
}: IBillPaymentEventCreatedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: BILL_CREATED,
properties: {},
});
};

private handleTrackEditedBillEvent = ({
tenantId,
}: IBillPaymentEventEditedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: BILL_EDITED,
properties: {},
});
};

private handleTrackDeletedBillEvent = ({
tenantId,
}: IBillPaymentEventDeletedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: BILL_DELETED,
properties: {},
});
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import {
IExpenseCreatedPayload,
IExpenseEventEditPayload,
IExpenseEventDeletePayload,
} from '@/interfaces';
import { PosthogService } from '../PostHog';
import events from '@/subscribers/events';
import {
EXPENSE_CREATED,
EXPENSE_EDITED,
EXPENSE_DELETED,
} from '@/constants/event-tracker';

@Service()
export class ExpenseEventsTracker extends EventSubscriber {
@Inject()
private posthog: PosthogService;

/**
* Constructor method.
*/
public attach(bus) {
bus.subscribe(
events.expenses.onCreated,
this.handleTrackExpenseCreatedEvent
);
bus.subscribe(events.expenses.onEdited, this.handleTrackEditedExpenseEvent);
bus.subscribe(
events.expenses.onDeleted,
this.handleTrackDeletedExpenseEvent
);
}

private handleTrackExpenseCreatedEvent = ({
tenantId,
}: IExpenseCreatedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: EXPENSE_CREATED,
properties: {},
});
};

private handleTrackEditedExpenseEvent = ({
tenantId,
}: IExpenseEventEditPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: EXPENSE_EDITED,
properties: {},
});
};

private handleTrackDeletedExpenseEvent = ({
tenantId,
}: IExpenseEventDeletePayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: EXPENSE_DELETED,
properties: {},
});
};
}
Loading
Loading