-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[EBT] Add page url to browser-side context #159916
Merged
pgayvallet
merged 9 commits into
elastic:main
from
pgayvallet:kbn-149249-ebt-page-title-url
Jun 20, 2023
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ced717e
add the page's url to browser EBT context
pgayvallet 91384c7
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 851e3fb
fix integration tests
pgayvallet 272f1bf
add integration tests
pgayvallet 8f92d65
Merge remote-tracking branch 'upstream/main' into kbn-149249-ebt-page…
pgayvallet 06452c6
test navigating within an app
pgayvallet 02e9233
import browser type
pgayvallet bdae254
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine a154080
Merge branch 'main' into kbn-149249-ebt-page-title-url
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import type { HttpSetup, HttpStart } from '@kbn/core-http-browser'; | |
import type { Capabilities } from '@kbn/core-capabilities-common'; | ||
import type { MountPoint } from '@kbn/core-mount-utils-browser'; | ||
import type { OverlayStart } from '@kbn/core-overlays-browser'; | ||
import type { AnalyticsServiceSetup } from '@kbn/core-analytics-browser'; | ||
import type { | ||
App, | ||
AppDeepLink, | ||
|
@@ -35,10 +36,18 @@ import type { InternalApplicationSetup, InternalApplicationStart, Mounter } from | |
|
||
import { getLeaveAction, isConfirmAction } from './application_leave'; | ||
import { getUserConfirmationHandler } from './navigation_confirm'; | ||
import { appendAppPath, parseAppUrl, relativeToAbsolute, getAppInfo } from './utils'; | ||
import { | ||
appendAppPath, | ||
parseAppUrl, | ||
relativeToAbsolute, | ||
getAppInfo, | ||
getLocationObservable, | ||
} from './utils'; | ||
import { registerAnalyticsContextProvider } from './register_analytics_context_provider'; | ||
|
||
export interface SetupDeps { | ||
http: HttpSetup; | ||
analytics: AnalyticsServiceSetup; | ||
history?: History<any>; | ||
/** Used to redirect to external urls */ | ||
redirectTo?: (path: string) => void; | ||
|
@@ -111,6 +120,7 @@ export class ApplicationService { | |
|
||
public setup({ | ||
http: { basePath }, | ||
analytics, | ||
redirectTo = (path: string) => { | ||
window.location.assign(path); | ||
}, | ||
|
@@ -126,6 +136,12 @@ export class ApplicationService { | |
}), | ||
}); | ||
|
||
const location$ = getLocationObservable(window.location, this.history); | ||
registerAnalyticsContextProvider({ | ||
analytics, | ||
location$, | ||
}); | ||
Comment on lines
+139
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The actual registration |
||
|
||
this.navigate = (url, state, replace) => { | ||
// basePath not needed here because `history` is configured with basename | ||
return replace ? this.history!.replace(url, state) : this.history!.push(url, state); | ||
|
38 changes: 38 additions & 0 deletions
38
...ication/core-application-browser-internal/src/register_analytics_context_provider.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { firstValueFrom, ReplaySubject, Subject } from 'rxjs'; | ||
import { registerAnalyticsContextProvider } from './register_analytics_context_provider'; | ||
import { analyticsServiceMock } from '@kbn/core-analytics-browser-mocks'; | ||
|
||
describe('registerAnalyticsContextProvider', () => { | ||
let analytics: ReturnType<typeof analyticsServiceMock.createAnalyticsServiceSetup>; | ||
let location$: Subject<string>; | ||
|
||
beforeEach(() => { | ||
analytics = analyticsServiceMock.createAnalyticsServiceSetup(); | ||
location$ = new ReplaySubject<string>(1); | ||
registerAnalyticsContextProvider({ analytics, location$ }); | ||
}); | ||
|
||
test('should register the analytics context provider', () => { | ||
expect(analytics.registerContextProvider).toHaveBeenCalledTimes(1); | ||
expect(analytics.registerContextProvider).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
name: 'page url', | ||
}) | ||
); | ||
}); | ||
|
||
test('emits a context value when location$ emits', async () => { | ||
location$.next('/some_url'); | ||
await expect( | ||
firstValueFrom(analytics.registerContextProvider.mock.calls[0][0].context$) | ||
).resolves.toEqual({ page_url: '/some_url' }); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
.../application/core-application-browser-internal/src/register_analytics_context_provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { AnalyticsServiceSetup } from '@kbn/core-analytics-server'; | ||
import { type Observable, map } from 'rxjs'; | ||
|
||
export function registerAnalyticsContextProvider({ | ||
analytics, | ||
location$, | ||
}: { | ||
analytics: AnalyticsServiceSetup; | ||
location$: Observable<string>; | ||
}) { | ||
analytics.registerContextProvider({ | ||
name: 'page url', | ||
context$: location$.pipe(map((location) => ({ page_url: location }))), | ||
schema: { | ||
page_url: { type: 'text', _meta: { description: 'The page url' } }, | ||
}, | ||
}); | ||
} |
65 changes: 65 additions & 0 deletions
65
...e/application/core-application-browser-internal/src/utils/get_location_observable.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { createBrowserHistory, type History } from 'history'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { getLocationObservable } from './get_location_observable'; | ||
|
||
const nextTick = () => new Promise((resolve) => window.setTimeout(resolve, 1)); | ||
|
||
describe('getLocationObservable', () => { | ||
let history: History; | ||
|
||
beforeEach(() => { | ||
history = createBrowserHistory(); | ||
}); | ||
|
||
it('emits with the initial location', async () => { | ||
const location$ = getLocationObservable({ pathname: '/foo', hash: '' }, history); | ||
expect(await firstValueFrom(location$)).toEqual('/foo'); | ||
}); | ||
|
||
it('emits when the location changes', async () => { | ||
const location$ = getLocationObservable({ pathname: '/foo', hash: '' }, history); | ||
const locations: string[] = []; | ||
location$.subscribe((location) => locations.push(location)); | ||
|
||
history.push({ pathname: '/bar' }); | ||
history.push({ pathname: '/dolly' }); | ||
|
||
await nextTick(); | ||
|
||
expect(locations).toEqual(['/foo', '/bar', '/dolly']); | ||
}); | ||
|
||
it('emits only once for a given url', async () => { | ||
const location$ = getLocationObservable({ pathname: '/foo', hash: '' }, history); | ||
const locations: string[] = []; | ||
location$.subscribe((location) => locations.push(location)); | ||
|
||
history.push({ pathname: '/bar' }); | ||
history.push({ pathname: '/bar' }); | ||
history.push({ pathname: '/foo' }); | ||
|
||
await nextTick(); | ||
|
||
expect(locations).toEqual(['/foo', '/bar', '/foo']); | ||
}); | ||
|
||
it('includes the hash when present', async () => { | ||
const location$ = getLocationObservable({ pathname: '/foo', hash: '#/index' }, history); | ||
const locations: string[] = []; | ||
location$.subscribe((location) => locations.push(location)); | ||
|
||
history.push({ pathname: '/bar', hash: '#/home' }); | ||
|
||
await nextTick(); | ||
|
||
expect(locations).toEqual(['/foo#/index', '/bar#/home']); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: would it work when navigating inside the same app?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it does, but I can add a test to ensure that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in 06452c6