Skip to content

Commit

Permalink
fix stopTrackEvents (#1859)
Browse files Browse the repository at this point in the history
Co-authored-by: Nev <54870357+MSNev@users.noreply.github.com>
  • Loading branch information
Karlie-777 and MSNev committed Jun 29, 2022
1 parent 4c54b9d commit 30e4518
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
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

0 comments on commit 30e4518

Please sign in to comment.