feat: Tracking more Posthog events

This commit is contained in:
Ahmed Bouhuolia
2024-09-03 11:26:24 +02:00
parent 81c0761fbe
commit 62594efa00
6 changed files with 273 additions and 0 deletions

View File

@@ -34,6 +34,26 @@ 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 SUBSCRIPTION_CANCELLED = 'Subscription cancelled';
export const SUBSCRIPTION_RESUMED = 'Subscription resumed';
export const SUBSCRIPTION_PLAN_CHANGED = 'Subscription plan changed';
export const CUSTOMER_CREATED = 'Customer created';
export const CUSTOMER_EDITED = 'Customer edited';
export const CUSTOMER_DELETED = 'Customer deleted';
export const VENDOR_CREATED = 'Vendor created';
export const VENDOR_EDITED = 'Vendor edited';
export const VENDOR_DELETED = 'Vendor deleted';
export const TRANSACTIONS_LOCKING_LOCKED = 'Transactions locking locked';
export const TRANSACTIONS_LOCKING_LOCKING_CANCELLED =
'Transactions locking cancelled';
export const TRANSACTIONS_LOCKING_PARTIALLY_UNLOCKED =
'Transactions locking partially unlocked';
export const TRANSACTIONS_LOCKING_PARTIALLY_UNLOCK_CANCELLED =
'Transactions locking partially unlock cancelled';
export const ACCOUNT_GROUP = 'Account';
export const ITEM_GROUP = 'Item';
export const AUTH_GROUP = 'Auth';

View File

@@ -241,6 +241,7 @@ export interface ICustomerEventCreatingPayload {
trx: Knex.Transaction;
}
export interface ICustomerEventEditedPayload {
tenantId: number;
customerId: number;
customer: ICustomer;
trx: Knex.Transaction;

View File

@@ -0,0 +1,65 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import {
ICustomerEventCreatedPayload,
ICustomerEventEditedPayload,
ICustomerEventDeletedPayload,
} from '@/interfaces';
import { PosthogService } from '../PostHog';
import events from '@/subscribers/events';
import {
CUSTOMER_CREATED,
CUSTOMER_EDITED,
CUSTOMER_DELETED,
} from '@/constants/event-tracker';
@Service()
export class CustomerEventsTracker extends EventSubscriber {
@Inject()
private posthog: PosthogService;
public attach(bus) {
bus.subscribe(
events.customers.onCreated,
this.handleTrackCustomerCreatedEvent
);
bus.subscribe(
events.customers.onEdited,
this.handleTrackEditedCustomerEvent
);
bus.subscribe(
events.customers.onDeleted,
this.handleTrackDeletedCustomerEvent
);
}
private handleTrackCustomerCreatedEvent = ({
tenantId,
}: ICustomerEventCreatedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: CUSTOMER_CREATED,
properties: {},
});
};
private handleTrackEditedCustomerEvent = ({
tenantId,
}: ICustomerEventEditedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: CUSTOMER_EDITED,
properties: {},
});
};
private handleTrackDeletedCustomerEvent = ({
tenantId,
}: ICustomerEventDeletedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: CUSTOMER_DELETED,
properties: {},
});
};
}

View File

