[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7DFHC5WYPV7VW6H4S6GVFSH":3,"skill-same:01M7DFHC5WYPV7VW6H4S6GVFSH":51},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":30,"commitSha":31,"skillDirectory":32,"forksCount":33,"repositoryHtmlUrl":34,"repositoryHomepageUrl":6,"ownerHtmlUrl":35,"ownerBlogUrl":6,"ownerDescription":6,"updatedAt":36,"install":37,"resources":50},"01M7DFHC5WYPV7VW6H4S6GVFSH","applicationinsights-web-ts",null,"Instrument browser\u002Fweb apps with the Application Insights JavaScript SDK (@microsoft\u002Fapplicationinsights-web). Use for Real User Monitoring (RUM) — page views, clicks, AJAX\u002Ffetch dependencies, exceptions, custom events, and browser-side GenAI agent traces correlated to backend...","Hazek-dc\u002FPresensiGuru_FaceNet_EMAR","Hazek-dc","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F75208960?v=4","01M7DEQV5B4YGS86A81DYH81DR","Hazek","01M7DEQV5XP4TM0C6NTHXCXCHR",1,"MIT","https:\u002F\u002Fgithub.com\u002FHazek-dc\u002FPresensiGuru_FaceNet_EMAR\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Fapplicationinsights-web-ts\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: applicationinsights-web-ts\ndescription: Instrument browser\u002Fweb apps with the Application Insights JavaScript SDK (@microsoft\u002Fapplicationinsights-web). Use for Real User Monitoring (RUM) — page views, clicks, AJAX\u002Ffetch dependencies, exceptions, custom events, and browser-side GenAI agent traces correlated to backend...\nrisk: unknown\nsource: https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\nsource_repo: microsoft\u002Fskills\nsource_type: official\ndate_added: 2026-07-01\nlicense: MIT\nlicense_source: https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Fblob\u002Fmain\u002FLICENSE\n---\n\n# Application Insights JavaScript SDK (Web) for TypeScript\n## When to Use\n\nUse this skill when you need instrument browser\u002Fweb apps with the Application Insights JavaScript SDK (@microsoft\u002Fapplicationinsights-web). Use for Real User Monitoring (RUM) — page views, clicks, AJAX\u002Ffetch dependencies, exceptions, custom events, and browser-side GenAI agent traces correlated to backend...\n\n\nReal User Monitoring (RUM) for browser apps with `@microsoft\u002Fapplicationinsights-web`. Auto-collects page views, AJAX\u002Ffetch dependencies, unhandled exceptions, and (with the Click Analytics plugin) clicks. Supports custom events, metrics, and **GenAI agent traces** that follow OpenTelemetry GenAI semantic conventions and correlate to backend spans via W3C Trace Context.\n\n> **Distinct from `azure-monitor-opentelemetry-ts`**, which is for Node.js server apps. This skill is for **browser\u002Fweb** code (and React Native).\n\n## Before Implementation\n\nSearch `microsoft-docs` MCP for current API patterns:\n\n- Query: \"Application Insights JavaScript SDK setup\"\n- Query: \"Application Insights JavaScript SDK configuration\"\n- Query: \"Application Insights JavaScript framework extensions React Angular\"\n- Verify package version: `npm view @microsoft\u002Fapplicationinsights-web version`\n\n## Packages\n\n| Package | Purpose |\n| --- | --- |\n| `@microsoft\u002Fapplicationinsights-web` | Core RUM SDK (page views, AJAX, exceptions). |\n| `@microsoft\u002Fapplicationinsights-clickanalytics-js` | Auto-collect click telemetry. |\n| `@microsoft\u002Fapplicationinsights-react-js` | React plugin (router instrumentation, hooks, HOC, ErrorBoundary). |\n| `@microsoft\u002Fapplicationinsights-react-native` | React Native plugin (native crashes, sessions). |\n| `@microsoft\u002Fapplicationinsights-angularplugin-js` | Angular plugin (router events, ErrorHandler). |\n| `@microsoft\u002Fapplicationinsights-debugplugin-js` | Dev-only telemetry inspector. |\n| `@microsoft\u002Fapplicationinsights-perfmarkmeasure-js` | User Timing (`performance.mark\u002Fmeasure`) integration. |\n\n## Installation\n\n```bash\nnpm i --save @microsoft\u002Fapplicationinsights-web\n# Optional plugins (install only what you use):\nnpm i --save @microsoft\u002Fapplicationinsights-clickanalytics-js\nnpm i --save @microsoft\u002Fapplicationinsights-react-js @microsoft\u002Fapplicationinsights-react-native @microsoft\u002Fapplicationinsights-angularplugin-js\n```\n\nTypings ship with the package — no separate `@types\u002F...` install needed.\n\n## Connection String\n\nThe browser SDK requires a connection string at init time. **It ships in plaintext to clients** — Microsoft Entra ID auth is not supported for browser telemetry. Use a separate App Insights resource with local auth enabled for browser RUM if you need to isolate it from backend telemetry.\n\n```bash\n# Vite \u002F CRA \u002F Next.js — expose to client via the public env prefix\nVITE_APPINSIGHTS_CONNECTION_STRING=\"InstrumentationKey=...;IngestionEndpoint=https:\u002F\u002F...;LiveEndpoint=https:\u002F\u002F...\"\nNEXT_PUBLIC_APPINSIGHTS_CONNECTION_STRING=\"InstrumentationKey=...\"\n```\n\n## Quick Start (npm)\n\n```typescript\nimport { ApplicationInsights } from \"@microsoft\u002Fapplicationinsights-web\";\n\nexport const appInsights = new ApplicationInsights({\n  config: {\n    connectionString: import.meta.env.VITE_APPINSIGHTS_CONNECTION_STRING,\n    enableAutoRouteTracking: true,        \u002F\u002F SPA route changes -> page views\n    enableCorsCorrelation: true,          \u002F\u002F propagate Request-Id \u002F traceparent to cross-origin AJAX\n    enableRequestHeaderTracking: true,\n    enableResponseHeaderTracking: true,\n    distributedTracingMode: 2,            \u002F\u002F DistributedTracingModes.AI_AND_W3C — emit traceparent for backend correlation\n    autoTrackPageVisitTime: true,\n    disableFetchTracking: false,          \u002F\u002F fetch() is auto-instrumented by default\n    excludeRequestFromAutoTrackingPatterns: [\u002Flivemetrics\\.azure\\.com\u002Fi]\n  }\n});\n\nappInsights.loadAppInsights();\nappInsights.trackPageView();\n```\n\nCall `loadAppInsights()` exactly once, as early as possible (before user interactions you want tracked). Then `trackPageView()` for the initial load — when `enableAutoRouteTracking` is on, subsequent route changes are automatic.\n\n## Quick Start (SDK Loader Script)\n\nRecommended when you want auto-updating SDK and zero build pipeline. Paste this as the **first** `\u003Cscript>` in `\u003Chead>`:\n\n```html\n\u003Cscript type=\"text\u002Fjavascript\" src=\"https:\u002F\u002Fjs.monitor.azure.com\u002Fscripts\u002Fb\u002Fai.3.gbl.min.js\" crossorigin=\"anonymous\">\u003C\u002Fscript>\n\u003Cscript type=\"text\u002Fjavascript\">\n  var appInsights = window.appInsights || function (cfg) {\n    \u002F* See: https:\u002F\u002Flearn.microsoft.com\u002Fazure\u002Fazure-monitor\u002Fapp\u002Fjavascript-sdk\n       Use the latest snippet from the Microsoft Learn page above — it includes\n       backup-CDN failover (cr), SDK-load-failure reporting, and the queue shim\n       so calls before SDK ready are not lost. *\u002F\n  }({ src: \"https:\u002F\u002Fjs.monitor.azure.com\u002Fscripts\u002Fb\u002Fai.3.gbl.min.js\",\n      crossOrigin: \"anonymous\",\n      cfg: { connectionString: \"YOUR_CONNECTION_STRING\" } });\n\u003C\u002Fscript>\n```\n\nLoader-only API (queued until SDK loads): `trackEvent`, `trackPageView`, `trackException`, `trackTrace`, `trackDependencyData`, `trackMetric`, `trackPageViewPerformance`, `startTrackPage`, `stopTrackPage`, `startTrackEvent`, `stopTrackEvent`, `addTelemetryInitializer`, `setAuthenticatedUserContext`, `clearAuthenticatedUserContext`, `flush`.\n\n## Core Tracking APIs\n\n```typescript\n\u002F\u002F Page views (SPAs that disable enableAutoRouteTracking)\nappInsights.trackPageView({ name: \"Checkout\", uri: \"\u002Fcheckout\", properties: { cartSize: 3 } });\n\n\u002F\u002F Custom events (user actions, business events)\nappInsights.trackEvent({ name: \"PurchaseCompleted\" }, { orderId: \"ord_123\", amountUsd: 49.95 });\n\n\u002F\u002F Exceptions (caught errors)\ntry {\n  await pay(order);\n} catch (err) {\n  appInsights.trackException({ exception: err as Error, severityLevel: 3, properties: { orderId: order.id } });\n}\n\n\u002F\u002F Traces (logs, severity 0=Verbose, 1=Info, 2=Warning, 3=Error, 4=Critical)\nappInsights.trackTrace({ message: \"Cart hydrated from local storage\", severityLevel: 1 });\n\n\u002F\u002F Custom metrics (numeric)\nappInsights.trackMetric({ name: \"checkout.duration_ms\", average: 1234 });\n\n\u002F\u002F Dependencies (manually-tracked outbound calls — fetch\u002FXHR are auto-tracked)\nappInsights.trackDependencyData({\n  id: crypto.randomUUID(),\n  name: \"GET \u002Fapi\u002Forders\",\n  duration: 87, success: true, responseCode: 200,\n  data: \"https:\u002F\u002Fapi.example.com\u002Fapi\u002Forders\", target: \"api.example.com\", type: \"Fetch\"\n});\n\n\u002F\u002F User identity (set ONCE per authenticated session — values are PII; do not pass emails)\nappInsights.setAuthenticatedUserContext(\"user-id-123\", \"tenant-456\", \u002F*storeInCookie*\u002F true);\nappInsights.clearAuthenticatedUserContext(); \u002F\u002F on logout\n\n\u002F\u002F Force send before unload\nappInsights.flush();\n```\n\n## Telemetry Initializers (enrichment & filtering)\n\nRun for every envelope before send. Return `false` to drop.\n\n```typescript\nimport type { ITelemetryItem } from \"@microsoft\u002Fapplicationinsights-web\";\n\nappInsights.addTelemetryInitializer((item: ITelemetryItem) => {\n  item.tags ??= {};\n  item.tags[\"ai.cloud.role\"] = \"web-shop\";\n  item.tags[\"ai.cloud.roleInstance\"] = window.location.hostname;\n  item.data ??= {};\n  item.data[\"app.version\"] = import.meta.env.VITE_APP_VERSION;\n  item.data[\"app.build\"] = import.meta.env.VITE_BUILD_SHA;\n\n  \u002F\u002F Drop noisy health-check page views\n  if (item.baseType === \"PageviewData\" && item.baseData?.uri?.endsWith(\"\u002Fhealthz\")) return false;\n\n  \u002F\u002F Scrub query-string secrets\n  if (item.baseData?.uri) {\n    item.baseData.uri = item.baseData.uri.replace(\u002F([?&](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Ftoken|sig|key)=)[^&]+\u002Fgi, \"$1REDACTED\");\n  }\n});\n```\n\n## Click Analytics\n\n```typescript\nimport { ClickAnalyticsPlugin } from \"@microsoft\u002Fapplicationinsights-clickanalytics-js\";\n\nconst clickPlugin = new ClickAnalyticsPlugin();\nconst appInsights = new ApplicationInsights({\n  config: {\n    connectionString: import.meta.env.VITE_APPINSIGHTS_CONNECTION_STRING,\n    extensions: [clickPlugin],\n    extensionConfig: {\n      [clickPlugin.identifier]: {\n        autoCapture: true,\n        dataTags: { useDefaultContentNameOrId: true, customDataPrefix: \"data-ai-\" },\n        urlCollectHash: false,\n        behaviorValidator: (b: string) => \u002F^[a-z0-9_]+$\u002F.test(b) ? b : \"\"\n      }\n    }\n  }\n});\nappInsights.loadAppInsights();\n```\n\nMark elements with `data-ai-*` attributes; clicks are emitted as Custom Events with parent-content metadata.\n\n## SPA Route Tracking\n\n- **Built-in:** set `enableAutoRouteTracking: true`. Hooks `history.pushState\u002FreplaceState` and `popstate`.\n- **React Router:** use `@microsoft\u002Fapplicationinsights-react-js` `withAITracking` HOC (see [references\u002Fframework-extensions.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fframework-extensions.md)).\n- **Manual:** call `appInsights.trackPageView({ name, uri })` in your router's `useEffect` on route change. Disable `enableAutoRouteTracking` to avoid double counting.\n\n## Distributed Tracing (correlate to backend)\n\nSet `distributedTracingMode: 2` (`DistributedTracingModes.AI_AND_W3C`). The SDK adds `traceparent` (and legacy `Request-Id`) to outbound `fetch`\u002F`XHR`. Backends instrumented with **OpenTelemetry** (e.g. `@azure\u002Fmonitor-opentelemetry`) auto-link to the browser's operation_Id.\n\nFor cross-origin calls, also set `enableCorsCorrelation: true` and add the calling origin to the **CORS exposed headers** on the API.\n\n## GenAI Agent Traces (OTel semantic conventions)\n\nWhen the browser invokes an AI agent (function-calling, tool-use, model calls direct from the client), emit App Insights **Dependency** telemetry whose attributes follow the OpenTelemetry **GenAI semantic conventions** so they are queryable alongside backend agent spans in App Insights \u002F Log Analytics.\n\n**Set the opt-in env first** so backend instrumentations agree on the same schema version:\n\n```bash\nOTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental\n```\n\n### Required attribute keys (use the OTel names verbatim)\n\n| Span \u002F op | Required attributes |\n| --- | --- |\n| `invoke_agent {agent.name}` | `gen_ai.operation.name=invoke_agent`, `gen_ai.provider.name`, `gen_ai.agent.name`, `gen_ai.agent.id` (when known) |\n| `create_agent {agent.name}` | `gen_ai.operation.name=create_agent`, `gen_ai.provider.name`, `gen_ai.agent.name`, `gen_ai.request.model` |\n| `chat {model}` | `gen_ai.operation.name=chat`, `gen_ai.provider.name`, `gen_ai.request.model`, `gen_ai.response.model`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens` |\n| `execute_tool {tool.name}` | `gen_ai.operation.name=execute_tool`, `gen_ai.tool.name`, `gen_ai.tool.type` (`function` \\| `extension` \\| `datastore`), `gen_ai.tool.call.id` |\n\n`gen_ai.provider.name` well-known values: `openai`, `azure.ai.openai`, `azure.ai.inference`, `anthropic`, `aws.bedrock`, `gcp.gemini`, `gcp.vertex_ai`, `cohere`, `mistral_ai`, `groq`, `deepseek`, `perplexity`, `x_ai`, `ibm.watsonx.ai`.\n\n> **Sensitive content opt-in.** `gen_ai.system_instructions`, `gen_ai.input.messages`, `gen_ai.output.messages`, `gen_ai.tool.call.arguments`, `gen_ai.tool.call.result` are **Opt-In** by default. Gate them behind a runtime flag and avoid them in production unless you have approved data handling.\n\n### Pattern: invoke_agent + nested tool\u002Fmodel spans\n\n```typescript\nimport { ApplicationInsights, SeverityLevel } from \"@microsoft\u002Fapplicationinsights-web\";\n\ntype GenAiAttrs = Record\u003Cstring, string | number | boolean | undefined>;\n\nfunction startGenAiSpan(name: string, attrs: GenAiAttrs) {\n  const id = crypto.randomUUID();\n  const start = performance.now();\n  const baseProps: GenAiAttrs = { \"gen_ai.span.id\": id, ...attrs };\n  return {\n    end(success: boolean, extra: GenAiAttrs = {}, error?: Error) {\n      const duration = Math.round(performance.now() - start);\n      const properties = { ...baseProps, ...extra };\n      appInsights.trackDependencyData({\n        id, name, duration, success,\n        responseCode: error ? 500 : 200,\n        type: \"GenAI\",\n        target: String(attrs[\"gen_ai.provider.name\"] ?? \"genai\"),\n        properties: properties as Record\u003Cstring, string>\n      });\n      if (error) {\n        appInsights.trackException({\n          exception: error,\n          severityLevel: SeverityLevel.Error,\n          properties: { ...properties, \"error.type\": error.name } as Record\u003Cstring, string>\n        });\n      }\n    }\n  };\n}\n\n\u002F\u002F Agent invocation\nconst agentSpan = startGenAiSpan(\"invoke_agent ResearchAssistant\", {\n  \"gen_ai.operation.name\": \"invoke_agent\",\n  \"gen_ai.provider.name\": \"azure.ai.openai\",\n  \"gen_ai.agent.name\": \"ResearchAssistant\",\n  \"gen_ai.agent.id\": \"asst_5j66UpCpwteGg4YSxUnt7lPY\",\n  \"gen_ai.request.model\": \"gpt-4o-mini\",\n  \"server.address\": \"myresource.openai.azure.com\"\n});\n\ntry {\n  \u002F\u002F Nested chat completion span\n  const chat = startGenAiSpan(\"chat gpt-4o-mini\", {\n    \"gen_ai.operation.name\": \"chat\",\n    \"gen_ai.provider.name\": \"azure.ai.openai\",\n    \"gen_ai.request.model\": \"gpt-4o-mini\"\n  });\n  const res = await callAzureOpenAi(\u002F* ... *\u002F);\n  chat.end(true, {\n    \"gen_ai.response.model\": res.model,\n    \"gen_ai.response.id\": res.id,\n    \"gen_ai.response.finish_reasons\": JSON.stringify(res.choices.map(c => c.finish_reason)),\n    \"gen_ai.usage.input_tokens\": res.usage.prompt_tokens,\n    \"gen_ai.usage.output_tokens\": res.usage.completion_tokens,\n    \"gen_ai.output.type\": \"text\"\n  });\n\n  \u002F\u002F Nested tool execution span\n  const tool = startGenAiSpan(\"execute_tool getWeather\", {\n    \"gen_ai.operation.name\": \"execute_tool\",\n    \"gen_ai.tool.name\": \"getWeather\",\n    \"gen_ai.tool.type\": \"function\",\n    \"gen_ai.tool.call.id\": \"call_abc123\"\n  });\n  const toolResult = await runGetWeather({ location: \"SF\" });\n  tool.end(true);\n\n  agentSpan.end(true, {\n    \"gen_ai.usage.input_tokens\": res.usage.prompt_tokens,\n    \"gen_ai.usage.output_tokens\": res.usage.completion_tokens\n  });\n} catch (err) {\n  agentSpan.end(false, { \"error.type\": (err as Error).name }, err as Error);\n}\n```\n\nThe browser's `traceparent` is automatically attached to outbound `fetch` (when `distributedTracingMode: 2`), so downstream Azure OpenAI \u002F agent backend spans hang under the same operation_Id in App Insights.\n\nFor the full attribute reference, well-known values, and content-capture guidance, see [references\u002Fagent-traces.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fagent-traces.md).\n\n### KQL: query GenAI traces in App Insights\n\n```kusto\ndependencies\n| where type == \"GenAI\"\n| extend op   = tostring(customDimensions[\"gen_ai.operation.name\"]),\n         agent = tostring(customDimensions[\"gen_ai.agent.name\"]),\n         model = tostring(customDimensions[\"gen_ai.request.model\"]),\n         tin   = toint(customDimensions[\"gen_ai.usage.input_tokens\"]),\n         tout  = toint(customDimensions[\"gen_ai.usage.output_tokens\"])\n| summarize calls=count(), p95_ms=percentile(duration, 95),\n            avg_in=avg(tin), avg_out=avg(tout) by op, agent, model, bin(timestamp, 5m)\n```\n\n## React (TypeScript)\n\nSee [references\u002Fframework-extensions.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fframework-extensions.md) for full React, React Native, Angular, Next.js, and Vite recipes.\n\n```typescript\nimport { ApplicationInsights } from \"@microsoft\u002Fapplicationinsights-web\";\nimport { ReactPlugin, withAITracking } from \"@microsoft\u002Fapplicationinsights-react-js\";\nimport { createBrowserHistory } from \"history\";\n\nconst reactPlugin = new ReactPlugin();\nconst browserHistory = createBrowserHistory();\n\nexport const appInsights = new ApplicationInsights({\n  config: {\n    connectionString: import.meta.env.VITE_APPINSIGHTS_CONNECTION_STRING,\n    extensions: [reactPlugin],\n    extensionConfig: { [reactPlugin.identifier]: { history: browserHistory } }\n  }\n});\nappInsights.loadAppInsights();\n\nexport const TrackedCheckout = withAITracking(reactPlugin, Checkout, \"Checkout\");\n```\n\n## React Native\n\n```typescript\nimport { ApplicationInsights } from \"@microsoft\u002Fapplicationinsights-web\";\nimport { ReactNativePlugin } from \"@microsoft\u002Fapplicationinsights-react-native\";\n\nconst rnPlugin = new ReactNativePlugin();\nconst appInsights = new ApplicationInsights({\n  config: {\n    connectionString: process.env.EXPO_PUBLIC_APPINSIGHTS_CONNECTION_STRING,\n    extensions: [rnPlugin],\n    disableFetchTracking: false\n  }\n});\nappInsights.loadAppInsights();\n```\n\n## Performance — Web Vitals\n\nAuto-collected: page-load timings via `PerformanceTiming` \u002F `PerformanceNavigationTiming`. To add Core Web Vitals:\n\n```typescript\nimport { onCLS, onLCP, onINP, type Metric } from \"web-vitals\";\n\nfunction send(m: Metric) {\n  appInsights.trackMetric(\n    { name: `web_vitals.${m.name.toLowerCase()}`, average: m.value },\n    { rating: m.rating, navigationType: m.navigationType, id: m.id }\n  );\n}\nonCLS(send); onLCP(send); onINP(send);\n```\n\n## Cookies & Privacy\n\n```typescript\nnew ApplicationInsights({ config: {\n  connectionString,\n  isCookieUseDisabled: true,         \u002F\u002F hard-disable all cookies\n  cookieCfg: { enabled: true, domain: \".example.com\", path: \"\u002F\", expiry: 365 }\n}});\n```\n\nTo honor consent dynamically:\n\n```typescript\nappInsights.getCookieMgr().setEnabled(userGaveConsent);\nappInsights.config.disableTelemetry = !userGaveConsent;\n```\n\n## Sampling\n\nServer-side ingestion sampling (recommended) is configured on the App Insights resource. SDK-side sampling reduces network use:\n\n```typescript\nnew ApplicationInsights({ config: { connectionString, samplingPercentage: 50 } });\n```\n\nPer-type sampling via telemetry initializer: drop with `return false` based on `item.baseType`.\n\n## Offline \u002F Send-on-Unload\n\nThe SDK uses `sendBeacon` (default `onunloadDisableBeacon: false`) to flush on `pagehide` \u002F `unload`. For SPAs, also call `appInsights.flush()` before destructive transitions (logout, hard reload).\n\n## Common Pitfalls\n\n1. **Do not initialize twice.** Re-importing the module under different bundles produces duplicate page views. Use a single shared module export.\n2. **Initialize before first user input** to avoid losing early clicks\u002Fexceptions.\n3. **Connection string is public** — never reuse the same App Insights resource for backend secrets.\n4. **`enableAutoRouteTracking` + manual `trackPageView`** = duplicates. Pick one.\n5. **CORS distributed tracing** requires the API to allow `Request-Id`, `Request-Context`, `traceparent`, `tracestate` request headers and expose `Request-Context` response header.\n6. **GenAI sensitive content** (`gen_ai.input.messages` etc.) is Opt-In — never log without an explicit runtime flag and approved data handling.\n7. **Agent token usage is on `chat` spans, not `invoke_agent`** — copy aggregated usage to the parent agent span only if you know it.\n8. **React StrictMode** double-invokes effects in dev — guard `loadAppInsights()` with a module-level singleton.\n\n## Bundle Size\n\nThe full web SDK is ~110 KB minified (~36 KB gzipped). For aggressive budgets, use the **Loader Script** path so the SDK loads asynchronously off the critical path, or tree-shake unused plugins.\n\n## Key Types\n\n```typescript\nimport {\n  ApplicationInsights,\n  SeverityLevel,\n  DistributedTracingModes,\n  type IConfiguration,\n  type IConfig,\n  type ITelemetryItem,\n  type ITelemetryPlugin,\n  type ICustomProperties,\n  type IPageViewTelemetry,\n  type IEventTelemetry,\n  type IExceptionTelemetry,\n  type ITraceTelemetry,\n  type IMetricTelemetry,\n  type IDependencyTelemetry\n} from \"@microsoft\u002Fapplicationinsights-web\";\n```\n\n## Best Practices\n\n1. **One singleton instance** exported from a single module.\n2. **Initialize early** in the app entrypoint, before router setup.\n3. **Use telemetry initializers** to attach `app.version`, `tenantId`, and to scrub PII \u002F query-string secrets.\n4. **Set `distributedTracingMode: 2`** and ensure your APIs accept\u002Fexpose W3C trace context headers.\n5. **For GenAI**, follow OTel `gen_ai.*` attribute names verbatim — they are queryable across browser and backend telemetry uniformly.\n6. **Gate sensitive content capture** (`gen_ai.input.messages` \u002F `gen_ai.output.messages`) behind a build-time or runtime opt-in.\n7. **Flush on logout \u002F sensitive navigation** so in-flight telemetry isn't dropped.\n\n## References\n\n- [references\u002Fagent-traces.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fagent-traces.md) — Full OTel GenAI semconv distilled (agent \u002F model \u002F tool spans, attributes, content capture).\n- [references\u002Fframework-extensions.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fframework-extensions.md) — React, React Native, Angular, Next.js, Vite recipes.\n- [references\u002Fconfiguration.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fconfiguration.md) — Full `IConfiguration` reference and tuning guide.\n- Microsoft Learn: \u003Chttps:\u002F\u002Flearn.microsoft.com\u002Fazure\u002Fazure-monitor\u002Fapp\u002Fjavascript-sdk>\n- ApplicationInsights-JS source: \u003Chttps:\u002F\u002Fgithub.com\u002Fmicrosoft\u002FApplicationInsights-JS>\n- OTel GenAI semantic conventions: \u003Chttps:\u002F\u002Fopentelemetry.io\u002Fdocs\u002Fspecs\u002Fsemconv\u002Fgen-ai\u002F>\n\n## Limitations\n\n- Use this skill only when the task clearly matches its upstream source and local project context.\n- Verify commands, generated code, dependencies, credentials, and external service behavior before applying changes.\n- Do not treat examples as a substitute for environment-specific tests, security review, or user approval for destructive or costly actions.\n","# Application Insights JavaScript SDK (Web) for TypeScript\n## When to Use\n\nUse this skill when you need instrument browser\u002Fweb apps with the Application Insights JavaScript SDK (@microsoft\u002Fapplicationinsights-web). Use for Real User Monitoring (RUM) — page views, clicks, AJAX\u002Ffetch dependencies, exceptions, custom events, and browser-side GenAI agent traces correlated to backend...\n\n\nReal User Monitoring (RUM) for browser apps with `@microsoft\u002Fapplicationinsights-web`. Auto-collects page views, AJAX\u002Ffetch dependencies, unhandled exceptions, and (with the Click Analytics plugin) clicks. Supports custom events, metrics, and **GenAI agent traces** that follow OpenTelemetry GenAI semantic conventions and correlate to backend spans via W3C Trace Context.\n\n> **Distinct from `azure-monitor-opentelemetry-ts`**, which is for Node.js server apps. This skill is for **browser\u002Fweb** code (and React Native).\n\n## Before Implementation\n\nSearch `microsoft-docs` MCP for current API patterns:\n\n- Query: \"Application Insights JavaScript SDK setup\"\n- Query: \"Application Insights JavaScript SDK configuration\"\n- Query: \"Application Insights JavaScript framework extensions React Angular\"\n- Verify package version: `npm view @microsoft\u002Fapplicationinsights-web version`\n\n## Packages\n\n| Package | Purpose |\n| --- | --- |\n| `@microsoft\u002Fapplicationinsights-web` | Core RUM SDK (page views, AJAX, exceptions). |\n| `@microsoft\u002Fapplicationinsights-clickanalytics-js` | Auto-collect click telemetry. |\n| `@microsoft\u002Fapplicationinsights-react-js` | React plugin (router instrumentation, hooks, HOC, ErrorBoundary). |\n| `@microsoft\u002Fapplicationinsights-react-native` | React Native plugin (native crashes, sessions). |\n| `@microsoft\u002Fapplicationinsights-angularplugin-js` | Angular plugin (router events, ErrorHandler). |\n| `@microsoft\u002Fapplicationinsights-debugplugin-js` | Dev-only telemetry inspector. |\n| `@microsoft\u002Fapplicationinsights-perfmarkmeasure-js` | User Timing (`performance.mark\u002Fmeasure`) integration. |\n\n## Installation\n\n```bash\nnpm i --save @microsoft\u002Fapplicationinsights-web\n# Optional plugins (install only what you use):\nnpm i --save @microsoft\u002Fapplicationinsights-clickanalytics-js\nnpm i --save @microsoft\u002Fapplicationinsights-react-js @microsoft\u002Fapplicationinsights-react-native @microsoft\u002Fapplicationinsights-angularplugin-js\n```\n\nTypings ship with the package — no separate `@types\u002F...` install needed.\n\n## Connection String\n\nThe browser SDK requires a connection string at init time. **It ships in plaintext to clients** — Microsoft Entra ID auth is not supported for browser telemetry. Use a separate App Insights resource with local auth enabled for browser RUM if you need to isolate it from backend telemetry.\n\n```bash\n# Vite \u002F CRA \u002F Next.js — expose to client via the public env prefix\nVITE_APPINSIGHTS_CONNECTION_STRING=\"InstrumentationKey=...;IngestionEndpoint=https:\u002F\u002F...;LiveEndpoint=https:\u002F\u002F...\"\nNEXT_PUBLIC_APPINSIGHTS_CONNECTION_STRING=\"InstrumentationKey=...\"\n```\n\n## Quick Start (npm)\n\n```typescript\nimport { ApplicationInsights } from \"@microsoft\u002Fapplicationinsights-web\";\n\nexport const appInsights = new ApplicationInsights({\n  config: {\n    connectionString: import.meta.env.VITE_APPINSIGHTS_CONNECTION_STRING,\n    enableAutoRouteTracking: true,        \u002F\u002F SPA route changes -> page views\n    enableCorsCorrelation: true,          \u002F\u002F propagate Request-Id \u002F traceparent to cross-origin AJAX\n    enableRequestHeaderTracking: true,\n    enableResponseHeaderTracking: true,\n    distributedTracingMode: 2,            \u002F\u002F DistributedTracingModes.AI_AND_W3C — emit traceparent for backend correlation\n    autoTrackPageVisitTime: true,\n    disableFetchTracking: false,          \u002F\u002F fetch() is auto-instrumented by default\n    excludeRequestFromAutoTrackingPatterns: [\u002Flivemetrics\\.azure\\.com\u002Fi]\n  }\n});\n\nappInsights.loadAppInsights();\nappInsights.trackPageView();\n```\n\nCall `loadAppInsights()` exactly once, as early as possible (before user interactions you want tracked). Then `trackPageView()` for the initial load — when `enableAutoRouteTracking` is on, subsequent route changes are automatic.\n\n## Quick Start (SDK Loader Script)\n\nRecommended when you want auto-updating SDK and zero build pipeline. Paste this as the **first** `\u003Cscript>` in `\u003Chead>`:\n\n```html\n\u003Cscript type=\"text\u002Fjavascript\" src=\"https:\u002F\u002Fjs.monitor.azure.com\u002Fscripts\u002Fb\u002Fai.3.gbl.min.js\" crossorigin=\"anonymous\">\u003C\u002Fscript>\n\u003Cscript type=\"text\u002Fjavascript\">\n  var appInsights = window.appInsights || function (cfg) {\n    \u002F* See: https:\u002F\u002Flearn.microsoft.com\u002Fazure\u002Fazure-monitor\u002Fapp\u002Fjavascript-sdk\n       Use the latest snippet from the Microsoft Learn page above — it includes\n       backup-CDN failover (cr), SDK-load-failure reporting, and the queue shim\n       so calls before SDK ready are not lost. *\u002F\n  }({ src: \"https:\u002F\u002Fjs.monitor.azure.com\u002Fscripts\u002Fb\u002Fai.3.gbl.min.js\",\n      crossOrigin: \"anonymous\",\n      cfg: { connectionString: \"YOUR_CONNECTION_STRING\" } });\n\u003C\u002Fscript>\n```\n\nLoader-only API (queued until SDK loads): `trackEvent`, `trackPageView`, `trackException`, `trackTrace`, `trackDependencyData`, `trackMetric`, `trackPageViewPerformance`, `startTrackPage`, `stopTrackPage`, `startTrackEvent`, `stopTrackEvent`, `addTelemetryInitializer`, `setAuthenticatedUserContext`, `clearAuthenticatedUserContext`, `flush`.\n\n## Core Tracking APIs\n\n```typescript\n\u002F\u002F Page views (SPAs that disable enableAutoRouteTracking)\nappInsights.trackPageView({ name: \"Checkout\", uri: \"\u002Fcheckout\", properties: { cartSize: 3 } });\n\n\u002F\u002F Custom events (user actions, business events)\nappInsights.trackEvent({ name: \"PurchaseCompleted\" }, { orderId: \"ord_123\", amountUsd: 49.95 });\n\n\u002F\u002F Exceptions (caught errors)\ntry {\n  await pay(order);\n} catch (err) {\n  appInsights.trackException({ exception: err as Error, severityLevel: 3, properties: { orderId: order.id } });\n}\n\n\u002F\u002F Traces (logs, severity 0=Verbose, 1=Info, 2=Warning, 3=Error, 4=Critical)\nappInsights.trackTrace({ message: \"Cart hydrated from local storage\", severityLevel: 1 });\n\n\u002F\u002F Custom metrics (numeric)\nappInsights.trackMetric({ name: \"checkout.duration_ms\", average: 1234 });\n\n\u002F\u002F Dependencies (manually-tracked outbound calls — fetch\u002FXHR are auto-tracked)\nappInsights.trackDependencyData({\n  id: crypto.randomUUID(),\n  name: \"GET \u002Fapi\u002Forders\",\n  duration: 87, success: true, responseCode: 200,\n  data: \"https:\u002F\u002Fapi.example.com\u002Fapi\u002Forders\", target: \"api.example.com\", type: \"Fetch\"\n});\n\n\u002F\u002F User identity (set ONCE per authenticated session — values are PII; do not pass emails)\nappInsights.setAuthenticatedUserContext(\"user-id-123\", \"tenant-456\", \u002F*storeInCookie*\u002F true);\nappInsights.clearAuthenticatedUserContext(); \u002F\u002F on logout\n\n\u002F\u002F Force send before unload\nappInsights.flush();\n```\n\n## Telemetry Initializers (enrichment & filtering)\n\nRun for every envelope before send. Return `false` to drop.\n\n```typescript\nimport type { ITelemetryItem } from \"@microsoft\u002Fapplicationinsights-web\";\n\nappInsights.addTelemetryInitializer((item: ITelemetryItem) => {\n  item.tags ??= {};\n  item.tags[\"ai.cloud.role\"] = \"web-shop\";\n  item.tags[\"ai.cloud.roleInstance\"] = window.location.hostname;\n  item.data ??= {};\n  item.data[\"app.version\"] = import.meta.env.VITE_APP_VERSION;\n  item.data[\"app.build\"] = import.meta.env.VITE_BUILD_SHA;\n\n  \u002F\u002F Drop noisy health-check page views\n  if (item.baseType === \"PageviewData\" && item.baseData?.uri?.endsWith(\"\u002Fhealthz\")) return false;\n\n  \u002F\u002F Scrub query-string secrets\n  if (item.baseData?.uri) {\n    item.baseData.uri = item.baseData.uri.replace(\u002F([?&](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Ftoken|sig|key)=)[^&]+\u002Fgi, \"$1REDACTED\");\n  }\n});\n```\n\n## Click Analytics\n\n```typescript\nimport { ClickAnalyticsPlugin } from \"@microsoft\u002Fapplicationinsights-clickanalytics-js\";\n\nconst clickPlugin = new ClickAnalyticsPlugin();\nconst appInsights = new ApplicationInsights({\n  config: {\n    connectionString: import.meta.env.VITE_APPINSIGHTS_CONNECTION_STRING,\n    extensions: [clickPlugin],\n    extensionConfig: {\n      [clickPlugin.identifier]: {\n        autoCapture: true,\n        dataTags: { useDefaultContentNameOrId: true, customDataPrefix: \"data-ai-\" },\n        urlCollectHash: false,\n        behaviorValidator: (b: string) => \u002F^[a-z0-9_]+$\u002F.test(b) ? b : \"\"\n      }\n    }\n  }\n});\nappInsights.loadAppInsights();\n```\n\nMark elements with `data-ai-*` attributes; clicks are emitted as Custom Events with parent-content metadata.\n\n## SPA Route Tracking\n\n- **Built-in:** set `enableAutoRouteTracking: true`. Hooks `history.pushState\u002FreplaceState` and `popstate`.\n- **React Router:** use `@microsoft\u002Fapplicationinsights-react-js` `withAITracking` HOC (see [references\u002Fframework-extensions.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fframework-extensions.md)).\n- **Manual:** call `appInsights.trackPageView({ name, uri })` in your router's `useEffect` on route change. Disable `enableAutoRouteTracking` to avoid double counting.\n\n## Distributed Tracing (correlate to backend)\n\nSet `distributedTracingMode: 2` (`DistributedTracingModes.AI_AND_W3C`). The SDK adds `traceparent` (and legacy `Request-Id`) to outbound `fetch`\u002F`XHR`. Backends instrumented with **OpenTelemetry** (e.g. `@azure\u002Fmonitor-opentelemetry`) auto-link to the browser's operation_Id.\n\nFor cross-origin calls, also set `enableCorsCorrelation: true` and add the calling origin to the **CORS exposed headers** on the API.\n\n## GenAI Agent Traces (OTel semantic conventions)\n\nWhen the browser invokes an AI agent (function-calling, tool-use, model calls direct from the client), emit App Insights **Dependency** telemetry whose attributes follow the OpenTelemetry **GenAI semantic conventions** so they are queryable alongside backend agent spans in App Insights \u002F Log Analytics.\n\n**Set the opt-in env first** so backend instrumentations agree on the same schema version:\n\n```bash\nOTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental\n```\n\n### Required attribute keys (use the OTel names verbatim)\n\n| Span \u002F op | Required attributes |\n| --- | --- |\n| `invoke_agent {agent.name}` | `gen_ai.operation.name=invoke_agent`, `gen_ai.provider.name`, `gen_ai.agent.name`, `gen_ai.agent.id` (when known) |\n| `create_agent {agent.name}` | `gen_ai.operation.name=create_agent`, `gen_ai.provider.name`, `gen_ai.agent.name`, `gen_ai.request.model` |\n| `chat {model}` | `gen_ai.operation.name=chat`, `gen_ai.provider.name`, `gen_ai.request.model`, `gen_ai.response.model`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens` |\n| `execute_tool {tool.name}` | `gen_ai.operation.name=execute_tool`, `gen_ai.tool.name`, `gen_ai.tool.type` (`function` \\| `extension` \\| `datastore`), `gen_ai.tool.call.id` |\n\n`gen_ai.provider.name` well-known values: `openai`, `azure.ai.openai`, `azure.ai.inference`, `anthropic`, `aws.bedrock`, `gcp.gemini`, `gcp.vertex_ai`, `cohere`, `mistral_ai`, `groq`, `deepseek`, `perplexity`, `x_ai`, `ibm.watsonx.ai`.\n\n> **Sensitive content opt-in.** `gen_ai.system_instructions`, `gen_ai.input.messages`, `gen_ai.output.messages`, `gen_ai.tool.call.arguments`, `gen_ai.tool.call.result` are **Opt-In** by default. Gate them behind a runtime flag and avoid them in production unless you have approved data handling.\n\n### Pattern: invoke_agent + nested tool\u002Fmodel spans\n\n```typescript\nimport { ApplicationInsights, SeverityLevel } from \"@microsoft\u002Fapplicationinsights-web\";\n\ntype GenAiAttrs = Record\u003Cstring, string | number | boolean | undefined>;\n\nfunction startGenAiSpan(name: string, attrs: GenAiAttrs) {\n  const id = crypto.randomUUID();\n  const start = performance.now();\n  const baseProps: GenAiAttrs = { \"gen_ai.span.id\": id, ...attrs };\n  return {\n    end(success: boolean, extra: GenAiAttrs = {}, error?: Error) {\n      const duration = Math.round(performance.now() - start);\n      const properties = { ...baseProps, ...extra };\n      appInsights.trackDependencyData({\n        id, name, duration, success,\n        responseCode: error ? 500 : 200,\n        type: \"GenAI\",\n        target: String(attrs[\"gen_ai.provider.name\"] ?? \"genai\"),\n        properties: properties as Record\u003Cstring, string>\n      });\n      if (error) {\n        appInsights.trackException({\n          exception: error,\n          severityLevel: SeverityLevel.Error,\n          properties: { ...properties, \"error.type\": error.name } as Record\u003Cstring, string>\n        });\n      }\n    }\n  };\n}\n\n\u002F\u002F Agent invocation\nconst agentSpan = startGenAiSpan(\"invoke_agent ResearchAssistant\", {\n  \"gen_ai.operation.name\": \"invoke_agent\",\n  \"gen_ai.provider.name\": \"azure.ai.openai\",\n  \"gen_ai.agent.name\": \"ResearchAssistant\",\n  \"gen_ai.agent.id\": \"asst_5j66UpCpwteGg4YSxUnt7lPY\",\n  \"gen_ai.request.model\": \"gpt-4o-mini\",\n  \"server.address\": \"myresource.openai.azure.com\"\n});\n\ntry {\n  \u002F\u002F Nested chat completion span\n  const chat = startGenAiSpan(\"chat gpt-4o-mini\", {\n    \"gen_ai.operation.name\": \"chat\",\n    \"gen_ai.provider.name\": \"azure.ai.openai\",\n    \"gen_ai.request.model\": \"gpt-4o-mini\"\n  });\n  const res = await callAzureOpenAi(\u002F* ... *\u002F);\n  chat.end(true, {\n    \"gen_ai.response.model\": res.model,\n    \"gen_ai.response.id\": res.id,\n    \"gen_ai.response.finish_reasons\": JSON.stringify(res.choices.map(c => c.finish_reason)),\n    \"gen_ai.usage.input_tokens\": res.usage.prompt_tokens,\n    \"gen_ai.usage.output_tokens\": res.usage.completion_tokens,\n    \"gen_ai.output.type\": \"text\"\n  });\n\n  \u002F\u002F Nested tool execution span\n  const tool = startGenAiSpan(\"execute_tool getWeather\", {\n    \"gen_ai.operation.name\": \"execute_tool\",\n    \"gen_ai.tool.name\": \"getWeather\",\n    \"gen_ai.tool.type\": \"function\",\n    \"gen_ai.tool.call.id\": \"call_abc123\"\n  });\n  const toolResult = await runGetWeather({ location: \"SF\" });\n  tool.end(true);\n\n  agentSpan.end(true, {\n    \"gen_ai.usage.input_tokens\": res.usage.prompt_tokens,\n    \"gen_ai.usage.output_tokens\": res.usage.completion_tokens\n  });\n} catch (err) {\n  agentSpan.end(false, { \"error.type\": (err as Error).name }, err as Error);\n}\n```\n\nThe browser's `traceparent` is automatically attached to outbound `fetch` (when `distributedTracingMode: 2`), so downstream Azure OpenAI \u002F agent backend spans hang under the same operation_Id in App Insights.\n\nFor the full attribute reference, well-known values, and content-capture guidance, see [references\u002Fagent-traces.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fagent-traces.md).\n\n### KQL: query GenAI traces in App Insights\n\n```kusto\ndependencies\n| where type == \"GenAI\"\n| extend op   = tostring(customDimensions[\"gen_ai.operation.name\"]),\n         agent = tostring(customDimensions[\"gen_ai.agent.name\"]),\n         model = tostring(customDimensions[\"gen_ai.request.model\"]),\n         tin   = toint(customDimensions[\"gen_ai.usage.input_tokens\"]),\n         tout  = toint(customDimensions[\"gen_ai.usage.output_tokens\"])\n| summarize calls=count(), p95_ms=percentile(duration, 95),\n            avg_in=avg(tin), avg_out=avg(tout) by op, agent, model, bin(timestamp, 5m)\n```\n\n## React (TypeScript)\n\nSee [references\u002Fframework-extensions.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fframework-extensions.md) for full React, React Native, Angular, Next.js, and Vite recipes.\n\n```typescript\nimport { ApplicationInsights } from \"@microsoft\u002Fapplicationinsights-web\";\nimport { ReactPlugin, withAITracking } from \"@microsoft\u002Fapplicationinsights-react-js\";\nimport { createBrowserHistory } from \"history\";\n\nconst reactPlugin = new ReactPlugin();\nconst browserHistory = createBrowserHistory();\n\nexport const appInsights = new ApplicationInsights({\n  config: {\n    connectionString: import.meta.env.VITE_APPINSIGHTS_CONNECTION_STRING,\n    extensions: [reactPlugin],\n    extensionConfig: { [reactPlugin.identifier]: { history: browserHistory } }\n  }\n});\nappInsights.loadAppInsights();\n\nexport const TrackedCheckout = withAITracking(reactPlugin, Checkout, \"Checkout\");\n```\n\n## React Native\n\n```typescript\nimport { ApplicationInsights } from \"@microsoft\u002Fapplicationinsights-web\";\nimport { ReactNativePlugin } from \"@microsoft\u002Fapplicationinsights-react-native\";\n\nconst rnPlugin = new ReactNativePlugin();\nconst appInsights = new ApplicationInsights({\n  config: {\n    connectionString: process.env.EXPO_PUBLIC_APPINSIGHTS_CONNECTION_STRING,\n    extensions: [rnPlugin],\n    disableFetchTracking: false\n  }\n});\nappInsights.loadAppInsights();\n```\n\n## Performance — Web Vitals\n\nAuto-collected: page-load timings via `PerformanceTiming` \u002F `PerformanceNavigationTiming`. To add Core Web Vitals:\n\n```typescript\nimport { onCLS, onLCP, onINP, type Metric } from \"web-vitals\";\n\nfunction send(m: Metric) {\n  appInsights.trackMetric(\n    { name: `web_vitals.${m.name.toLowerCase()}`, average: m.value },\n    { rating: m.rating, navigationType: m.navigationType, id: m.id }\n  );\n}\nonCLS(send); onLCP(send); onINP(send);\n```\n\n## Cookies & Privacy\n\n```typescript\nnew ApplicationInsights({ config: {\n  connectionString,\n  isCookieUseDisabled: true,         \u002F\u002F hard-disable all cookies\n  cookieCfg: { enabled: true, domain: \".example.com\", path: \"\u002F\", expiry: 365 }\n}});\n```\n\nTo honor consent dynamically:\n\n```typescript\nappInsights.getCookieMgr().setEnabled(userGaveConsent);\nappInsights.config.disableTelemetry = !userGaveConsent;\n```\n\n## Sampling\n\nServer-side ingestion sampling (recommended) is configured on the App Insights resource. SDK-side sampling reduces network use:\n\n```typescript\nnew ApplicationInsights({ config: { connectionString, samplingPercentage: 50 } });\n```\n\nPer-type sampling via telemetry initializer: drop with `return false` based on `item.baseType`.\n\n## Offline \u002F Send-on-Unload\n\nThe SDK uses `sendBeacon` (default `onunloadDisableBeacon: false`) to flush on `pagehide` \u002F `unload`. For SPAs, also call `appInsights.flush()` before destructive transitions (logout, hard reload).\n\n## Common Pitfalls\n\n1. **Do not initialize twice.** Re-importing the module under different bundles produces duplicate page views. Use a single shared module export.\n2. **Initialize before first user input** to avoid losing early clicks\u002Fexceptions.\n3. **Connection string is public** — never reuse the same App Insights resource for backend secrets.\n4. **`enableAutoRouteTracking` + manual `trackPageView`** = duplicates. Pick one.\n5. **CORS distributed tracing** requires the API to allow `Request-Id`, `Request-Context`, `traceparent`, `tracestate` request headers and expose `Request-Context` response header.\n6. **GenAI sensitive content** (`gen_ai.input.messages` etc.) is Opt-In — never log without an explicit runtime flag and approved data handling.\n7. **Agent token usage is on `chat` spans, not `invoke_agent`** — copy aggregated usage to the parent agent span only if you know it.\n8. **React StrictMode** double-invokes effects in dev — guard `loadAppInsights()` with a module-level singleton.\n\n## Bundle Size\n\nThe full web SDK is ~110 KB minified (~36 KB gzipped). For aggressive budgets, use the **Loader Script** path so the SDK loads asynchronously off the critical path, or tree-shake unused plugins.\n\n## Key Types\n\n```typescript\nimport {\n  ApplicationInsights,\n  SeverityLevel,\n  DistributedTracingModes,\n  type IConfiguration,\n  type IConfig,\n  type ITelemetryItem,\n  type ITelemetryPlugin,\n  type ICustomProperties,\n  type IPageViewTelemetry,\n  type IEventTelemetry,\n  type IExceptionTelemetry,\n  type ITraceTelemetry,\n  type IMetricTelemetry,\n  type IDependencyTelemetry\n} from \"@microsoft\u002Fapplicationinsights-web\";\n```\n\n## Best Practices\n\n1. **One singleton instance** exported from a single module.\n2. **Initialize early** in the app entrypoint, before router setup.\n3. **Use telemetry initializers** to attach `app.version`, `tenantId`, and to scrub PII \u002F query-string secrets.\n4. **Set `distributedTracingMode: 2`** and ensure your APIs accept\u002Fexpose W3C trace context headers.\n5. **For GenAI**, follow OTel `gen_ai.*` attribute names verbatim — they are queryable across browser and backend telemetry uniformly.\n6. **Gate sensitive content capture** (`gen_ai.input.messages` \u002F `gen_ai.output.messages`) behind a build-time or runtime opt-in.\n7. **Flush on logout \u002F sensitive navigation** so in-flight telemetry isn't dropped.\n\n## References\n\n- [references\u002Fagent-traces.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fagent-traces.md) — Full OTel GenAI semconv distilled (agent \u002F model \u002F tool spans, attributes, content capture).\n- [references\u002Fframework-extensions.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fframework-extensions.md) — React, React Native, Angular, Next.js, Vite recipes.\n- [references\u002Fconfiguration.md](https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts\u002Freferences\u002Fconfiguration.md) — Full `IConfiguration` reference and tuning guide.\n- Microsoft Learn: \u003Chttps:\u002F\u002Flearn.microsoft.com\u002Fazure\u002Fazure-monitor\u002Fapp\u002Fjavascript-sdk>\n- ApplicationInsights-JS source: \u003Chttps:\u002F\u002Fgithub.com\u002Fmicrosoft\u002FApplicationInsights-JS>\n- OTel GenAI semantic conventions: \u003Chttps:\u002F\u002Fopentelemetry.io\u002Fdocs\u002Fspecs\u002Fsemconv\u002Fgen-ai\u002F>\n\n## Limitations\n\n- Use this skill only when the task clearly matches its upstream source and local project context.\n- Verify commands, generated code, dependencies, credentials, and external service behavior before applying changes.\n- Do not treat examples as a substitute for environment-specific tests, security review, or user approval for destructive or costly actions.\n",{"name":5,"risk":24,"source":25,"license":15,"date_added":26,"description":7,"source_repo":27,"source_type":28,"license_source":29},"unknown","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Ftree\u002Fmain\u002F.github\u002Fplugins\u002Fazure-sdk-typescript\u002Fskills\u002Fapplicationinsights-web-ts",1782864000000,"microsoft\u002Fskills","official","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fskills\u002Fblob\u002Fmain\u002FLICENSE","main","b1e4ffadb4f933bbbcaa03a83f8b9929171ac28b",".agents\u002Fskills\u002Fapplicationinsights-web-ts",0,"https:\u002F\u002Fgithub.com\u002FHazek-dc\u002FPresensiGuru_FaceNet_EMAR","https:\u002F\u002Fgithub.com\u002FHazek-dc","2026-10-22 08:36:44",{"source":8,"skillName":5,"commands":38,"prompt":48,"zipAvailable":49},[39,42,45],{"tool":40,"command":41},"npx","npx skills add Hazek-dc\u002FPresensiGuru_FaceNet_EMAR --skill applicationinsights-web-ts",{"tool":43,"command":44},"bunx","bunx skills add Hazek-dc\u002FPresensiGuru_FaceNet_EMAR --skill applicationinsights-web-ts",{"tool":46,"command":47},"pnpm","pnpm dlx skills add Hazek-dc\u002FPresensiGuru_FaceNet_EMAR --skill applicationinsights-web-ts","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FHazek-dc\u002FPresensiGuru_FaceNet_EMAR\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Fapplicationinsights-web-ts\u002FSKILL.md",true,[],[52,59,66,73,80,87],{"skillNo":53,"name":54,"nameZh":6,"description":55,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":56,"categories":57,"keywords":58,"publishedAt":19},"01M7DS6GGYRZAYD33PFPS8Z2T3","testing-best-practices","Laravel test design and review. Use when selecting coverage, naming or structuring tests, choosing assertions or test data, isolating dependencies, testing HTTP or security boundaries, improving suite performance, or reviewing test value. Use framework guidance or search-docs for Pest and PHPUnit syntax.","https:\u002F\u002Fgithub.com\u002FHazek-dc\u002FPresensiGuru_FaceNet_EMAR\u002Fblob\u002Fmain\u002Fattendance-web\u002F.claude\u002Fskills\u002Ftesting-best-practices\u002FSKILL.md",[],[],{"skillNo":60,"name":61,"nameZh":6,"description":62,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":63,"categories":64,"keywords":65,"publishedAt":19},"01M7DS6BC3AW7HTPRHE2TTVKWM","tailwindcss-development","Always invoke when the user's message includes 'tailwind' in any form. Also invoke for: building responsive grid layouts (multi-column card grids, product grids), flex\u002Fgrid page structures (dashboards with sidebars, fixed topbars, mobile-toggle navs), styling UI components (cards, tables, navbars, pricing sections, forms, inputs, badges), adding dark mode variants, fixing spacing or typography, and Tailwind v3\u002Fv4 work. The core use case: writing or fixing Tailwind utility classes in HTML templates (Blade, JSX, Vue). Skip for backend PHP logic, database queries, API routes, JavaScript with no HTML\u002FCSS component, CSS file audits, build tool configuration, and vanilla CSS.","https:\u002F\u002Fgithub.com\u002FHazek-dc\u002FPresensiGuru_FaceNet_EMAR\u002Fblob\u002Fmain\u002Fattendance-web\u002F.claude\u002Fskills\u002Ftailwindcss-development\u002FSKILL.md",[],[],{"skillNo":67,"name":68,"nameZh":6,"description":69,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":70,"categories":71,"keywords":72,"publishedAt":19},"01M7DS66GE6B11MYJDVC74HXM4","laravel-best-practices","Apply this skill whenever writing, reviewing, or refactoring Laravel PHP code. This includes creating or modifying controllers, models, migrations, form requests, policies, jobs, scheduled commands, service classes, and Eloquent queries. Triggers for N+1 and query performance issues, caching strategies, authorization and security patterns, validation, error handling, queue and job configuration, route definitions, and architectural decisions. Also use for Laravel code reviews and refactoring existing Laravel code to follow best practices. Covers any task involving Laravel backend PHP code patterns.","https:\u002F\u002Fgithub.com\u002FHazek-dc\u002FPresensiGuru_FaceNet_EMAR\u002Fblob\u002Fmain\u002Fattendance-web\u002F.claude\u002Fskills\u002Flaravel-best-practices\u002FSKILL.md",[],[],{"skillNo":74,"name":75,"nameZh":6,"description":76,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":77,"categories":78,"keywords":79,"publishedAt":19},"01M7DS5ZW717DMS6P1E90AQCKV","infer-conventions","Use this skill to analyze how a Laravel application is actually written and record its conventions as shared rules. Trigger when the user wants to detect, infer, document, or standardize project conventions or coding style, set up or grow `.ai\u002Frules`, resolve mixed or conflicting patterns (e.g. \"are we using Form Requests or inline validation?\"), or onboard agents and teammates to \"how we do things here\". Covers: a systematic sweep of ~49 Laravel convention dimensions (validation, models, architecture, testing, frontend, database, console), open-ended house-pattern discovery, conflict reporting, and recording rules scoped to the right paths via the Boost `record-rule` MCP tool. Only run this skill when the user explicitly asks for it; never start a sweep as part of another task. Do not use for one-off code review, enforcing formatting a linter already handles, or editing `.ai\u002Frules` files by hand.","https:\u002F\u002Fgithub.com\u002FHazek-dc\u002FPresensiGuru_FaceNet_EMAR\u002Fblob\u002Fmain\u002Fattendance-web\u002F.claude\u002Fskills\u002Finfer-conventions\u002FSKILL.md",[],[],{"skillNo":81,"name":82,"nameZh":6,"description":83,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":84,"categories":85,"keywords":86,"publishedAt":19},"01M7DS5W12RGCVJTFQJ1N0GB8E","inertia-react-development","Develops Inertia.js v2 React client-side applications. Activates when creating React pages, forms, or navigation; using \u003CLink>, \u003CForm>, useForm, or router; working with deferred props, prefetching, or polling; or when user mentions React with Inertia, React pages, React forms, or React navigation.","https:\u002F\u002Fgithub.com\u002FHazek-dc\u002FPresensiGuru_FaceNet_EMAR\u002Fblob\u002Fmain\u002Fattendance-web\u002F.claude\u002Fskills\u002Finertia-react-development\u002FSKILL.md",[],[],{"skillNo":88,"name":89,"nameZh":6,"description":90,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":91,"categories":92,"keywords":93,"publishedAt":19},"01M7DS5PVDPMPCWSK2RXPDC9BB","deploying-to-cloud","Deploys and manages Laravel applications on Laravel Cloud using the `cloud` CLI. Use when the user wants to deploy an app, ship to Cloud, create or manage applications, environments, databases, caches, object storage, queues, domains, instances, background processes, secrets, compute, scheduled tasks, or Laravel Cloud infrastructure; or check billing, usage, or spend. Triggers on deployment, shipping, Cloud management, environment setup, database provisioning, billing or usage queries, the `cloud` CLI, and Laravel Cloud deployment troubleshooting.","https:\u002F\u002Fgithub.com\u002FHazek-dc\u002FPresensiGuru_FaceNet_EMAR\u002Fblob\u002Fmain\u002Fattendance-web\u002F.claude\u002Fskills\u002Fdeploying-to-cloud\u002FSKILL.md",[],[]]