From 24189c4ce0c0403c98ed7ea1e2a43034097c3056 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Thu, 8 Sep 2022 14:53:22 +0200 Subject: [PATCH] 94310: Track search with service method instead of tracking component --- .../core/shared/search/search.service.spec.ts | 3 ++ src/app/core/shared/search/search.service.ts | 37 ++++++++++++++++++- .../search-page/search-page.component.html | 3 +- src/app/shared/search/search.component.ts | 14 ++++++- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/app/core/shared/search/search.service.spec.ts b/src/app/core/shared/search/search.service.spec.ts index f9b768655e..1b5717ab4b 100644 --- a/src/app/core/shared/search/search.service.spec.ts +++ b/src/app/core/shared/search/search.service.spec.ts @@ -25,6 +25,7 @@ import { SearchObjects } from '../../../shared/search/models/search-objects.mode import { PaginationService } from '../../pagination/pagination.service'; import { SearchConfigurationService } from './search-configuration.service'; import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub'; +import { Angulartics2 } from 'angulartics2'; @Component({ template: '' }) class DummyComponent { @@ -57,6 +58,7 @@ describe('SearchService', () => { { provide: DSpaceObjectDataService, useValue: {} }, { provide: PaginationService, useValue: {} }, { provide: SearchConfigurationService, useValue: searchConfigService }, + { provide: Angulartics2, useValue: {} }, SearchService ], }); @@ -124,6 +126,7 @@ describe('SearchService', () => { { provide: DSpaceObjectDataService, useValue: {} }, { provide: PaginationService, useValue: paginationService }, { provide: SearchConfigurationService, useValue: searchConfigService }, + { provide: Angulartics2, useValue: {} }, SearchService ], }); diff --git a/src/app/core/shared/search/search.service.ts b/src/app/core/shared/search/search.service.ts index f70416594d..47ece4f289 100644 --- a/src/app/core/shared/search/search.service.ts +++ b/src/app/core/shared/search/search.service.ts @@ -43,6 +43,7 @@ import { ObjectCacheService } from '../../cache/object-cache.service'; import { NotificationsService } from '../../../shared/notifications/notifications.service'; import { HttpClient } from '@angular/common/http'; import { DSOChangeAnalyzer } from '../../data/dso-change-analyzer.service'; +import { Angulartics2 } from 'angulartics2'; /* tslint:disable:max-classes-per-file */ /** @@ -124,7 +125,8 @@ export class SearchService implements OnDestroy { private communityService: CommunityDataService, private dspaceObjectService: DSpaceObjectDataService, private paginationService: PaginationService, - private searchConfigurationService: SearchConfigurationService + private searchConfigurationService: SearchConfigurationService, + private angulartics2: Angulartics2, ) { this.searchDataService = new DataServiceImpl( undefined, @@ -134,7 +136,7 @@ export class SearchService implements OnDestroy { undefined, undefined, undefined, - undefined + undefined, ); } @@ -442,6 +444,37 @@ export class SearchService implements OnDestroy { return this.rdb.buildFromHref(href$); } + /** + * Send search event to rest api using angularitics + * @param config Paginated search options used + * @param searchQueryResponse The response objects of the performed search + */ + trackSearch(config: PaginatedSearchOptions, searchQueryResponse: SearchObjects) { + const filters: { filter: string, operator: string, value: string, label: string; }[] = []; + const appliedFilters = searchQueryResponse.appliedFilters || []; + for (let i = 0, filtersLength = appliedFilters.length; i < filtersLength; i++) { + const appliedFilter = appliedFilters[i]; + filters.push(appliedFilter); + } + this.angulartics2.eventTrack.next({ + action: 'search', + properties: { + searchOptions: config, + page: { + size: config.pagination.size, // same as searchQueryResponse.page.elementsPerPage + totalElements: searchQueryResponse.pageInfo.totalElements, + totalPages: searchQueryResponse.pageInfo.totalPages, + number: config.pagination.currentPage, // same as searchQueryResponse.page.currentPage + }, + sort: { + by: config.sort.field, + order: config.sort.direction + }, + filters: filters, + }, + }); + } + /** * @returns {string} The base path to the search page */ diff --git a/src/app/search-page/search-page.component.html b/src/app/search-page/search-page.component.html index 5143d22809..2b2d4fab33 100644 --- a/src/app/search-page/search-page.component.html +++ b/src/app/search-page/search-page.component.html @@ -1,2 +1 @@ - - + diff --git a/src/app/shared/search/search.component.ts b/src/app/shared/search/search.component.ts index c017a5065b..cc242992b2 100644 --- a/src/app/shared/search/search.component.ts +++ b/src/app/shared/search/search.component.ts @@ -134,6 +134,11 @@ export class SearchComponent implements OnInit { */ @Input() viewModeList: ViewMode[]; + /** + * Whether or not to track search statistics by sending updates to the rest api + */ + @Input() trackStatistics = false; + /** * The current configuration used during the search */ @@ -353,8 +358,13 @@ export class SearchComponent implements OnInit { followLink('thumbnail', { isOptional: true }) ).pipe(getFirstCompletedRemoteData()) .subscribe((results: RemoteData>) => { - if (results.hasSucceeded && results.payload?.page?.length > 0) { - this.resultFound.emit(results.payload); + if (results.hasSucceeded) { + if (this.trackStatistics) { + this.service.trackSearch(searchOptions, results.payload); + } + if (results.payload?.page?.length > 0) { + this.resultFound.emit(results.payload); + } } this.resultsRD$.next(results); });