@@ -0,0 +1,76 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import { ITransactionsLockingPartialUnlocked } from '@/interfaces';
import { PosthogService } from '../PostHog';
import {
TRANSACTIONS_LOCKING_LOCKED,
TRANSACTIONS_LOCKING_LOCKING_CANCELLED,
TRANSACTIONS_LOCKING_PARTIALLY_UNLOCK_CANCELLED,
TRANSACTIONS_LOCKING_PARTIALLY_UNLOCKED,
} from '@/constants/event-tracker';
import events from '@/subscribers/events';
@Service()
export class TransactionsLockingEventsTracker extends EventSubscriber {
@Inject()
private posthog: PosthogService;
public attach(bus) {
bus.subscribe(
events.transactionsLocking.locked,
this.handleTransactionsLockingLockedEvent
);
bus.subscribe(
events.transactionsLocking.lockCanceled,
this.handleTransactionsLockingCancelledEvent
);
bus.subscribe(
events.transactionsLocking.partialUnlocked,
this.handleTransactionsLockingPartiallyUnlockedEvent
);
bus.subscribe(
events.transactionsLocking.partialUnlockCanceled,
this.handleTransactionsLockingPartiallyUnlockCancelledEvent
);
}
private handleTransactionsLockingLockedEvent = ({
tenantId,
}: ITransactionsLockingPartialUnlocked) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: TRANSACTIONS_LOCKING_LOCKED,
properties: {},
});
};
private handleTransactionsLockingCancelledEvent = ({
tenantId,
}: ITransactionsLockingPartialUnlocked) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: TRANSACTIONS_LOCKING_LOCKING_CANCELLED,
properties: {},
});
};
private handleTransactionsLockingPartiallyUnlockedEvent = ({
tenantId,
}: ITransactionsLockingPartialUnlocked) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: TRANSACTIONS_LOCKING_PARTIALLY_UNLOCKED,
properties: {},
});
};
private handleTransactionsLockingPartiallyUnlockCancelledEvent = ({
tenantId,
}: ITransactionsLockingPartialUnlocked) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: TRANSACTIONS_LOCKING_PARTIALLY_UNLOCK_CANCELLED,
properties: {},
});
};
}

View File

@@ -0,0 +1,55 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import { ITransactionsLockingPartialUnlocked } from '@/interfaces';
import { PosthogService } from '../PostHog';
import {
SUBSCRIPTION_CANCELLED,
SUBSCRIPTION_PLAN_CHANGED,
SUBSCRIPTION_RESUMED,
} from '@/constants/event-tracker';
import events from '@/subscribers/events';
@Service()
export class TransactionsLockingEventsTracker extends EventSubscriber {
@Inject()
private posthog: PosthogService;
public attach(bus) {
bus.subscribe(
events.subscription.onSubscriptionResumed,
this.handleSubscriptionResumedEvent
);
bus.subscribe(
events.subscription.onSubscriptionCancelled,
this.handleSubscriptionCancelledEvent
);
bus.subscribe(
events.subscription.onSubscriptionPlanChanged,
this.handleSubscriptionPlanChangedEvent
);
}
private handleSubscriptionResumedEvent = ({ tenantId }) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: SUBSCRIPTION_RESUMED,
properties: {},
});
};
private handleSubscriptionCancelledEvent = ({ tenantId }) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: SUBSCRIPTION_CANCELLED,
properties: {},
});
};
private handleSubscriptionPlanChangedEvent = ({ tenantId }) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: SUBSCRIPTION_PLAN_CHANGED,
properties: {},
});
};
}

View File

@@ -0,0 +1,56 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import {
IVendorEventCreatedPayload,
IVendorEventEditedPayload,
IVendorEventDeletedPayload,
} from '@/interfaces';
import { PosthogService } from '../PostHog';
import events from '@/subscribers/events';
import {
VENDOR_CREATED,
VENDOR_EDITED,
VENDOR_DELETED,
} from '@/constants/event-tracker';
@Service()
export class VendorEventsTracker extends EventSubscriber {
@Inject()
private posthog: PosthogService;
public attach(bus) {
bus.subscribe(events.vendors.onCreated, this.handleTrackVendorCreatedEvent);
bus.subscribe(events.vendors.onEdited, this.handleTrackEditedVendorEvent);
bus.subscribe(events.vendors.onDeleted, this.handleTrackDeletedVendorEvent);
}
private handleTrackVendorCreatedEvent = ({
tenantId,
}: IVendorEventCreatedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: VENDOR_CREATED,
properties: {},
});
};
private handleTrackEditedVendorEvent = ({
tenantId,
}: IVendorEventEditedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: VENDOR_EDITED,
properties: {},
});
};
private handleTrackDeletedVendorEvent = ({
tenantId,
}: IVendorEventDeletedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: VENDOR_DELETED,
properties: {},
});
};
}