-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #646 from bigcapitalhq/events-tracking
feat(server): Events tracking using Posthog
- Loading branch information
Showing
17 changed files
with
685 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,3 +89,7 @@ S3_ACCESS_KEY_ID= | |
S3_SECRET_ACCESS_KEY= | ||
S3_ENDPOINT= | ||
S3_BUCKET= | ||
|
||
# PostHog | ||
POSTHOG_API_KEY= | ||
POSTHOG_HOST= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/server/src/services/EventsTracker/events/AccountEventsTracker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {}, | ||
}); | ||
}; | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/server/src/services/EventsTracker/events/AuthenticationEventsTracker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
}; | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/server/src/services/EventsTracker/events/BillEventsTracker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {}, | ||
}); | ||
}; | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/server/src/services/EventsTracker/events/ExpenseEventsTracker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {}, | ||
}); | ||
}; | ||
} |
Oops, something went wrong.