Skip to content

Commit

Permalink
[main] fix startTime timestamp (#2183)
Browse files Browse the repository at this point in the history
  • Loading branch information
siyuniu-ms committed Oct 20, 2023
1 parent 36856e1 commit d0f2a35
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
isNullOrUndefined, isString, isUndefined, mergeEvtNamespace, onConfigChange, safeGetCookieMgr, strUndefined, throwError
} from "@microsoft/applicationinsights-core-js";
import { PropertiesPlugin } from "@microsoft/applicationinsights-properties-js";
import { getPerformance, isError, objDeepFreeze, objDefine, scheduleTimeout, strIndexOf } from "@nevware21/ts-utils";
import { isError, objDeepFreeze, objDefine, scheduleTimeout, strIndexOf } from "@nevware21/ts-utils";
import { IAppInsightsInternal, PageViewManager } from "./Telemetry/PageViewManager";
import { PageViewPerformanceManager } from "./Telemetry/PageViewPerformanceManager";
import { PageVisitTimeManager } from "./Telemetry/PageVisitTimeManager";
Expand Down Expand Up @@ -285,22 +285,11 @@ 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 {
if (isNullOrUndefined(pageView.startTime)) {
// calculate the start time manually
let duration = ((properties || pageView.properties || {}).duration || 0);
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);
}
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;
}

0 comments on commit d0f2a35

Please sign in to comment.