Skip to content

Commit

Permalink
Correct pageView telemetry.
Browse files Browse the repository at this point in the history
  • Loading branch information
CXuesong committed Dec 7, 2019
1 parent 52f9451 commit 260ca6d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
34 changes: 29 additions & 5 deletions src/appContainer/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,41 @@ function startNewPageScope(location: Location<any>): 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<IRouteRootProps> {
private _pageScopeId: string | undefined;
private _pageTitle: string | undefined;
private _refUrl: string;
private _pageUrl: string;
public constructor(props: Readonly<IRouteRootProps>) {
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);
Expand All @@ -66,10 +87,13 @@ export class RouteRoot extends React.PureComponent<IRouteRootProps> {
</PageTitleContext.Consumer>);
}
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<IRouteRootProps>) {
if (prevProps.location !== this.props.location) {
Expand Down
8 changes: 4 additions & 4 deletions src/utility/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 260ca6d

Please sign in to comment.