From 7df0a3006d7225f2f685a0a801eedc1979a61b78 Mon Sep 17 00:00:00 2001 From: Karlie Li Date: Tue, 24 Jun 2025 17:30:52 -0700 Subject: [PATCH 1/3] add click tests --- .../Unit/src/applicationinsights.e2e.tests.ts | 3 + .../Tests/Unit/src/ClickEventTest.ts | 109 +++++++++++++++++- .../src/handlers/AutoCaptureHandler.ts | 6 + 3 files changed, 116 insertions(+), 2 deletions(-) diff --git a/AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts b/AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts index c3fc8830a..a6747b5de 100644 --- a/AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts +++ b/AISKU/Tests/Unit/src/applicationinsights.e2e.tests.ts @@ -1685,6 +1685,9 @@ export class ApplicationInsightsTests extends AITestClass { () => { this._ai.addTelemetryInitializer((item: ITelemetryItem) => { item.tags[this.tagKeys.cloudName] = "my.custom.cloud.name"; + if (item.baseType === "PageviewPerformanceData") { + // modify your items here + } }); this._ai.trackEvent({ name: "Custom event via addTelemetryInitializer" }); } diff --git a/extensions/applicationinsights-clickanalytics-js/Tests/Unit/src/ClickEventTest.ts b/extensions/applicationinsights-clickanalytics-js/Tests/Unit/src/ClickEventTest.ts index 445c9f749..4d55ebb5e 100644 --- a/extensions/applicationinsights-clickanalytics-js/Tests/Unit/src/ClickEventTest.ts +++ b/extensions/applicationinsights-clickanalytics-js/Tests/Unit/src/ClickEventTest.ts @@ -4,11 +4,11 @@ import { Assert, AITestClass } from "@microsoft/ai-test-framework"; import { IConfig, utlCanUseLocalStorage } from "@microsoft/applicationinsights-common"; -import { ITelemetryItem, AppInsightsCore, IPlugin, IConfiguration, DiagnosticLogger, hasDocument, isFunction, IAppInsightsCore} from '@microsoft/applicationinsights-core-js'; +import { ITelemetryItem, AppInsightsCore, IPlugin, IConfiguration, DiagnosticLogger, hasDocument, isFunction, IAppInsightsCore, eventOn} from '@microsoft/applicationinsights-core-js'; import { ClickAnalyticsPlugin, BehaviorMapValidator, BehaviorValueValidator, BehaviorEnumValidator } from '../../../src/ClickAnalyticsPlugin'; import { PageAction } from "../../../src/events/PageAction"; import { DomContentHandler } from '../../../src/handlers/DomContentHandler'; -import { IPageActionOverrideValues } from '../../../src/Interfaces/Datamodel'; +import { IClickAnalyticsConfiguration, IPageActionOverrideValues } from '../../../src/Interfaces/Datamodel'; import { sanitizeUrl } from "../../../src/DataCollector"; import { DEFAULT_AI_BLOB_ATTRIBUTE_TAG, DEFAULT_DATA_PREFIX, DEFAULT_DONOT_TRACK_TAG } from "../../../src/common/Utils"; import { PropertiesPlugin } from "@microsoft/applicationinsights-properties-js"; @@ -165,6 +165,111 @@ export class ClickEventTest extends AITestClass { } }); + this.testCase({ + name: "autoCapture: click events are automatically captured and tracked", + useFakeTimers: true, + test: () => { + const config = { + coreData: {}, + autoCapture: true, + dataTags: { + useDefaultContentNameOrId: true, + metaDataPrefix: "ha-", + customDataPrefix: "data-ha-", + aiBlobAttributeTag: "blob" + } + } as IClickAnalyticsConfiguration; + let clickAnalyticsPlugin = new ClickAnalyticsPlugin(); + let core = new AppInsightsCore(); + let channel = new ChannelPlugin(); + core.initialize({ + instrumentationKey: "testIkey", + extensionConfig: { + [clickAnalyticsPlugin.identifier]: config + } + } as IConfig & IConfiguration, [clickAnalyticsPlugin, channel]); + this.onDone(() => { + core.unload(false); + }); + + let spy = this.sandbox.spy(clickAnalyticsPlugin.core, "track"); + + let element = document.createElement("button"); + element.setAttribute("id", "testAutoCaptureBtn"); + element.setAttribute("data-ha-aN", "autoCaptureArea"); + document.body.appendChild(element); + + const mouseDownEvent = new MouseEvent("mousedown", { bubbles: true, cancelable: true }); + element.dispatchEvent(mouseDownEvent); + + this.clock.tick(500); + + Assert.equal(true, spy.called, "track should be called on click event"); + let calledEvent: ITelemetryItem = spy.getCall(0).args[0]; + Assert.equal("testAutoCaptureBtn", calledEvent.baseData["name"]); + + // Clean up + document.body.removeChild(element); + } + }); + + this.testCase({ + name: "autoCapture: click events are automatically captured and tracked with customized config", + useFakeTimers: true, + test: () => { + const config = { + coreData: {}, + autoCapture: true, + dataTags: { + useDefaultContentNameOrId: true, + metaDataPrefix: "ha-", + customDataPrefix: "data-ha-", + aiBlobAttributeTag: "blob" + }, + trackElementTypes: "A" + } as IClickAnalyticsConfiguration; + let clickAnalyticsPlugin = new ClickAnalyticsPlugin(); + let core = new AppInsightsCore(); + let channel = new ChannelPlugin(); + core.initialize({ + instrumentationKey: "testIkey", + extensionConfig: { + [clickAnalyticsPlugin.identifier]: config + } + } as IConfig & IConfiguration, [clickAnalyticsPlugin, channel]); + this.onDone(() => { + core.unload(false); + }); + + let spy = this.sandbox.spy(clickAnalyticsPlugin.core, "track"); + + let element = document.createElement("a"); + element.setAttribute("id", "testAutoCaptureA"); + element.setAttribute("data-ha-aN", "autoCaptureArea"); + document.body.appendChild(element); + + let elementNotToTrack = document.createElement("button"); + elementNotToTrack.setAttribute("id", "testAutoCaptureBtn"); + elementNotToTrack.setAttribute("data-ha-aN", "autoCaptureArea"); + document.body.appendChild(element); + + let mouseDownEvent = new MouseEvent("mousedown", { bubbles: true, cancelable: true }); + element.dispatchEvent(mouseDownEvent); + + let mouseDownEventNotToTrack = new MouseEvent("mousedown", { bubbles: true, cancelable: true }); + elementNotToTrack.dispatchEvent(mouseDownEventNotToTrack); + + this.clock.tick(500); + + Assert.equal(true, spy.called, "track should be called on click event"); + let calledEvent: ITelemetryItem = spy.getCall(0).args[0]; + Assert.equal("testAutoCaptureA", calledEvent.baseData["name"]); + + // Clean up + document.body.removeChild(element); + } + }); + this.testCase({ name: "trackPageView properties are correctly assigned (Empty)", test: () => { diff --git a/extensions/applicationinsights-clickanalytics-js/src/handlers/AutoCaptureHandler.ts b/extensions/applicationinsights-clickanalytics-js/src/handlers/AutoCaptureHandler.ts index 0fd543aca..120d799b2 100644 --- a/extensions/applicationinsights-clickanalytics-js/src/handlers/AutoCaptureHandler.ts +++ b/extensions/applicationinsights-clickanalytics-js/src/handlers/AutoCaptureHandler.ts @@ -68,6 +68,7 @@ export class AutoCaptureHandler implements IAutoCaptureHandler { } if (clickEvent) { let element = clickEvent.srcElement || clickEvent.target; + console.log(element.tagName) // populate overrideValues var overrideValues: IPageActionOverrideValues = { @@ -75,6 +76,7 @@ export class AutoCaptureHandler implements IAutoCaptureHandler { clickCoordinateY: clickEvent.pageY }; var isRightClickObj = isRightClick(clickEvent as MouseEvent); + console.log(isRightClickObj) if (isRightClickObj) { overrideValues.actionType = ActionType.CLICKRIGHT; } else if (isLeftClick(clickEvent as MouseEvent)) { @@ -86,13 +88,17 @@ export class AutoCaptureHandler implements IAutoCaptureHandler { } else if (isMiddleClick(clickEvent as MouseEvent)) { overrideValues.actionType = ActionType.CLICKMIDDLE; } else { + console.log("test123") return; } + console.log("test123456") while (element && element.tagName) { + console.log("test") // control property will be available for