Skip to content

Commit

Permalink
chore(#8433) add telemetry events to contact forms (#8676)
Browse files Browse the repository at this point in the history
* Feat: add telemetry event to contact form

* Fix: update the field of the event

* Chore: add telemetry data to contacts page

* Chore: update unit tests

* Update webapp/src/ts/modules/contacts/contacts-edit.component.ts

---------
  • Loading branch information
Benmuiruri authored Nov 3, 2023
1 parent cf5b725 commit c34e12b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
29 changes: 28 additions & 1 deletion webapp/src/ts/modules/contacts/contacts-edit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ContactSaveService } from '@mm-services/contact-save.service';
import { Selectors } from '@mm-selectors/index';
import { GlobalActions } from '@mm-actions/global';
import { ContactsActions } from '@mm-actions/contacts';
import { TelemetryService } from '@mm-services/telemetry.service';
import { TranslateService } from '@mm-services/translate.service';


Expand All @@ -29,6 +30,7 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
private contactTypesService:ContactTypesService,
private dbService:DbService,
private contactSaveService:ContactSaveService,
private telemetryService:TelemetryService,
private translateService:TranslateService,
) {
this.globalActions = new GlobalActions(store);
Expand All @@ -54,6 +56,9 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
enketoContact;

private routeSnapshot;
private telemetryData: any = {
preRender: Date.now()
};

ngOnInit() {
this.subscribeToStore();
Expand Down Expand Up @@ -251,7 +256,17 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
formContext.valuechangeListener = this.resetFormError.bind(this);
formContext.titleKey = titleKey;

return this.formService.render(formContext);
const formInstance = await this.formService.render(formContext);
this.telemetryData.postRender = Date.now();
this.telemetryData.form = formId;
this.telemetryData.action = 'edit';

await this.telemetryService.record(
`enketo:contacts:${this.telemetryData.form}:${this.telemetryData.action}:render`,
this.telemetryData.postRender - this.telemetryData.preRender
);

return formInstance;
}

private setEnketoContact(formInstance) {
Expand All @@ -268,6 +283,12 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
return;
}

this.telemetryData.preSave = Date.now();
this.telemetryService.record(
`enketo:contacts:${this.telemetryData.form}:${this.telemetryData.action}:user_edit_time`,
this.telemetryData.preSave - this.telemetryData.postRender
);

const form = this.enketoContact.formInstance;
const docId = this.enketoContact.docId;
this.globalActions.setEnketoSavingStatus(true);
Expand All @@ -291,6 +312,12 @@ export class ContactsEditComponent implements OnInit, OnDestroy, AfterViewInit {
this.globalActions.setEnketoSavingStatus(false);
this.globalActions.setEnketoEditedStatus(false);

this.telemetryData.postSave = Date.now();
this.telemetryService.record(
`enketo:contacts:${this.telemetryData.form}:${this.telemetryData.action}:save`,
this.telemetryData.postSave - this.telemetryData.preSave
);

this.translateService
.get(docId ? 'contact.updated' : 'contact.created')
.then(snackBarContent => this.globalActions.setSnackbarContent(snackBarContent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { EnketoComponent } from '@mm-components/enketo/enketo.component';
import { ContactsEditComponent } from '@mm-modules/contacts/contacts-edit.component';
import { ComponentsModule } from '@mm-components/components.module';
import { TranslateService } from '@mm-services/translate.service';
import { TelemetryService } from '@mm-services/telemetry.service';
import { DbService } from '@mm-services/db.service';
import { Selectors } from '@mm-selectors/index';
import { LineageModelGeneratorService } from '@mm-services/lineage-model-generator.service';
Expand All @@ -33,6 +34,7 @@ describe('ContactsEdit component', () => {
let lineageModelGeneratorService;
let contactSaveService;
let routeSnapshot;
let telemetryService;

beforeEach(() => {
contactTypesService = {
Expand All @@ -54,6 +56,7 @@ describe('ContactsEdit component', () => {
render: sinon.stub(),
unload: sinon.stub(),
};
telemetryService = { record: sinon.stub() };
lineageModelGeneratorService = { contact: sinon.stub().resolves({ doc: { } }) };
contactSaveService = { save: sinon.stub() };

Expand Down Expand Up @@ -84,6 +87,7 @@ describe('ContactsEdit component', () => {
{ provide: FormService, useValue: formService },
{ provide: ContactTypesService, useValue: contactTypesService },
{ provide: ContactSaveService, useValue: contactSaveService },
{ provide: TelemetryService, useValue: telemetryService },
],
declarations: [
EnketoComponent,
Expand Down Expand Up @@ -326,6 +330,8 @@ describe('ContactsEdit component', () => {
titleKey: 'clinic_create_key',
});
expect(component.contentError).to.equal(false);
expect(telemetryService.record.calledOnce).to.be.true;
expect(telemetryService.record.args[0][0]).to.equal('enketo:contacts:clinic_create_form_id:edit:render');
});

it('should render form without parent', async () => {
Expand Down Expand Up @@ -357,6 +363,8 @@ describe('ContactsEdit component', () => {
titleKey: 'district_create_key',
});
expect(component.contentError).to.equal(false);
expect(telemetryService.record.calledOnce).to.be.true;
expect(telemetryService.record.args[0][0]).to.equal('enketo:contacts:district_create_form_id:edit:render');
});
});

Expand Down Expand Up @@ -462,6 +470,8 @@ describe('ContactsEdit component', () => {
type: 'patient',
});
expect(component.contentError).to.equal(false);
expect(telemetryService.record.calledOnce).to.be.true;
expect(telemetryService.record.args[0][0]).to.equal('enketo:contacts:patient_edit_form:edit:render');
});

it('should render form with create form', async () => {
Expand Down Expand Up @@ -498,6 +508,8 @@ describe('ContactsEdit component', () => {
type: 'a_clinic_type',
});
expect(component.contentError).to.equal(false);
expect(telemetryService.record.calledOnce).to.be.true;
expect(telemetryService.record.args[0][0]).to.equal('enketo:contacts:a_clinic_type_create_form:edit:render');
});

it('should select correct form for correct type', async () => {
Expand Down Expand Up @@ -537,6 +549,8 @@ describe('ContactsEdit component', () => {
type: 'the correct type',
});
expect(component.contentError).to.equal(false);
expect(telemetryService.record.calledOnce).to.be.true;
expect(telemetryService.record.args[0][0]).to.equal('enketo:contacts:the correct_edit_form:edit:render');
});
});
});
Expand Down Expand Up @@ -618,7 +632,10 @@ describe('ContactsEdit component', () => {
contactSaveService.save.resolves({ docId: 'new_clinic_id' });

await component.save();

expect(telemetryService.record.callCount).to.equal(3);
expect(telemetryService.record.args[0][0]).to.equal('enketo:contacts:clinic_create_form_id:edit:render');
expect(telemetryService.record.args[1][0]).to.equal('enketo:contacts:clinic_create_form_id:edit:user_edit_time');
expect(telemetryService.record.args[2][0]).to.equal('enketo:contacts:clinic_create_form_id:edit:save');
expect(setEnketoSavingStatus.callCount).to.equal(2);
expect(setEnketoSavingStatus.args).to.deep.equal([[true], [false]]);
expect(setEnketoError.callCount).to.equal(1);
Expand Down Expand Up @@ -661,6 +678,9 @@ describe('ContactsEdit component', () => {
expect(contactSaveService.save.args[0]).to.deep.equal([ form, 'the_person', 'person', undefined ]);
expect(router.navigate.callCount).to.equal(1);
expect(router.navigate.args[0]).to.deep.equal([['/contacts', 'the_person']]);
expect(telemetryService.record.callCount).to.equal(3);
expect(telemetryService.record.args[0][0]).to.equal('enketo:contacts:person_edit_form_id:edit:render');
expect(telemetryService.record.args[1][0]).to.equal('enketo:contacts:person_edit_form_id:edit:user_edit_time');
});

it('when editing existent contact of configurable type', async () => {
Expand Down Expand Up @@ -696,6 +716,9 @@ describe('ContactsEdit component', () => {
expect(contactSaveService.save.args[0]).to.deep.equal([ form, 'the_patient', 'patient', undefined ]);
expect(router.navigate.callCount).to.equal(1);
expect(router.navigate.args[0]).to.deep.equal([['/contacts', 'the_patient']]);
expect(telemetryService.record.callCount).to.equal(3);
expect(telemetryService.record.args[0][0]).to.equal('enketo:contacts:patient_create_form_id:edit:render');
expect(telemetryService.record.args[1][0]).to.equal('enketo:contacts:patient_create_form_id:edit:user_edit_time');
});
});
});

0 comments on commit c34e12b

Please sign in to comment.