diff --git a/src/app/core/services/browser.referrer.service.spec.ts b/src/app/core/services/browser.referrer.service.spec.ts new file mode 100644 index 0000000000..9dc8f466b6 --- /dev/null +++ b/src/app/core/services/browser.referrer.service.spec.ts @@ -0,0 +1,68 @@ +import { of as observableOf } from 'rxjs'; +import { RouteService } from './route.service'; +import { BrowserReferrerService } from './browser.referrer.service'; + +describe(`BrowserReferrerService`, () => { + let service: BrowserReferrerService; + const documentReferrer = 'https://www.referrer.com'; + const origin = 'https://www.dspace.org'; + let routeService: RouteService; + + beforeEach(() => { + routeService = { + getHistory: () => observableOf([]) + } as any; + service = new BrowserReferrerService( + { referrer: documentReferrer }, + routeService, + { getCurrentOrigin: () => origin } as any + ); + }); + + describe(`getReferrer`, () => { + describe(`when the history is an empty`, () => { + beforeEach(() => { + spyOn(routeService, 'getHistory').and.returnValue(observableOf([])); + }); + + it(`should return document.referrer`, (done: DoneFn) => { + service.getReferrer().subscribe((emittedReferrer: string) => { + expect(emittedReferrer).toBe(documentReferrer); + done(); + }); + }); + }); + + describe(`when the history only contains the current route`, () => { + beforeEach(() => { + spyOn(routeService, 'getHistory').and.returnValue(observableOf(['/current/route'])); + }); + + it(`should return document.referrer`, (done: DoneFn) => { + service.getReferrer().subscribe((emittedReferrer: string) => { + expect(emittedReferrer).toBe(documentReferrer); + done(); + }); + }); + }); + + describe(`when the history contains multiple routes`, () => { + const prevUrl = '/the/route/we/need'; + beforeEach(() => { + spyOn(routeService, 'getHistory').and.returnValue(observableOf([ + '/first/route', + '/second/route', + prevUrl, + '/current/route' + ])); + }); + + it(`should return the last route before the current one combined with the origin from HardRedirectService`, (done: DoneFn) => { + service.getReferrer().subscribe((emittedReferrer: string) => { + expect(emittedReferrer).toBe(origin + prevUrl); + done(); + }); + }); + }); + }); +}); diff --git a/src/app/core/services/browser.referrer.service.ts b/src/app/core/services/browser.referrer.service.ts new file mode 100644 index 0000000000..64be95d241 --- /dev/null +++ b/src/app/core/services/browser.referrer.service.ts @@ -0,0 +1,54 @@ +import { ReferrerService } from './referrer.service'; +import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; +import { hasNoValue } from '../../shared/empty.util'; +import { URLCombiner } from '../url-combiner/url-combiner'; +import { Inject, Injectable } from '@angular/core'; +import { DOCUMENT } from '@angular/common'; +import { HardRedirectService } from './hard-redirect.service'; +import { RouteService } from './route.service'; + +/** + * A service to determine the referrer + * + * The browser implementation will get the referrer from document.referrer, in the event that the + * previous page visited was not an angular URL. If it was, the route history in the store must be + * used, since document.referrer doesn't get updated on route changes + */ +@Injectable() +export class BrowserReferrerService extends ReferrerService { + + constructor( + @Inject(DOCUMENT) protected document: any, + protected routeService: RouteService, + protected hardRedirectService: HardRedirectService, + ) { + super(); + } + + /** + * Return the referrer + * + * Return the referrer URL based on the route history in the store. If there is no route history + * in the store yet, document.referrer will be used + */ + public getReferrer(): Observable { + return this.routeService.getHistory().pipe( + map((history: string[]) => { + const currentURL = history[history.length - 1]; + // if the current URL isn't set yet, or the only URL in the history is the current one, + // return document.referrer (note that that may be empty too, e.g. if you've just opened a + // new browser tab) + if (hasNoValue(currentURL) || history.every((url: string) => url === currentURL)) { + return this.document.referrer; + } else { + // reverse the history + const reversedHistory = [...history].reverse(); + // and find the first URL that differs from the current one + const prevUrl = reversedHistory.find((url: string) => url !== currentURL); + return new URLCombiner(this.hardRedirectService.getCurrentOrigin(), prevUrl).toString(); + } + }) + ); + } +} diff --git a/src/app/core/services/referrer.service.ts b/src/app/core/services/referrer.service.ts new file mode 100644 index 0000000000..79621fdbeb --- /dev/null +++ b/src/app/core/services/referrer.service.ts @@ -0,0 +1,15 @@ +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs'; + +/** + * A service to determine the referrer, i.e. the previous URL that led the user to the current one + */ +@Injectable() +export abstract class ReferrerService { + + /** + * Return the referrer + */ + abstract getReferrer(): Observable; + +} diff --git a/src/app/core/services/server.referrer.service.spec.ts b/src/app/core/services/server.referrer.service.spec.ts new file mode 100644 index 0000000000..4a7f841093 --- /dev/null +++ b/src/app/core/services/server.referrer.service.spec.ts @@ -0,0 +1,34 @@ +import { ServerReferrerService } from './server.referrer.service'; + +describe(`ServerReferrerService`, () => { + let service: ServerReferrerService; + const referrer = 'https://www.referrer.com'; + + describe(`getReferrer`, () => { + describe(`when the referer header is set`, () => { + beforeEach(() => { + service = new ServerReferrerService({ headers: { referer: referrer }}); + }); + + it(`should return the referer header`, (done: DoneFn) => { + service.getReferrer().subscribe((emittedReferrer: string) => { + expect(emittedReferrer).toBe(referrer); + done(); + }); + }); + }); + + describe(`when the referer header is not set`, () => { + beforeEach(() => { + service = new ServerReferrerService({ headers: {}}); + }); + + it(`should return an empty string`, (done: DoneFn) => { + service.getReferrer().subscribe((emittedReferrer: string) => { + expect(emittedReferrer).toBe(''); + done(); + }); + }); + }); + }); +}); diff --git a/src/app/core/services/server.referrer.service.ts b/src/app/core/services/server.referrer.service.ts new file mode 100644 index 0000000000..b2f063a07c --- /dev/null +++ b/src/app/core/services/server.referrer.service.ts @@ -0,0 +1,31 @@ +import { ReferrerService } from './referrer.service'; +import { Observable, of as observableOf } from 'rxjs'; +import { Inject, Injectable } from '@angular/core'; +import { REQUEST } from '@nguniversal/express-engine/tokens'; + +/** + * A service to determine the referrer + * + * The server implementation will get the referrer from the 'Referer' header of the request sent to + * the express server + */ +@Injectable() +export class ServerReferrerService extends ReferrerService { + + constructor( + @Inject(REQUEST) protected request: any, + ) { + super(); + } + + /** + * Return the referrer + * + * Return the 'Referer' header from the request, or an empty string if the header wasn't set + * (for consistency with the document.referrer property on the browser side) + */ + public getReferrer(): Observable { + const referrer = this.request.headers.referer || ''; + return observableOf(referrer); + } +} diff --git a/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal-issue/journal-issue-search-result-grid-element.component.html b/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal-issue/journal-issue-search-result-grid-element.component.html index c6a2f5bcc3..13c5286e71 100644 --- a/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal-issue/journal-issue-search-result-grid-element.component.html +++ b/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal-issue/journal-issue-search-result-grid-element.component.html @@ -5,7 +5,7 @@
@@ -36,7 +36,7 @@

diff --git a/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal-volume/journal-volume-search-result-grid-element.component.html b/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal-volume/journal-volume-search-result-grid-element.component.html index b7e1330c86..c64da0b632 100644 --- a/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal-volume/journal-volume-search-result-grid-element.component.html +++ b/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal-volume/journal-volume-search-result-grid-element.component.html @@ -5,7 +5,7 @@
@@ -36,7 +36,7 @@

diff --git a/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal/journal-search-result-grid-element.component.html b/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal/journal-search-result-grid-element.component.html index e4c9ada3ca..9037e364d8 100644 --- a/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal/journal-search-result-grid-element.component.html +++ b/src/app/entity-groups/journal-entities/item-grid-elements/search-result-grid-elements/journal/journal-search-result-grid-element.component.html @@ -5,7 +5,7 @@
@@ -40,7 +40,7 @@

diff --git a/src/app/entity-groups/journal-entities/item-list-elements/search-result-list-elements/journal-issue/journal-issue-search-result-list-element.component.html b/src/app/entity-groups/journal-entities/item-list-elements/search-result-list-elements/journal-issue/journal-issue-search-result-list-element.component.html index 9d02a5d837..e3293be3a0 100644 --- a/src/app/entity-groups/journal-entities/item-list-elements/search-result-list-elements/journal-issue/journal-issue-search-result-list-element.component.html +++ b/src/app/entity-groups/journal-entities/item-list-elements/search-result-list-elements/journal-issue/journal-issue-search-result-list-element.component.html @@ -1,7 +1,7 @@
@@ -15,7 +15,7 @@
@@ -15,7 +15,7 @@
- @@ -13,7 +13,7 @@
- diff --git a/src/app/entity-groups/research-entities/item-grid-elements/search-result-grid-elements/person/person-search-result-grid-element.component.html b/src/app/entity-groups/research-entities/item-grid-elements/search-result-grid-elements/person/person-search-result-grid-element.component.html index fe26fd7063..0cbe19c1e0 100644 --- a/src/app/entity-groups/research-entities/item-grid-elements/search-result-grid-elements/person/person-search-result-grid-element.component.html +++ b/src/app/entity-groups/research-entities/item-grid-elements/search-result-grid-elements/person/person-search-result-grid-element.component.html @@ -5,7 +5,7 @@
diff --git a/src/app/entity-groups/research-entities/item-grid-elements/search-result-grid-elements/project/project-search-result-grid-element.component.html b/src/app/entity-groups/research-entities/item-grid-elements/search-result-grid-elements/project/project-search-result-grid-element.component.html index cf47f947cc..97d4016ec4 100644 --- a/src/app/entity-groups/research-entities/item-grid-elements/search-result-grid-elements/project/project-search-result-grid-element.component.html +++ b/src/app/entity-groups/research-entities/item-grid-elements/search-result-grid-elements/project/project-search-result-grid-element.component.html @@ -5,7 +5,7 @@
diff --git a/src/app/entity-groups/research-entities/item-list-elements/search-result-list-elements/org-unit/org-unit-search-result-list-element.component.html b/src/app/entity-groups/research-entities/item-list-elements/search-result-list-elements/org-unit/org-unit-search-result-list-element.component.html index 9495577c01..07c7c5bb89 100644 --- a/src/app/entity-groups/research-entities/item-list-elements/search-result-list-elements/org-unit/org-unit-search-result-list-element.component.html +++ b/src/app/entity-groups/research-entities/item-list-elements/search-result-list-elements/org-unit/org-unit-search-result-list-element.component.html @@ -1,7 +1,7 @@
diff --git a/src/app/shared/object-grid/community-grid-element/community-grid-element.component.html b/src/app/shared/object-grid/community-grid-element/community-grid-element.component.html index c448c70991..8bb3763a2b 100644 --- a/src/app/shared/object-grid/community-grid-element/community-grid-element.component.html +++ b/src/app/shared/object-grid/community-grid-element/community-grid-element.component.html @@ -1,5 +1,5 @@
- + @@ -11,7 +11,7 @@

{{ dsoNameService.getName(object) }}

{{object.shortDescription}}

diff --git a/src/app/shared/object-grid/search-result-grid-element/collection-search-result/collection-search-result-grid-element.component.html b/src/app/shared/object-grid/search-result-grid-element/collection-search-result/collection-search-result-grid-element.component.html index 1e46f11144..b00e67679a 100644 --- a/src/app/shared/object-grid/search-result-grid-element/collection-search-result/collection-search-result-grid-element.component.html +++ b/src/app/shared/object-grid/search-result-grid-element/collection-search-result/collection-search-result-grid-element.component.html @@ -1,5 +1,5 @@
- + @@ -12,7 +12,7 @@

{{ dsoNameService.getName(dso) }}

{{dso.shortDescription}}

diff --git a/src/app/shared/object-grid/search-result-grid-element/community-search-result/community-search-result-grid-element.component.html b/src/app/shared/object-grid/search-result-grid-element/community-search-result/community-search-result-grid-element.component.html index 96e10156c3..478db93c34 100644 --- a/src/app/shared/object-grid/search-result-grid-element/community-search-result/community-search-result-grid-element.component.html +++ b/src/app/shared/object-grid/search-result-grid-element/community-search-result/community-search-result-grid-element.component.html @@ -1,5 +1,5 @@
- + @@ -12,7 +12,7 @@

{{ dsoNameService.getName(dso) }}

{{dso.shortDescription}}

diff --git a/src/app/shared/object-grid/search-result-grid-element/item-search-result/item/item-search-result-grid-element.component.html b/src/app/shared/object-grid/search-result-grid-element/item-search-result/item/item-search-result-grid-element.component.html index 3d6e251238..3e6efbf765 100644 --- a/src/app/shared/object-grid/search-result-grid-element/item-search-result/item/item-search-result-grid-element.component.html +++ b/src/app/shared/object-grid/search-result-grid-element/item-search-result/item/item-search-result-grid-element.component.html @@ -2,7 +2,7 @@
- diff --git a/src/app/shared/object-list/browse-entry-list-element/browse-entry-list-element.component.html b/src/app/shared/object-list/browse-entry-list-element/browse-entry-list-element.component.html index dcbdd77bff..ff7efcc309 100644 --- a/src/app/shared/object-list/browse-entry-list-element/browse-entry-list-element.component.html +++ b/src/app/shared/object-list/browse-entry-list-element/browse-entry-list-element.component.html @@ -1,5 +1,5 @@
- + {{object.value}} diff --git a/src/app/shared/object-list/collection-list-element/collection-list-element.component.html b/src/app/shared/object-list/collection-list-element/collection-list-element.component.html index 83d88b6a63..707adabde2 100644 --- a/src/app/shared/object-list/collection-list-element/collection-list-element.component.html +++ b/src/app/shared/object-list/collection-list-element/collection-list-element.component.html @@ -1,4 +1,4 @@ - + {{ dsoNameService.getName(object) }} diff --git a/src/app/shared/object-list/community-list-element/community-list-element.component.html b/src/app/shared/object-list/community-list-element/community-list-element.component.html index 8ade44a4df..ea6782b949 100644 --- a/src/app/shared/object-list/community-list-element/community-list-element.component.html +++ b/src/app/shared/object-list/community-list-element/community-list-element.component.html @@ -1,4 +1,4 @@ - + {{ dsoNameService.getName(object) }} diff --git a/src/app/shared/object-list/search-result-list-element/collection-search-result/collection-search-result-list-element.component.html b/src/app/shared/object-list/search-result-list-element/collection-search-result/collection-search-result-list-element.component.html index 6856c63cce..0bcb890f7d 100644 --- a/src/app/shared/object-list/search-result-list-element/collection-search-result/collection-search-result-list-element.component.html +++ b/src/app/shared/object-list/search-result-list-element/collection-search-result/collection-search-result-list-element.component.html @@ -2,7 +2,7 @@
- +
diff --git a/src/app/shared/object-list/search-result-list-element/community-search-result/community-search-result-list-element.component.html b/src/app/shared/object-list/search-result-list-element/community-search-result/community-search-result-list-element.component.html index 037b62e736..d38170e863 100644 --- a/src/app/shared/object-list/search-result-list-element/community-search-result/community-search-result-list-element.component.html +++ b/src/app/shared/object-list/search-result-list-element/community-search-result/community-search-result-list-element.component.html @@ -2,7 +2,7 @@
- +
diff --git a/src/app/shared/object-list/search-result-list-element/item-search-result/item-types/item/item-search-result-list-element.component.html b/src/app/shared/object-list/search-result-list-element/item-search-result/item-types/item/item-search-result-list-element.component.html index 88d6ab6e07..f7a687048a 100644 --- a/src/app/shared/object-list/search-result-list-element/item-search-result/item-types/item/item-search-result-list-element.component.html +++ b/src/app/shared/object-list/search-result-list-element/item-search-result/item-types/item/item-search-result-list-element.component.html @@ -1,6 +1,6 @@
- { beforeEach(() => { angulartics2 = { - eventTrack: observableOf({action: 'page_view', properties: {object: 'mock-object'}}), + eventTrack: observableOf({action: 'page_view', properties: { + object: 'mock-object', + referrer: 'https://www.referrer.com' + }}), filterDeveloperMode: () => filter(() => true) } as any; statisticsService = jasmine.createSpyObj('statisticsService', {trackViewEvent: null}); @@ -20,7 +23,7 @@ describe('Angulartics2DSpace', () => { it('should use the statisticsService', () => { provider.startTracking(); - expect(statisticsService.trackViewEvent).toHaveBeenCalledWith('mock-object' as any); + expect(statisticsService.trackViewEvent).toHaveBeenCalledWith('mock-object' as any, 'https://www.referrer.com'); }); }); diff --git a/src/app/statistics/angulartics/dspace-provider.ts b/src/app/statistics/angulartics/dspace-provider.ts index d5e09f06d2..86c16b5c01 100644 --- a/src/app/statistics/angulartics/dspace-provider.ts +++ b/src/app/statistics/angulartics/dspace-provider.ts @@ -25,7 +25,7 @@ export class Angulartics2DSpace { private eventTrack(event) { if (event.action === 'page_view') { - this.statisticsService.trackViewEvent(event.properties.object); + this.statisticsService.trackViewEvent(event.properties.object, event.properties.referrer); } else if (event.action === 'search') { this.statisticsService.trackSearchEvent( event.properties.searchOptions, diff --git a/src/app/statistics/angulartics/dspace/view-tracker.component.ts b/src/app/statistics/angulartics/dspace/view-tracker.component.ts index d12b6c2f69..805d311cfd 100644 --- a/src/app/statistics/angulartics/dspace/view-tracker.component.ts +++ b/src/app/statistics/angulartics/dspace/view-tracker.component.ts @@ -1,6 +1,10 @@ -import { Component, Input, OnInit } from '@angular/core'; +import { Component, Input, OnInit, OnDestroy } from '@angular/core'; import { Angulartics2 } from 'angulartics2'; import { DSpaceObject } from '../../../core/shared/dspace-object.model'; +import { Subscription } from 'rxjs/internal/Subscription'; +import { take } from 'rxjs/operators'; +import { hasValue } from '../../../shared/empty.util'; +import { ReferrerService } from '../../../core/services/referrer.service'; /** * This component triggers a page view statistic @@ -10,18 +14,43 @@ import { DSpaceObject } from '../../../core/shared/dspace-object.model'; styleUrls: ['./view-tracker.component.scss'], templateUrl: './view-tracker.component.html', }) -export class ViewTrackerComponent implements OnInit { +export class ViewTrackerComponent implements OnInit, OnDestroy { + /** + * The DSpaceObject to track a view event about + */ @Input() object: DSpaceObject; + /** + * The subscription on this.referrerService.getReferrer() + * @protected + */ + protected sub: Subscription; + constructor( - public angulartics2: Angulartics2 + public angulartics2: Angulartics2, + public referrerService: ReferrerService ) { } ngOnInit(): void { - this.angulartics2.eventTrack.next({ - action: 'page_view', - properties: {object: this.object}, - }); + this.sub = this.referrerService.getReferrer() + .pipe(take(1)) + .subscribe((referrer: string) => { + this.angulartics2.eventTrack.next({ + action: 'page_view', + properties: { + object: this.object, + referrer + }, + }); + }); + } + + ngOnDestroy(): void { + // unsubscribe in the case that this component is destroyed before + // this.referrerService.getReferrer() has emitted + if (hasValue(this.sub)) { + this.sub.unsubscribe(); + } } } diff --git a/src/app/statistics/statistics.service.spec.ts b/src/app/statistics/statistics.service.spec.ts index b573daf476..45e954c51c 100644 --- a/src/app/statistics/statistics.service.spec.ts +++ b/src/app/statistics/statistics.service.spec.ts @@ -26,12 +26,13 @@ describe('StatisticsService', () => { it('should send a request to track an item view ', () => { const mockItem: any = {uuid: 'mock-item-uuid', type: 'item'}; - service.trackViewEvent(mockItem); + service.trackViewEvent(mockItem, 'https://www.referrer.com'); const request: RestRequest = requestService.send.calls.mostRecent().args[0]; expect(request.body).toBeDefined('request.body'); const body = JSON.parse(request.body); expect(body.targetId).toBe('mock-item-uuid'); expect(body.targetType).toBe('item'); + expect(body.referrer).toBe('https://www.referrer.com'); }); }); diff --git a/src/app/statistics/statistics.service.ts b/src/app/statistics/statistics.service.ts index aa706967a7..56181d233c 100644 --- a/src/app/statistics/statistics.service.ts +++ b/src/app/statistics/statistics.service.ts @@ -31,11 +31,16 @@ export class StatisticsService { /** * To track a page view * @param dso: The dso which was viewed + * @param referrer: The referrer used by the client to reach the dso page */ - trackViewEvent(dso: DSpaceObject) { + trackViewEvent( + dso: DSpaceObject, + referrer: string + ) { this.sendEvent('/statistics/viewevents', { targetId: dso.uuid, - targetType: (dso as any).type + targetType: (dso as any).type, + referrer }); } diff --git a/src/modules/app/browser-app.module.ts b/src/modules/app/browser-app.module.ts index 68e328a8d5..d6295cf791 100644 --- a/src/modules/app/browser-app.module.ts +++ b/src/modules/app/browser-app.module.ts @@ -31,6 +31,8 @@ import { GoogleAnalyticsService } from '../../app/statistics/google-analytics.se import { AuthRequestService } from '../../app/core/auth/auth-request.service'; import { BrowserAuthRequestService } from '../../app/core/auth/browser-auth-request.service'; import { BrowserInitService } from './browser-init.service'; +import { ReferrerService } from '../../app/core/services/referrer.service'; +import { BrowserReferrerService } from '../../app/core/services/browser.referrer.service'; export const REQ_KEY = makeStateKey('req'); @@ -107,6 +109,10 @@ export function getRequest(transferState: TransferState): any { provide: AuthRequestService, useClass: BrowserAuthRequestService, }, + { + provide: ReferrerService, + useClass: BrowserReferrerService, + }, { provide: LocationToken, useFactory: locationProvider, diff --git a/src/modules/app/server-app.module.ts b/src/modules/app/server-app.module.ts index 7d162c5fd1..b3d718b0b2 100644 --- a/src/modules/app/server-app.module.ts +++ b/src/modules/app/server-app.module.ts @@ -35,6 +35,8 @@ import { ServerAuthRequestService } from '../../app/core/auth/server-auth-reques import { ServerInitService } from './server-init.service'; import { XhrFactory } from '@angular/common'; import { ServerXhrService } from '../../app/core/services/server-xhr.service'; +import { ReferrerService } from '../../app/core/services/referrer.service'; +import { ServerReferrerService } from '../../app/core/services/server.referrer.service'; export function createTranslateLoader(transferState: TransferState) { return new TranslateServerLoader(transferState, 'dist/server/assets/i18n/', '.json'); @@ -110,6 +112,10 @@ export function createTranslateLoader(transferState: TransferState) { provide: XhrFactory, useClass: ServerXhrService, }, + { + provide: ReferrerService, + useClass: ServerReferrerService, + }, ] }) export class ServerAppModule {