Skip to content

Commit

Permalink
analytics: initialize autoTrackPageVisitTime deps (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
markwolff committed Apr 9, 2019
1 parent 3ea7cd9 commit 0ff1264
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
8 changes: 5 additions & 3 deletions vNext/AISKU/src/Initialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ export class Initialization implements IApplicationInsights {
* @param {IPageViewTelemetry} pageView
* @memberof Initialization
*/
public trackPageView(pageView: IPageViewTelemetry) {
this.appInsights.trackPageView(pageView);
public trackPageView(pageView?: IPageViewTelemetry) {
const inPv = pageView || {};
this.appInsights.trackPageView(inPv);
}

/**
Expand All @@ -92,7 +93,8 @@ export class Initialization implements IApplicationInsights {
* @memberof Initialization
*/
public trackPageViewPerformance(pageViewPerformance: IPageViewPerformanceTelemetry): void {
this.appInsights.trackPageViewPerformance(pageViewPerformance);
const inPvp = pageViewPerformance || {};
this.appInsights.trackPageViewPerformance(inPvp);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,62 @@ export class ApplicationInsightsTests extends TestClass {
}

public registerTests() {
this.testCase({
name: 'AppInsightsTests: PageVisitTimeManager is constructed when analytics plugin is initialized',
test: () => {
// Setup
var channel = new ChannelPlugin();
var core = new AppInsightsCore();
var appInsights: ApplicationInsights = new ApplicationInsights();

// Act
var config = {
instrumentationKey: 'ikey'
};

core.initialize(
config,
[appInsights, channel]
);
const pvtm = appInsights['_pageVisitTimeManager'];

// Assert
Assert.ok(pvtm)
Assert.ok(pvtm['_logger']);
Assert.ok(pvtm['pageVisitTimeTrackingHandler']);
}
});

this.testCase({
name: 'AppInsightsTests: PageVisitTimeManager is available when config.autoTrackPageVisitTime is true and trackPageView is called',
test: () => {
// Setup
var channel = new ChannelPlugin();
var core = new AppInsightsCore();
var appInsights: ApplicationInsights = new ApplicationInsights();

var config = {
instrumentationKey: 'ikey',
autoTrackPageVisitTime: true
};
core.initialize(
config,
[appInsights, channel]
);
const pvtm = appInsights['_pageVisitTimeManager'];
const pvtmSpy = this.sandbox.spy(pvtm, 'trackPreviousPageVisit');

Assert.ok(pvtm)
Assert.ok(pvtmSpy.notCalled);

// Act
appInsights.trackPageView();

// Assert
Assert.ok(pvtmSpy.calledOnce);
}
});

this.testCase({
name: 'AppInsightsTests: config can be set from root',
test: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,13 @@ export class ApplicationInsights implements IAppInsights, ITelemetryPlugin, IApp
* @param customProperties Additional data used to filter events and metrics. Defaults to empty.
* If a user wants to provide duration for pageLoad, it'll have to be in pageView.properties.duration
*/
public trackPageView(pageView: IPageViewTelemetry, customProperties?: ICustomProperties) {
public trackPageView(pageView?: IPageViewTelemetry, customProperties?: ICustomProperties) {
try {
this._pageViewManager.trackPageView(pageView, customProperties);
const inPv = pageView || {};
this._pageViewManager.trackPageView(inPv, customProperties);

if (this.config.autoTrackPageVisitTime) {
this._pageVisitTimeManager.trackPreviousPageVisit(pageView.name, pageView.uri);
this._pageVisitTimeManager.trackPreviousPageVisit(inPv.name, inPv.uri);
}
} catch (e) {
this._logger.throwInternal(
Expand Down Expand Up @@ -502,6 +503,7 @@ export class ApplicationInsights implements IAppInsights, ITelemetryPlugin, IApp

this._pageViewPerformanceManager = new PageViewPerformanceManager(this.core);
this._pageViewManager = new PageViewManager(this, this.config.overridePageViewDuration, this.core, this._pageViewPerformanceManager);
this._pageVisitTimeManager = new PageVisitTimeManager(this._logger, (pageName, pageUrl, pageVisitTime) => this.trackPageVisitTime(pageName, pageUrl, pageVisitTime))

this._telemetryInitializers = [];
this._addDefaultTelemetryInitializers(configGetters);
Expand Down Expand Up @@ -564,6 +566,22 @@ export class ApplicationInsights implements IAppInsights, ITelemetryPlugin, IApp
this._isInitialized = true;
}

/**
* Log a page visit time
* @param pageName Name of page
* @param pageVisitDuration Duration of visit to the page in milleseconds
*/
private trackPageVisitTime(pageName: string, pageUrl: string, pageVisitTime: number) {
var properties = { PageName: pageName, PageUrl: pageUrl };
this.trackMetric({
name: "PageVisitTime",
average: pageVisitTime,
max: pageVisitTime,
min: pageVisitTime,
sampleCount: 1
}, properties);
}

private _addDefaultTelemetryInitializers(configGetters: ITelemetryConfig) {
if (!configGetters.isBrowserLinkTrackingEnabled()) {
const browserLinkPaths = ['/browserLinkSignalR/', '/__browserLink/'];
Expand Down

0 comments on commit 0ff1264

Please sign in to comment.