Skip to content
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

[main] fix startTime timestamp #2183

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,22 +285,6 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights
if (doc) {
pageView.refUri = pageView.refUri === undefined ? doc.referrer : pageView.refUri;
}

let perf = getPerformance();
// Access the performance timing object
const navigationEntries = (perf && perf.getEntriesByType && perf.getEntriesByType("navigation"));

// Edge Case the navigation Entries may return an empty array and the timeOrigin is not supported on IE
if (navigationEntries && navigationEntries[0] && !isUndefined(perf.timeOrigin)) {
// Get the value of loadEventStart
const loadEventStart = (navigationEntries[0] as PerformanceNavigationTiming).loadEventStart;
pageView.startTime = new Date(perf.timeOrigin + loadEventStart);
} else {
// calculate the start time manually
let duration = ((properties || pageView.properties || {}).duration || 0);
Copy link
Collaborator

@MSNev MSNev Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at all of the "usages" of the sendPageViewInternal, it looks like (once we remove this from here), that we should do this duration update in the AngularPlugin.ts Line 602 (before the _pageTracking calls the _self.sendPageViewInternal so that the "time" is still shifted back by the duration otherwise the Portal will show the wrong "startTime"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe?? (testing required)
We can just keep something here like

if (isNullOrUndefined(pageView.startTime)) {
    // calculate the start time manually
    let duration = ((properties || pageView.properties || {}).duration || 0);
    if (duration) {
        pageView.startTime = new Date(new Date().getTime() - duration);
    }
}

So that if the previous code (like the line 602 above) doesn't specify any startTime then we keep "using" this default, this should also work for the subsequent page views, even with your code below for the if (!firstPageViewSent){

pageView.startTime = new Date(new Date().getTime() - duration);
}

let telemetryItem = createTelemetryItem<IPageViewTelemetryInternal>(
pageView,
PageView.dataType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
IAppInsightsCore, IDiagnosticLogger, IProcessTelemetryUnloadContext, ITelemetryUnloadState, _eInternalMessageId, _throwInternal,
arrForEach, dumpObj, eLoggingSeverity, getDocument, getExceptionName, getLocation, isNullOrUndefined
} from "@microsoft/applicationinsights-core-js";
import { ITimerHandler, isWebWorker, scheduleTimeout } from "@nevware21/ts-utils";
import { ITimerHandler, getPerformance, isUndefined, isWebWorker, scheduleTimeout } from "@nevware21/ts-utils";
import { PageViewPerformanceManager } from "./PageViewPerformanceManager";

/**
Expand All @@ -35,6 +35,7 @@ export class PageViewManager {
let queueTimer: ITimerHandler = null;
let itemQueue: Array<() => boolean> = [];
let pageViewPerformanceSent: boolean = false;
let firstPageViewSent: boolean = false;
let _logger: IDiagnosticLogger;

if (core) {
Expand Down Expand Up @@ -95,6 +96,24 @@ export class PageViewManager {
let location = getLocation();
uri = pageView.uri = location && location.href || "";
}

if (!firstPageViewSent){
let perf = getPerformance();
// Access the performance timing object
const navigationEntries = (perf && perf.getEntriesByType && perf.getEntriesByType("navigation"));

// Edge Case the navigation Entries may return an empty array and the timeOrigin is not supported on IE
if (navigationEntries && navigationEntries[0] && !isUndefined(perf.timeOrigin)) {
// Get the value of loadEventStart
const loadEventStart = (navigationEntries[0] as PerformanceNavigationTiming).loadEventStart;
pageView.startTime = new Date(perf.timeOrigin + loadEventStart);
} else {
// calculate the start time manually
let duration = ((customProperties || pageView.properties || {}).duration || 0);
pageView.startTime = new Date(new Date().getTime() - duration);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be done of ALL other cases, otherwise the reported "time" will be wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise the Portal will show the wrong "startTime"

}
firstPageViewSent = true;
}

// case 1a. if performance timing is not supported by the browser, send the page view telemetry with the duration provided by the user. If the user
// do not provide the duration, set duration to undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export interface IPageViewTelemetry extends IPartC {
* iKey String - custom defined iKey.
*/
iKey?: string;

/**
* Time first page view is triggered
*/
startTime?: Date;
}


Expand All @@ -62,5 +67,4 @@ export interface IPageViewTelemetryInternal extends IPageViewTelemetry {
* Version of the part B schema, todo: set this value in trackpageView
*/
ver?: string;
startTime?: Date;
}
Loading