PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/articles/application-insights/app-insights-javascript.md

https://gitlab.com/yeah568/azure-content
Markdown | 252 lines | 133 code | 119 blank | 0 comment | 0 complexity | 5d15ff76c0739d7c704baa149fb7838b MD5 | raw file
  1. <properties
  2. pageTitle="Application Insights for JavaScript web apps | Microsoft Azure"
  3. description="Get page view and session counts, web client data, and track usage patterns. Detect exceptions and performance issues in JavaScript web pages."
  4. services="application-insights"
  5. documentationCenter=""
  6. authors="alancameronwills"
  7. manager="douge"/>
  8. <tags
  9. ms.service="application-insights"
  10. ms.workload="tbd"
  11. ms.tgt_pltfrm="ibiza"
  12. ms.devlang="na"
  13. ms.topic="get-started-article"
  14. ms.date="05/18/2016"
  15. ms.author="awills"/>
  16. # Application Insights for web pages
  17. [AZURE.INCLUDE [app-insights-selector-get-started-dotnet](../../includes/app-insights-selector-get-started-dotnet.md)]
  18. Find out about the performance and usage of your web page or app. Add Visual Studio Application Insights to your page script, and you'll get timings of page loads and AJAX calls, counts and details of browser exceptions and AJAX failures, as well as counts of users and sessions. All these can be segmented by page, client OS and browser version, geo location, and other dimensions. You can also set alerts on failure counts or slow page loading.
  19. You can use Application Insights with any web pages - you just add a short piece of JavaScript. If your web service is [Java](app-insights-java-get-started.md) or [ASP.NET](app-insights-asp-net.md), you can integrate telemetry from your server and clients.
  20. You'll need a subscription to [Microsoft Azure](https://azure.com). If your team has an organizational subscription, ask the owner to add your Microsoft Account to it. There's a free pricing tier, so development and small-scale use won't cost anything.
  21. ## Set up Application Insights for your web page
  22. You might already have it. If your app is a new ASP.NET project and you chose to add Application Insights in the New Project dialog Visual Studio, the script will have been added then, and you're all set up.
  23. Otherwise, you need to add a snippet of code your web pages, as follows.
  24. ### Open an Application Insights resource
  25. The Application Insights resource is where data about your page's performance and usage is displayed.
  26. Sign into [Azure portal](https://portal.azure.com).
  27. If you already set up monitoring for the server side of your app, you'll already have a resource:
  28. ![Choose Browse, Developer Services, Application Insights.](./media/app-insights-javascript/01-find.png)
  29. If you don't have one, create it:
  30. ![Choose New, Developer Services, Application Insights.](./media/app-insights-javascript/01-create.png)
  31. *Questions already?* [More about creating a resource](app-insights-create-new-resource.md).
  32. ### Add the SDK script to your app or web pages
  33. In Quick Start, get the script for web pages:
  34. ![On your app overview blade, choose Quick Start, Get code to monitor my web pages. Copy the script.](./media/app-insights-javascript/02-monitor-web-page.png)
  35. Insert the script just before the `<head>` tag of every page you want to track. If your website has a master page, you can put the script there. For example:
  36. * In an ASP.NET MVC project, you'd put it in `View\Shared\_Layout.cshtml`
  37. * In a SharePoint site, on the control panel, open [Site Settings / Master Page](app-insights-sharepoint.md).
  38. The script contains the instrumentation key that directs the data to your Application Insights resource.
  39. ([Deeper explanation of the script.](http://apmtips.com/blog/2015/03/18/javascript-snippet-explained/))
  40. *(If you're using a well-known web page framework, look around for Application Insights adaptors. For example, there's [an AngularJS module](http://ngmodules.org/modules/angular-appinsights).)*
  41. ## Detailed configuration
  42. There are several [parameters](https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#config) you can set, though in most cases, you shouldn't need to. For example, you can disable or limit the number of Ajax calls reported per page view (to reduce traffic); or you can set debug mode to have telemetry move rapidly through the pipeline without being batched.
  43. To set these parameters, look for this line in the code snippet, and add more comma-separated items after it:
  44. })({
  45. instrumentationKey: "..."
  46. // Insert here
  47. });
  48. The [available parameters](https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#config) include:
  49. // Send telemetry immediately without batching.
  50. // Remember to remove this when no longer required, as it
  51. // can affect browser performance.
  52. enableDebug: true,
  53. // Don't log browser exceptions.
  54. disableExceptionTracking: true,
  55. // Don't log ajax calls.
  56. disableAjaxTracking: boolean,
  57. // Limit number of Ajax calls logged, to reduce traffic.
  58. maxAjaxCallsPerView: 10, // default is 500
  59. // Time page load up to execution of first trackPageView().
  60. overridePageViewDuration: boolean,
  61. // Set these dynamically for an authenticated user.
  62. appUserId: string,
  63. accountId: string,
  64. ## <a name="run"></a>Run your app
  65. Run your web app, use it a while to generate telemetry, and wait a few seconds. You can either run it using the **F5** key on your development machine, or publish it and let users play with it.
  66. If you want to check the telemetry that a web app is sending to Application Insights, use your browser's debugging tools (**F12** on many browsers). Data is sent to dc.services.visualstudio.com.
  67. ## Explore your browser performance data
  68. Open the Browsers blade to show aggregated performance data from your users' browsers.
  69. ![In portal.azure.com, open your app's resource and click Settings, Browser](./media/app-insights-javascript/03.png)
  70. *No data yet? Click **Refresh** at the top of the page. Still nothing? See [Troubleshooting](app-insights-troubleshoot-faq.md).*
  71. The Browsers blade is a [Metrics Explorer blade](app-insights-metrics-explorer.md) with preset filters and chart selections. You can edit the time range, filters and chart configuration if you want, and save the result as a favorite. Click **Restore defaults** to get back to the original blade configuration.
  72. ## Page load performance
  73. At the top is a segmented chart of page load times. The total height of the chart represents the average time to load and display pages from your app in your users' browsers. The time is measured from when the browser sends the initial HTTP request until all synchronous load events have been processed, including layout and running scripts. It doesn't include asynchronous tasks such as loading web parts from AJAX calls.
  74. The chart segments the total page load time into the [standard timings defined by W3C](http://www.w3.org/TR/navigation-timing/#processing-model).
  75. ![](./media/app-insights-javascript/08-client-split.png)
  76. Note that the *network connect* time is often lower than you might expect, because it's an average over all requests from the browser to the server. Many individual requests have a connect time of 0 because there is already an active connection to the server.
  77. ### Slow loading?
  78. Slow page loads are a major source of dissatisfaction for your users. If the chart indicates slow page loads, it's easy to do some diagnostic research.
  79. The chart shows the average of all page loads in your app. To see if the problem is confined to particular pages, look further down the blade, where there's a grid segmented by page URL:
  80. ![](./media/app-insights-javascript/09-page-perf.png)
  81. Notice the page view count and standard deviation. If the page count is very low, then the issue isn't affecting users much. A high standard deviation (comparable to the average itself) indicates a lot of variation between individual measurements.
  82. **Zoom in on one URL and one page view.** Click any page name to see a blade of browser charts filtered just to that URL; and then on an instance of a page view.
  83. ![](./media/app-insights-javascript/35.png)
  84. Click `...` for a full list of properties for that event, or inspect the Ajax calls and related events. Slow Ajax calls affect the overall page load time if they are synchronous. Related events include server requests for the same URL (if you've set up Application Insights on your web server).
  85. **Page performance over time.** Back at the Browsers blade, change the Page View Load Time grid into a line chart to see if there were peaks at particular times:
  86. ![Click the head of the grid and select a new chart type](./media/app-insights-javascript/10-page-perf-area.png)
  87. **Segment by other dimensions.** Maybe your pages are slower to load on a particular browser, client OS, or user locality? Add a new chart and experiment with the **Group-by** dimension.
  88. ![](./media/app-insights-javascript/21.png)
  89. ## AJAX Performance
  90. Make sure any AJAX calls in your web pages are performing well. They are often used to fill parts of your page asynchronously. Although the overall page might load promptly, your users could be frustrated by staring at blank web parts, waiting for data to appear in them.
  91. AJAX calls made from your web page are shown on the Browsers blade as dependencies.
  92. There are summary charts in the upper part of the blade:
  93. ![](./media/app-insights-javascript/31.png)
  94. and detailed grids lower down:
  95. ![](./media/app-insights-javascript/33.png)
  96. Click any row for specific details.
  97. > [AZURE.NOTE] If you delete the Browsers filter on the blade, both server and AJAX dependencies will be included in these charts. Click Restore Defaults to reconfigure the filter.
  98. **To drill into failed Ajax calls** scroll down to the Dependency failures grid, and then click a row to see specific instances.
  99. ![](./media/app-insights-javascript/37.png)
  100. Click `...` for the full telemetry for an Ajax call.
  101. ### No Ajax calls reported?
  102. Ajax calls include any HTTP calls made from the script of your web page. If you don't see them reported, check that the code snippet doesn't set the `disableAjaxTracking` or `maxAjaxCallsPerView` [parameters](https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#config).
  103. ## Browser exceptions
  104. On the Browsers blade, there's an exceptions summary chart, and a grid of exception types further down the blade.
  105. ![](./media/app-insights-javascript/39.png)
  106. If you don't see browser exceptions reported, check that the code snippet doesn't set the `disableExceptionTracking` [parameter](https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#config).
  107. ## Inspect individual page view events
  108. Usually page view telemetry is analyzed by Application Insights and you see only cumulative reports, averaged over all your users. But for debugging purposes, you can also look at individual page view events.
  109. In the Diagnostic Search blade, set Filters to Page View.
  110. ![](./media/app-insights-javascript/12-search-pages.png)
  111. Select any event to see more detail. In the details page, click "..." to see even more detail.
  112. > [AZURE.NOTE] If you use [Search](app-insights-diagnostic-search.md), notice that you have to match whole words: "Abou" and "bout" do not match "About", but "Abou* " does. And you cannot begin a search term with a wildcard. For example, searching for "*bou" would not match "About".
  113. > [Learn more about diagnostic search](app-insights-diagnostic-search.md)
  114. ### Page view properties
  115. * **Page view duration**
  116. * By default, the time it takes to load the page, from client request to full load (including auxiliary files but excluding asynchronous tasks such as Ajax calls).
  117. * If you set `overridePageViewDuration` in the [page configuration](#detailed-configuration), the interval between client request to execution of the first `trackPageView`. If you moved trackPageView from its usual position after the initialization of the script, it will reflect a different value.
  118. * If `overridePageViewDuration` is set and a duration argument is provided in the `trackPageView()` call, then the argument value will be used instead.
  119. ## Custom page counts
  120. By default, a page count occurs each time a new page loads into the client browser. But you might want to count additional page views. For example, a page might display its content in tabs and you want to count a page when the user switches tabs. Or JavaScript code in the page might load new content without changing the browser's URL.
  121. Insert a JavaScript call like this at the appropriate point in your client code:
  122. appInsights.trackPageView(myPageName);
  123. The page name can contain the same characters as a URL, but anything after "#" or "?" will be ignored.
  124. ## Usage tracking
  125. Want to find out what your users do with your app?
  126. * [Learn about usage tracking](app-insights-web-track-usage.md)
  127. * [Learn about custom events and metrics API](app-insights-api-custom-events-metrics.md).
  128. #### <a name="video"></a> Video: Tracking Usage
  129. > [AZURE.VIDEO tracking-usage-with-application-insights]
  130. ## <a name="next"></a> Next steps
  131. * [Track usage](app-insights-web-track-usage.md)
  132. * [Custom events and metrics](app-insights-api-custom-events-metrics.md)
  133. * [Build-measure-learn](app-insights-overview-usage.md)