Merge branch 'add-referrer-to-pageview-event-7.0' into add-referrer-to-pageview-event-7.2

This commit is contained in:
Art Lowel
2023-05-30 16:08:59 +02:00
2 changed files with 37 additions and 16 deletions

View File

@@ -10,7 +10,7 @@ describe(`BrowserReferrerService`, () => {
beforeEach(() => {
routeService = {
getPreviousUrl: () => observableOf('')
getHistory: () => observableOf([])
} as any;
service = new BrowserReferrerService(
{ referrer: documentReferrer },
@@ -20,12 +20,9 @@ describe(`BrowserReferrerService`, () => {
});
describe(`getReferrer`, () => {
let prevUrl: string;
describe(`when getPreviousUrl is an empty string`, () => {
describe(`when the history is an empty`, () => {
beforeEach(() => {
prevUrl = '';
spyOn(routeService, 'getPreviousUrl').and.returnValue(observableOf(prevUrl));
spyOn(routeService, 'getHistory').and.returnValue(observableOf([]));
});
it(`should return document.referrer`, (done: DoneFn) => {
@@ -36,13 +33,31 @@ describe(`BrowserReferrerService`, () => {
});
});
describe(`when getPreviousUrl is not empty`, () => {
describe(`when the history only contains the current route`, () => {
beforeEach(() => {
prevUrl = '/some/local/route';
spyOn(routeService, 'getPreviousUrl').and.returnValue(observableOf(prevUrl));
spyOn(routeService, 'getHistory').and.returnValue(observableOf(['/current/route']));
});
it(`should return the value emitted by getPreviousUrl combined with the origin from HardRedirectService`, (done: DoneFn) => {
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();

View File

@@ -1,7 +1,7 @@
import { ReferrerService } from './referrer.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { isEmpty } from '../../shared/empty.util';
import { isEmpty, hasNoValue } from '../../shared/empty.util';
import { URLCombiner } from '../url-combiner/url-combiner';
import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@@ -33,13 +33,19 @@ export class BrowserReferrerService extends ReferrerService {
* in the store yet, document.referrer will be used
*/
public getReferrer(): Observable<string> {
return this.routeService.getPreviousUrl().pipe(
map((prevUrl: string) => {
// if we don't have anything in the history yet, return document.referrer
// (note that that may be empty too, e.g. if you've just opened a new browser tab)
if (isEmpty(prevUrl)) {
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();
}
})