feat(auth): track registration in analytics

This commit is contained in:
Karol Sójko 2022-08-09 20:18:53 +02:00
parent 29674b02e6
commit f25195b2c1
No known key found for this signature in database
GPG key ID: 78C42DF8267BAF26
3 changed files with 34 additions and 2 deletions

View file

@ -2,6 +2,7 @@ export enum AnalyticsActivity {
GeneralActivity = 'general-activity',
EditingItems = 'editing-items',
Login = 'login',
Register = 'register',
EmailUnbackedUpData = 'email-unbacked-up-data',
EmailBackup = 'email-backup',
LimitedDiscountOfferPurchased = 'limited-discount-offer-purchased',

View file

@ -4,16 +4,27 @@ import { Logger } from 'winston'
import { UserRegisteredEventHandler } from './UserRegisteredEventHandler'
import { AxiosInstance } from 'axios'
import { GetUserAnalyticsId } from '../UseCase/GetUserAnalyticsId/GetUserAnalyticsId'
import { AnalyticsStoreInterface } from '@standardnotes/analytics'
describe('UserRegisteredEventHandler', () => {
let httpClient: AxiosInstance
const userServerRegistrationUrl = 'https://user-server/registration'
const userServerAuthKey = 'auth-key'
let event: UserRegisteredEvent
let getUserAnalyticsId: GetUserAnalyticsId
let analyticsStore: AnalyticsStoreInterface
let logger: Logger
const createHandler = () =>
new UserRegisteredEventHandler(httpClient, userServerRegistrationUrl, userServerAuthKey, logger)
new UserRegisteredEventHandler(
httpClient,
userServerRegistrationUrl,
userServerAuthKey,
getUserAnalyticsId,
analyticsStore,
logger,
)
beforeEach(() => {
httpClient = {} as jest.Mocked<AxiosInstance>
@ -26,6 +37,12 @@ describe('UserRegisteredEventHandler', () => {
email: 'test@test.te',
}
getUserAnalyticsId = {} as jest.Mocked<GetUserAnalyticsId>
getUserAnalyticsId.execute = jest.fn().mockReturnValue({ analyticsId: 3 })
analyticsStore = {} as jest.Mocked<AnalyticsStoreInterface>
analyticsStore.markActivity = jest.fn()
logger = {} as jest.Mocked<Logger>
logger.debug = jest.fn()
})
@ -52,7 +69,14 @@ describe('UserRegisteredEventHandler', () => {
})
it('should not send a request to the user management server about a registration if url is not defined', async () => {
const handler = new UserRegisteredEventHandler(httpClient, '', userServerAuthKey, logger)
const handler = new UserRegisteredEventHandler(
httpClient,
'',
userServerAuthKey,
getUserAnalyticsId,
analyticsStore,
logger,
)
await handler.handle(event)
expect(httpClient.request).not.toHaveBeenCalled()

View file

@ -1,9 +1,11 @@
import { AnalyticsActivity, AnalyticsStoreInterface, Period } from '@standardnotes/analytics'
import { DomainEventHandlerInterface, UserRegisteredEvent } from '@standardnotes/domain-events'
import { AxiosInstance } from 'axios'
import { inject, injectable } from 'inversify'
import { Logger } from 'winston'
import TYPES from '../../Bootstrap/Types'
import { GetUserAnalyticsId } from '../UseCase/GetUserAnalyticsId/GetUserAnalyticsId'
@injectable()
export class UserRegisteredEventHandler implements DomainEventHandlerInterface {
@ -11,6 +13,8 @@ export class UserRegisteredEventHandler implements DomainEventHandlerInterface {
@inject(TYPES.HTTPClient) private httpClient: AxiosInstance,
@inject(TYPES.USER_SERVER_REGISTRATION_URL) private userServerRegistrationUrl: string,
@inject(TYPES.USER_SERVER_AUTH_KEY) private userServerAuthKey: string,
@inject(TYPES.GetUserAnalyticsId) private getUserAnalyticsId: GetUserAnalyticsId,
@inject(TYPES.AnalyticsStore) private analyticsStore: AnalyticsStoreInterface,
@inject(TYPES.Logger) private logger: Logger,
) {}
@ -20,6 +24,9 @@ export class UserRegisteredEventHandler implements DomainEventHandlerInterface {
return
}
const { analyticsId } = await this.getUserAnalyticsId.execute({ userUuid: event.payload.userUuid })
await this.analyticsStore.markActivity([AnalyticsActivity.Register], analyticsId, [Period.Today])
await this.httpClient.request({
method: 'POST',
url: this.userServerRegistrationUrl,