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

fix stopTrackEvent #1859

Merged
merged 2 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -382,7 +382,9 @@ export class AnalyticsPluginTests extends AITestClass {
"trackPageView",
"trackPageViewPerformance",
"startTrackPage",
"stopTrackPage"
"stopTrackPage",
"startTrackEvent",
"stopTrackEvent"
];
while (members.length) {
leTest(members.pop());
Expand All @@ -392,6 +394,7 @@ export class AnalyticsPluginTests extends AITestClass {

this.addGenericTests();
this.addStartStopTrackPageTests();
this.addStartStopTrackEventTests()
this.addTrackExceptionTests();
this.addOnErrorTests();
this.addTrackMetricTests();
Expand Down Expand Up @@ -1293,6 +1296,81 @@ export class AnalyticsPluginTests extends AITestClass {
});
}

private addStartStopTrackEventTests() {
const testValues = {
name: "testStopTrack",
properties: {
"property1": "5",
"property2": "10",
"refUri": "test.com"
},
measurements: {
"measurement": 300
}
};

this.testCase({
name: "TelemetryContex: empty Start/StopTrackEvent should only have duration properties",
useFakeTimers: true,
test: () => {
const core = new AppInsightsCore();
this.sandbox.stub(core, "getTransmissionControls");
const appInsights = new AnalyticsPlugin();
this.onDone(() => {
appInsights.teardown();
});

appInsights.initialize({ "instrumentationKey": "ikey" }, core, []);
const trackStub = this.sandbox.stub(appInsights.core, "track");
this.clock.tick(5);

// act
appInsights.startTrackEvent(testValues.name);
this.clock.tick(5);
appInsights.stopTrackEvent(testValues.name);
Assert.ok(trackStub.calledOnce, "single event tracking stopped");

// verify
const telemetry = trackStub.args[0][0];
Assert.equal(testValues.name,telemetry.baseData.name);
Assert.deepEqual({ "duration": "5"},telemetry.baseData.properties);
Assert.equal(undefined, telemetry.baseData.measurements.measurement);
}
});

this.testCase({
name: "TelemetryContex: Start/StopTrackEvent capture correct properties and measurements",
useFakeTimers: true,
test: () => {
const core = new AppInsightsCore();
this.sandbox.stub(core, "getTransmissionControls");
const appInsights = new AnalyticsPlugin();
this.onDone(() => {
appInsights.teardown();
});

appInsights.initialize({ "instrumentationKey": "ikey" }, core, []);
const trackStub = this.sandbox.stub(appInsights.core, "track");
this.clock.tick(5);

// act
appInsights.startTrackEvent(testValues.name);
this.clock.tick(5);
appInsights.stopTrackEvent(testValues.name,testValues.properties,testValues.measurements);
Assert.ok(trackStub.calledOnce, "single event tracking stopped");

// verify
const telemetry = trackStub.args[0][0];
Assert.equal(testValues.name,telemetry.baseData.name);
Assert.equal(testValues.properties.property1,telemetry.baseData.properties.property1);
Assert.equal(testValues.properties.property2, telemetry.baseData.properties.property2);
Assert.equal(testValues.properties.refUri, telemetry.baseData.properties.refUri);
Assert.equal("5", telemetry.baseData.properties.duration);
Assert.equal(testValues.measurements.measurement, telemetry.baseData.measurements.measurement);
}
});
}

private addTelemetryInitializerTests(): void {
this.testCase({
name: "TelemetryContext: onBeforeSendTelemetry is called within track() and gets the envelope as an argument",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights
*/
_self.stopTrackEvent = (name: string, properties?: { [key: string]: string }, measurements?: { [key: string]: number }) => {
try {
_eventTracking.stop(name, undefined, properties); // Todo: Fix to pass measurements once type is updated
_eventTracking.stop(name, undefined, properties, measurements);
} catch (e) {
_throwInternal(eLoggingSeverity.CRITICAL,
_eInternalMessageId.StopTrackEventFailed,
Expand Down Expand Up @@ -562,13 +562,17 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights

_eventTracking = new Timing(_self.diagLog(), "trackEvent");
_eventTracking.action =
(name?: string, url?: string, duration?: number, properties?: { [key: string]: string }) => {
(name?: string, url?: string, duration?: number, properties?: { [key: string]: string }, measurements?: { [key: string]: number }) => {
if (!properties) {
properties = {};
}

if (!measurements) {
measurements = {};
}

properties[durationProperty] = duration.toString();
_self.trackEvent({ name, properties } as IEventTelemetry);
_self.trackEvent({ name, properties, measurements } as IEventTelemetry);
}

// initialize page view timing
Expand Down