diff --git a/src/abstractions/event.service.ts b/src/abstractions/event.service.ts index d6777225129..40c080275f6 100644 --- a/src/abstractions/event.service.ts +++ b/src/abstractions/event.service.ts @@ -3,4 +3,5 @@ import { EventType } from '../enums/eventType'; export abstract class EventService { collect: (eventType: EventType, cipherId?: string, uploadImmediately?: boolean) => Promise; uploadEvents: () => Promise; + clearEvents: () => Promise; } diff --git a/src/angular/components/view.component.ts b/src/angular/components/view.component.ts index f6985b977e7..79591889b86 100644 --- a/src/angular/components/view.component.ts +++ b/src/angular/components/view.component.ts @@ -47,6 +47,7 @@ export class ViewComponent implements OnDestroy, OnInit { checkPasswordPromise: Promise; private totpInterval: any; + private previousCipherId: string; constructor(protected cipherService: CipherService, protected totpService: TotpService, protected tokenService: TokenService, protected i18nService: I18nService, @@ -57,7 +58,6 @@ export class ViewComponent implements OnDestroy, OnInit { protected eventService: EventService) { } ngOnInit() { - this.eventService.collect(EventType.Cipher_ClientViewed); this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => { this.ngZone.run(async () => { switch (message.command) { @@ -94,6 +94,11 @@ export class ViewComponent implements OnDestroy, OnInit { await this.totpTick(interval); }, 1000); } + + if (this.previousCipherId !== this.cipherId) { + this.eventService.collect(EventType.Cipher_ClientViewed, this.cipherId); + } + this.previousCipherId = this.cipherId; } edit() { @@ -104,7 +109,7 @@ export class ViewComponent implements OnDestroy, OnInit { this.platformUtilsService.eventTrack('Toggled Password'); this.showPassword = !this.showPassword; if (this.showPassword) { - this.eventService.collect(EventType.Cipher_ClientToggledPasswordVisible); + this.eventService.collect(EventType.Cipher_ClientToggledPasswordVisible, this.cipherId); } } @@ -112,7 +117,7 @@ export class ViewComponent implements OnDestroy, OnInit { this.platformUtilsService.eventTrack('Toggled Card Code'); this.showCardCode = !this.showCardCode; if (this.showCardCode) { - this.eventService.collect(EventType.Cipher_ClientToggledCardCodeVisible); + this.eventService.collect(EventType.Cipher_ClientToggledCardCodeVisible, this.cipherId); } } @@ -137,7 +142,7 @@ export class ViewComponent implements OnDestroy, OnInit { const f = (field as any); f.showValue = !f.showValue; if (f.showValue) { - this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible); + this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible, this.cipherId); } } @@ -162,11 +167,11 @@ export class ViewComponent implements OnDestroy, OnInit { this.i18nService.t('valueCopied', this.i18nService.t(typeI18nKey))); if (typeI18nKey === 'password') { - this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible); + this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible, this.cipherId); } else if (typeI18nKey === 'securityCode') { - this.eventService.collect(EventType.Cipher_ClientCopiedCardCode); + this.eventService.collect(EventType.Cipher_ClientCopiedCardCode, this.cipherId); } else if (aType === 'H_Field') { - this.eventService.collect(EventType.Cipher_ClientCopiedHiddenField); + this.eventService.collect(EventType.Cipher_ClientCopiedHiddenField, this.cipherId); } } diff --git a/src/models/data/eventData.ts b/src/models/data/eventData.ts index 232fb8235c8..f8639ad89c5 100644 --- a/src/models/data/eventData.ts +++ b/src/models/data/eventData.ts @@ -3,4 +3,5 @@ import { EventType } from '../../enums/eventType'; export class EventData { type: EventType; cipherId: string; + date: string; } diff --git a/src/models/request/eventRequest.ts b/src/models/request/eventRequest.ts index f9f52d910e6..83bfa3902a6 100644 --- a/src/models/request/eventRequest.ts +++ b/src/models/request/eventRequest.ts @@ -3,4 +3,5 @@ import { EventType } from '../../enums/eventType'; export class EventRequest { type: EventType; cipherId: string; + date: string; } diff --git a/src/services/event.service.ts b/src/services/event.service.ts index b3935465541..316073d3e50 100644 --- a/src/services/event.service.ts +++ b/src/services/event.service.ts @@ -31,6 +31,10 @@ export class EventService implements EventServiceAbstraction { } async collect(eventType: EventType, cipherId: string = null, uploadImmediately = false): Promise { + const authed = await this.userService.isAuthenticated(); + if (!authed) { + return; + } const organizations = await this.userService.getAllOrganizations(); if (organizations == null) { return; @@ -52,6 +56,7 @@ export class EventService implements EventServiceAbstraction { const event = new EventData(); event.type = eventType; event.cipherId = cipherId; + event.date = new Date().toISOString(); eventCollection.push(event); await this.storageService.save(ConstantsService.eventCollectionKey, eventCollection); if (uploadImmediately) { @@ -60,6 +65,10 @@ export class EventService implements EventServiceAbstraction { } async uploadEvents(): Promise { + const authed = await this.userService.isAuthenticated(); + if (!authed) { + return; + } const eventCollection = await this.storageService.get(ConstantsService.eventCollectionKey); if (eventCollection == null || eventCollection.length === 0) { return; @@ -68,11 +77,16 @@ export class EventService implements EventServiceAbstraction { const req = new EventRequest(); req.type = e.type; req.cipherId = e.cipherId; + req.date = e.date; return req; }); try { await this.apiService.postEventsCollect(request); - await this.storageService.remove(ConstantsService.eventCollectionKey); + this.clearEvents(); } catch { } } + + async clearEvents(): Promise { + await this.storageService.remove(ConstantsService.eventCollectionKey); + } }