diff --git a/src/app/core/shared/search/search.service.ts b/src/app/core/shared/search/search.service.ts index 9befd8ea13..a88a8b0d16 100644 --- a/src/app/core/shared/search/search.service.ts +++ b/src/app/core/shared/search/search.service.ts @@ -333,8 +333,9 @@ export class SearchService implements OnDestroy { * Send search event to rest api using angularitics * @param config Paginated search options used * @param searchQueryResponse The response objects of the performed search + * @param clickedObject Optional UUID of an object a search was performed and clicked for */ - trackSearch(config: PaginatedSearchOptions, searchQueryResponse: SearchObjects) { + trackSearch(config: PaginatedSearchOptions, searchQueryResponse: SearchObjects, clickedObject?: string) { const filters: { filter: string, operator: string, value: string, label: string; }[] = []; const appliedFilters = searchQueryResponse.appliedFilters || []; for (let i = 0, filtersLength = appliedFilters.length; i < filtersLength; i++) { @@ -356,6 +357,7 @@ export class SearchService implements OnDestroy { order: config.sort.direction }, filters: filters, + clickedObject, }, }); } diff --git a/src/app/search-page/search-page.module.ts b/src/app/search-page/search-page.module.ts index 19fd9bd309..cc76d0427d 100644 --- a/src/app/search-page/search-page.module.ts +++ b/src/app/search-page/search-page.module.ts @@ -4,7 +4,6 @@ import { CoreModule } from '../core/core.module'; import { SharedModule } from '../shared/shared.module'; import { SidebarService } from '../shared/sidebar/sidebar.service'; import { ConfigurationSearchPageGuard } from './configuration-search-page.guard'; -import { SearchTrackerComponent } from './search-tracker.component'; import { StatisticsModule } from '../statistics/statistics.module'; import { SearchPageComponent } from './search-page.component'; import { SearchFilterService } from '../core/shared/search/search-filter.service'; @@ -16,7 +15,6 @@ import { SearchModule } from '../shared/search/search.module'; const components = [ SearchPageComponent, - SearchTrackerComponent, ThemedSearchPageComponent ]; diff --git a/src/app/search-page/search-tracker.component.html b/src/app/search-page/search-tracker.component.html deleted file mode 100644 index c0c0ffe181..0000000000 --- a/src/app/search-page/search-tracker.component.html +++ /dev/null @@ -1 +0,0 @@ -  diff --git a/src/app/search-page/search-tracker.component.scss b/src/app/search-page/search-tracker.component.scss deleted file mode 100644 index c76cafbe44..0000000000 --- a/src/app/search-page/search-tracker.component.scss +++ /dev/null @@ -1,3 +0,0 @@ -:host { - display: none -} diff --git a/src/app/search-page/search-tracker.component.ts b/src/app/search-page/search-tracker.component.ts deleted file mode 100644 index f766e6f669..0000000000 --- a/src/app/search-page/search-tracker.component.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { Component, Inject, OnInit } from '@angular/core'; -import { Angulartics2 } from 'angulartics2'; -import { map, switchMap } from 'rxjs/operators'; -import { SearchComponent } from '../shared/search/search.component'; -import { SidebarService } from '../shared/sidebar/sidebar.service'; -import { HostWindowService } from '../shared/host-window.service'; -import { SEARCH_CONFIG_SERVICE } from '../my-dspace-page/my-dspace-page.component'; -import { RouteService } from '../core/services/route.service'; -import { SearchConfigurationService } from '../core/shared/search/search-configuration.service'; -import { SearchService } from '../core/shared/search/search.service'; -import { PaginatedSearchOptions } from '../shared/search/models/paginated-search-options.model'; -import { SearchObjects } from '../shared/search/models/search-objects.model'; -import { Router } from '@angular/router'; -import { RemoteData } from '../core/data/remote-data'; -import { DSpaceObject } from '../core/shared/dspace-object.model'; -import { getFirstSucceededRemoteData } from '../core/shared/operators'; - -/** - * This component triggers a page view statistic - */ -@Component({ - selector: 'ds-search-tracker', - styleUrls: ['./search-tracker.component.scss'], - templateUrl: './search-tracker.component.html', - providers: [ - { - provide: SEARCH_CONFIG_SERVICE, - useClass: SearchConfigurationService - } - ] -}) -export class SearchTrackerComponent extends SearchComponent implements OnInit { - - constructor( - protected service: SearchService, - protected sidebarService: SidebarService, - protected windowService: HostWindowService, - @Inject(SEARCH_CONFIG_SERVICE) public searchConfigService: SearchConfigurationService, - protected routeService: RouteService, - public angulartics2: Angulartics2, - protected router: Router - ) { - super(service, sidebarService, windowService, searchConfigService, routeService, router); - } - - ngOnInit(): void { - // super.ngOnInit(); - this.getSearchOptions().pipe( - switchMap((options: PaginatedSearchOptions) => - this.service.searchEntries(options).pipe( - getFirstSucceededRemoteData(), - map((rd: RemoteData>) => ({ - config: options, - searchQueryResponse: rd.payload - })) - )), - ).subscribe(({ config, searchQueryResponse }) => { - 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, - }, - }); - }); - } -} diff --git a/src/app/shared/search/search.component.spec.ts b/src/app/shared/search/search.component.spec.ts index 3f00cf354f..d0d9bdda86 100644 --- a/src/app/shared/search/search.component.spec.ts +++ b/src/app/shared/search/search.component.spec.ts @@ -31,6 +31,8 @@ import { SearchObjects } from './models/search-objects.model'; import { DSpaceObject } from '../../core/shared/dspace-object.model'; import { SearchFilterConfig } from './models/search-filter-config.model'; import { FilterType } from './models/filter-type.model'; +import { getCommunityPageRoute } from '../../community-page/community-page-routing-paths'; +import { getCollectionPageRoute } from '../../collection-page/collection-page-routing-paths'; let comp: SearchComponent; let fixture: ComponentFixture; @@ -101,8 +103,9 @@ const searchServiceStub = jasmine.createSpyObj('SearchService', { search: mockResultsRD$, getSearchLink: '/search', getScopes: observableOf(['test-scope']), - getSearchConfigurationFor: createSuccessfulRemoteDataObject$(searchConfig) -}); + getSearchConfigurationFor: createSuccessfulRemoteDataObject$(searchConfig), + trackSearch: {}, +}) as SearchService; const configurationParam = 'default'; const queryParam = 'test query'; const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f'; @@ -327,4 +330,64 @@ describe('SearchComponent', () => { })); }); + + describe('getDsoUUIDFromUrl', () => { + let url: string; + let result: string; + + describe('when the navigated URL is an entity route', () => { + beforeEach(() => { + url = '/entities/publication/9a364471-3f19-4e7b-916a-a24a44ff48e3'; + result = (comp as any).getDsoUUIDFromUrl(url); + }); + + it('should return the UUID', () => { + expect(result).toEqual('9a364471-3f19-4e7b-916a-a24a44ff48e3'); + }); + }); + + describe('when the navigated URL is a community route', () => { + beforeEach(() => { + url = `${getCommunityPageRoute('9a364471-3f19-4e7b-916a-a24a44ff48e3')}`; + result = (comp as any).getDsoUUIDFromUrl(url); + }); + + it('should return the UUID', () => { + expect(result).toEqual('9a364471-3f19-4e7b-916a-a24a44ff48e3'); + }); + }); + + describe('when the navigated URL is a collection route', () => { + beforeEach(() => { + url = `${getCollectionPageRoute('9a364471-3f19-4e7b-916a-a24a44ff48e3')}`; + result = (comp as any).getDsoUUIDFromUrl(url); + }); + + it('should return the UUID', () => { + expect(result).toEqual('9a364471-3f19-4e7b-916a-a24a44ff48e3'); + }); + }); + + describe('when the navigated URL is an item route', () => { + beforeEach(() => { + url = '/items/9a364471-3f19-4e7b-916a-a24a44ff48e3'; + result = (comp as any).getDsoUUIDFromUrl(url); + }); + + it('should return the UUID', () => { + expect(result).toEqual('9a364471-3f19-4e7b-916a-a24a44ff48e3'); + }); + }); + + describe('when the navigated URL is an invalid route', () => { + beforeEach(() => { + url = '/invalid/object/route/9a364471-3f19-4e7b-916a-a24a44ff48e3'; + result = (comp as any).getDsoUUIDFromUrl(url); + }); + + it('should return null', () => { + expect(result).toBeNull(); + }); + }); + }); }); diff --git a/src/app/shared/search/search.component.ts b/src/app/shared/search/search.component.ts index a5b9fb9c7d..2195d428c9 100644 --- a/src/app/shared/search/search.component.ts +++ b/src/app/shared/search/search.component.ts @@ -1,5 +1,5 @@ import { ChangeDetectionStrategy, Component, EventEmitter, Inject, Input, OnInit, Output } from '@angular/core'; -import { Router } from '@angular/router'; +import { NavigationStart, Router } from '@angular/router'; import { BehaviorSubject, combineLatest, Observable, Subscription } from 'rxjs'; import { debounceTime, distinctUntilChanged, filter, map, switchMap } from 'rxjs/operators'; @@ -11,7 +11,7 @@ import { DSpaceObject } from '../../core/shared/dspace-object.model'; import { pushInOut } from '../animations/push'; import { HostWindowService } from '../host-window.service'; import { SidebarService } from '../sidebar/sidebar.service'; -import { hasValue } from '../empty.util'; +import { hasValue, hasValueOperator, isNotEmpty } from '../empty.util'; import { RouteService } from '../../core/services/route.service'; import { SEARCH_CONFIG_SERVICE } from '../../my-dspace-page/my-dspace-page.component'; import { PaginatedSearchOptions } from './models/paginated-search-options.model'; @@ -35,6 +35,9 @@ import { environment } from 'src/environments/environment'; import { SubmissionObject } from '../../core/submission/models/submission-object.model'; import { SearchFilterConfig } from './models/search-filter-config.model'; import { WorkspaceItem } from '../..//core/submission/models/workspaceitem.model'; +import { ITEM_MODULE_PATH } from '../../item-page/item-page-routing-paths'; +import { COLLECTION_MODULE_PATH } from '../../collection-page/collection-page-routing-paths'; +import { COMMUNITY_MODULE_PATH } from '../../community-page/community-page-routing-paths'; @Component({ selector: 'ds-search', @@ -229,9 +232,21 @@ export class SearchComponent implements OnInit { searchLink: string; /** - * Subscription to unsubscribe from + * Regex to match UUIDs */ - sub: Subscription; + uuidRegex = /\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b/g; + + /** + * List of paths that are considered to be the start of a route to an object page (excluding "/", e.g. "items") + * These are expected to end on an object UUID + * If they match the route we're navigating to, an object property will be added to the search event sent + */ + allowedObjectPaths: string[] = ['entities', ITEM_MODULE_PATH, COLLECTION_MODULE_PATH, COMMUNITY_MODULE_PATH]; + + /** + * Subscriptions to unsubscribe from + */ + subs: Subscription[] = []; /** * Emits an event with the current search result entries @@ -301,7 +316,7 @@ export class SearchComponent implements OnInit { ); const searchOptions$: Observable = this.getSearchOptions().pipe(distinctUntilChanged()); - this.sub = combineLatest([configuration$, searchSortOptions$, searchOptions$, sortOption$]).pipe( + this.subs.push(combineLatest([configuration$, searchSortOptions$, searchOptions$, sortOption$]).pipe( filter(([configuration, searchSortOptions, searchOptions, sortOption]: [string, SortOptions[], PaginatedSearchOptions, SortOptions]) => { // filter for search options related to instanced paginated id return searchOptions.pagination.id === this.paginationId; @@ -332,7 +347,9 @@ export class SearchComponent implements OnInit { this.retrieveSearchResults(newSearchOptions); this.retrieveFilters(searchOptions); } - }); + })); + + this.subscribeToRoutingEvents(); } /** @@ -374,12 +391,10 @@ export class SearchComponent implements OnInit { } /** - * Unsubscribe from the subscription + * Unsubscribe from the subscriptions */ ngOnDestroy(): void { - if (hasValue(this.sub)) { - this.sub.unsubscribe(); - } + this.subs.filter((sub) => hasValue(sub)).forEach((sub) => sub.unsubscribe()); } /** @@ -440,6 +455,43 @@ export class SearchComponent implements OnInit { }); } + /** + * Subscribe to routing events to detect when a user moves away from the search page + * When the user is routing to an object page, it needs to send out a separate search event containing that object's UUID + * This method should only be called once and is essentially what SearchTrackingComponent used to do (now removed) + * @private + */ + private subscribeToRoutingEvents() { + this.subs.push( + this.router.events.pipe( + filter((event) => event instanceof NavigationStart), + map((event: NavigationStart) => this.getDsoUUIDFromUrl(event.url)), + hasValueOperator(), + ).subscribe((uuid) => { + if (this.resultsRD$.value.hasSucceeded) { + this.service.trackSearch(this.searchOptions$.value, this.resultsRD$.value.payload as SearchObjects, uuid); + } + }), + ); + } + + /** + * Get the UUID from a DSO url + * Return null if the url isn't an object page (allowedObjectPaths) or the UUID couldn't be found + * @param url + */ + private getDsoUUIDFromUrl(url: string): string { + if (isNotEmpty(url)) { + if (this.allowedObjectPaths.some((path) => url.startsWith(`/${path}`))) { + const uuid = url.substring(url.lastIndexOf('/') + 1); + if (uuid.match(this.uuidRegex)) { + return uuid; + } + } + } + return null; + } + /** * Check if the sidebar is collapsed * @returns {Observable} emits true if the sidebar is currently collapsed, false if it is expanded diff --git a/src/app/statistics/angulartics/dspace-provider.ts b/src/app/statistics/angulartics/dspace-provider.ts index 6efa67f92a..d5e09f06d2 100644 --- a/src/app/statistics/angulartics/dspace-provider.ts +++ b/src/app/statistics/angulartics/dspace-provider.ts @@ -31,7 +31,8 @@ export class Angulartics2DSpace { event.properties.searchOptions, event.properties.page, event.properties.sort, - event.properties.filters + event.properties.filters, + event.properties.clickedObject, ); } } diff --git a/src/app/statistics/statistics.service.ts b/src/app/statistics/statistics.service.ts index a5888e1455..aa706967a7 100644 --- a/src/app/statistics/statistics.service.ts +++ b/src/app/statistics/statistics.service.ts @@ -45,12 +45,14 @@ export class StatisticsService { * @param page: An object that describes the pagination status * @param sort: An object that describes the sort status * @param filters: An array of search filters used to filter the result set + * @param clickedObject: UUID of object clicked */ trackSearchEvent( searchOptions: SearchOptions, page: { size: number, totalElements: number, totalPages: number, number: number }, sort: { by: string, order: string }, - filters?: { filter: string, operator: string, value: string, label: string }[] + filters?: { filter: string, operator: string, value: string, label: string }[], + clickedObject?: string, ) { const body = { query: searchOptions.query, @@ -87,6 +89,9 @@ export class StatisticsService { } Object.assign(body, { appliedFilters: bodyFilters }); } + if (hasValue(clickedObject)) { + Object.assign(body, { clickedObject }); + } this.sendEvent('/statistics/searchevents', body); }