From 260ca6dcca29de6a2c75a7dee9bbc2956e602139 Mon Sep 17 00:00:00 2001 From: forest93 Date: Sun, 8 Dec 2019 01:12:35 +0800 Subject: [PATCH] Correct pageView telemetry. --- src/appContainer/app.tsx | 34 +++++++++++++++++++++++++++++----- src/utility/telemetry.ts | 8 ++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/appContainer/app.tsx b/src/appContainer/app.tsx index 053af9d6..f05f99fe 100644 --- a/src/appContainer/app.tsx +++ b/src/appContainer/app.tsx @@ -39,20 +39,41 @@ function startNewPageScope(location: Location): string { return id; } -function endPageScope(id: string, title?: string) { - appInsights.stopTrackPage(id, undefined, { _name: title, contextId: id }); +function endPageScope(id: string, url: string, title?: string, refUri?: string, endReason?: string) { + appInsights.stopTrackPage(id, url, { + contextId: id, + endReason, + _outer_overrides: { + name: title, + refUri + } + }); } export class RouteRoot extends React.PureComponent { private _pageScopeId: string | undefined; private _pageTitle: string | undefined; + private _refUrl: string; + private _pageUrl: string; public constructor(props: Readonly) { super(props); + this._pageUrl = location.href; + this._refUrl = document.referrer; + } + private endPageScopeIfNeeded(endReason?: string) { + if (this._pageScopeId) { + // Keep track of the last title before routing to the next location. + endPageScope(this._pageScopeId, this._pageUrl, this._pageTitle, this._refUrl, endReason); + } + } + private onWindowUnload = () => { + this.endPageScopeIfNeeded("unload"); } private _onLocationChanged(): void { - // Keep track of the last title before routing to the next location. - endPageScope(this._pageScopeId!, this._pageTitle); + this.endPageScopeIfNeeded("locationChanged"); this._pageScopeId = startNewPageScope(this.props.location); + this._refUrl = this._pageUrl; + this._pageUrl = location.href; } public render() { const queryParams = parseQueryParams(this.props.location.search); @@ -66,10 +87,13 @@ export class RouteRoot extends React.PureComponent { ); } public componentDidMount() { + this._pageUrl = location.href; this._pageScopeId = startNewPageScope(this.props.location); + window.addEventListener("unload", this.onWindowUnload); } public componentWillUnmount() { - endPageScope(this._pageScopeId!, this._pageTitle); + window.removeEventListener("unload", this.onWindowUnload); + this.endPageScopeIfNeeded("unmount"); } public componentDidUpdate(prevProps: Readonly) { if (prevProps.location !== this.props.location) { diff --git a/src/utility/telemetry.ts b/src/utility/telemetry.ts index cf648366..ce805d58 100644 --- a/src/utility/telemetry.ts +++ b/src/utility/telemetry.ts @@ -15,10 +15,10 @@ export function initializeTracking() { function processTelemetry(item: ITelemetryItem): boolean { if (item.baseType === "PageviewData" && item.baseData) { // Allows us to override page title afterwards. - const surrogateName = item.baseData.properties && item.baseData.properties._name; - if (surrogateName) { - item.baseData.name = surrogateName; - delete item.baseData.properties._name; + const surrogateProps = item.baseData.properties && item.baseData.properties._outer_overrides; + if (surrogateProps) { + Object.assign(item.baseData, surrogateProps); + delete item.baseData.properties._outer_overrides; } } item.baseData = {