{"id":9756,"date":"2013-12-18T15:44:13","date_gmt":"2013-12-18T15:44:13","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2013\/12\/18\/implementing-custom-events-in-application-insights\/"},"modified":"2019-02-14T17:54:04","modified_gmt":"2019-02-15T01:54:04","slug":"implementing-custom-events-in-application-insights-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/implementing-custom-events-in-application-insights-2\/","title":{"rendered":"Implementing custom events in Application Insights"},"content":{"rendered":"<form id=\"form1\">\n<div>Application Insights Usage reports can supply an incredible wealth of information by simply adding a single line of JavaScript. Such as how many people visit a page, what browsers are they using, what operating systems they are using, where are they visiting from etc etc etc \u2013but this doesn\u2019t tell the entire story.&#160; For instance, it doesn\u2019t tell the development team application or interaction specific things like how much a customer LIKES the page.&#160;&#160; -Luckily this is easy to add. <\/div>\n<\/p><\/form>\n<p>Starting with a simple Asp.net form: <\/p>\n<blockquote>\n<p>&lt;%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; CodeBehind=&quot;WebForm2.aspx.vb&quot; Inherits=&quot;WebApplication1.WebForm2&quot; %&gt;     <br \/>&lt;!DOCTYPE html&gt;      <br \/>&lt;html xmlns=&quot;<a href=\"http:\/\/www.w3.org\/1999\/xhtml&quot;\">http:\/\/www.w3.org\/1999\/xhtml&quot;<\/a>&gt;      <br \/>&lt;head runat=&quot;server&quot;&gt;      <br \/>&#160;&#160;&#160; &lt;title&gt;&lt;\/title&gt;      <br \/>&lt;\/head&gt;      <br \/>&lt;body&gt;      <br \/>&#160;&#160;&#160; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;      <br \/>&#160;&#160;&#160; &lt;div&gt;      <br \/>&lt;input type=&quot;button&quot; value=&quot;Happy&quot; name=&quot;button1&quot;\/&gt;      <br \/>&lt;input type=&quot;button&quot; value=&quot;Unhappy&quot; name=&quot;button2&quot;\/&gt;      <br \/>&#160;&#160;&#160; &lt;\/div&gt;      <br \/>&#160;&#160;&#160; &lt;\/form&gt;      <br \/>&lt;\/body&gt;      <br \/>&lt;\/html&gt;<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/45\/92\/metablogapi\/7127.image_79C47F34.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;border-left: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/12\/4478.image_thumb_197358FD.png\" width=\"244\" height=\"178\" \/><\/a> <\/p>\n<p>&#160;<\/p>\n<p>1. We would start by adding the JavaScript to start the default instrumentation for usage (in purple below)<\/p>\n<p>&#160;<\/p>\n<blockquote>\n<p>&lt;%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; CodeBehind=&quot;WebForm2.aspx.vb&quot; Inherits=&quot;WebApplication1.WebForm2&quot; %&gt;     <br \/>&lt;!DOCTYPE html&gt;      <br \/>&lt;html xmlns=&quot;<a href=\"http:\/\/www.w3.org\/1999\/xhtml&quot;\">http:\/\/www.w3.org\/1999\/xhtml&quot;<\/a>&gt;      <br \/>&lt;head runat=&quot;server&quot;&gt;      <br \/>&#160;&#160;&#160; &lt;title&gt;&lt;\/title&gt;<\/p>\n<p><font color=\"#800080\"><em>&lt;script type=&quot;text\/javascript&quot;&gt;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; var _daAnalytics = _daAnalytics || {}; _daAnalytics.init = function (d) { _daAnalytics.Commands = _daAnalytics.Commands || []; for (var a = function (a) { return function () { _daAnalytics.Commands.push([a].concat(Array.prototype.slice.call(arguments, 0))) } }, b = &quot;siteId trackLinkClicks setUserId setProperty setView trackPage trackAction trackEvent trackView setAppId setAccountId setUserId&quot;.split(&quot; &quot;), c = 0; c &lt; b.length; c++) _daAnalytics[b[c]] = a(b[c]); _daAnalytics.setAppId(d); var a = document.createElement(&quot;script&quot;); a.type = &quot;text\/javascript&quot;; a.src = &quot;\/\/az416426.vo.msecnd.net\/scripts\/da.js&quot;; a.async = !0; var b = document.getElementsByTagName(&quot;script&quot;)[0]; b.parentNode.insertBefore(a, b); }; <\/em><\/font><\/p>\n<p><font color=\"#800080\"><em>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _daAnalytics.init(&quot;<strong>aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee<\/strong>&quot;);          <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _daAnalytics.trackPage();          <br \/>&#160;&#160;&#160; &lt;\/script&gt;<\/em><\/font><\/p>\n<p>&lt;\/head&gt;     <br \/>&lt;body&gt;      <br \/>&#160;&#160;&#160; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;      <br \/>&#160;&#160;&#160; &lt;div&gt;      <br \/>&lt;input type=&quot;button&quot; value=&quot;Happy&quot; name=&quot;button1&quot;\/&gt;      <br \/>&lt;input type=&quot;button&quot; value=&quot;Unhappy&quot; name=&quot;button2&quot;\/&gt;      <br \/>&#160;&#160;&#160; &lt;\/div&gt;      <br \/>&#160;&#160;&#160; &lt;\/form&gt;      <br \/>&lt;\/body&gt;      <br \/>&lt;\/html&gt;<\/p>\n<\/blockquote>\n<p>This JavaScript is created for you, and can be found in the Application Insights by clicking on the Control panel Icon: <\/p>\n<p><a href=\"https:\/\/YOURACCOUNT.visualstudio.com\/_appanalytics\/_admin\/keysanddownloads#application=YOURAPPLICATION\">https:\/\/YOURACCOUNT.visualstudio.com\/_appanalytics\/_admin\/keysanddownloads#application=YOURAPPLICATION<\/a> <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/45\/92\/metablogapi\/7633.image_6E2EC1F5.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;border-left: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/12\/8032.image_thumb_2DF8A87B.png\" width=\"621\" height=\"238\" \/><\/a> <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/45\/92\/metablogapi\/1273.image_6DC28F00.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;border-left: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/12\/7043.image_thumb_2262EB3C.png\" width=\"625\" height=\"406\" \/><\/a> <\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/45\/92\/metablogapi\/6087.image_622CD1C1.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;border-left: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/12\/7571.image_thumb_01DBAB8A.png\" width=\"630\" height=\"409\" \/><\/a> <\/p>\n<p>&#160;<\/p>\n<p>2. The next step is to add the custom event to the HTML source. This is done by calling __da.trackEvent() in green below. <\/p>\n<blockquote>\n<p>&lt;%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; CodeBehind=&quot;WebForm2.aspx.vb&quot; Inherits=&quot;WebApplication1.WebForm2&quot; %&gt;     <br \/>&lt;!DOCTYPE html&gt;      <br \/>&lt;html xmlns=&quot;<a href=\"http:\/\/www.w3.org\/1999\/xhtml&quot;\">http:\/\/www.w3.org\/1999\/xhtml&quot;<\/a>&gt;      <br \/>&lt;head runat=&quot;server&quot;&gt;      <br \/>&#160;&#160;&#160; &lt;title&gt;&lt;\/title&gt;<\/p>\n<p><em>&lt;script type=&quot;text\/javascript&quot;&gt;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; var _daAnalytics = _daAnalytics || {}; _daAnalytics.init = function (d) { _daAnalytics.Commands = _daAnalytics.Commands || []; for (var a = function (a) { return function () { _daAnalytics.Commands.push([a].concat(Array.prototype.slice.call(arguments, 0))) } }, b = &quot;siteId trackLinkClicks setUserId setProperty setView trackPage trackAction trackEvent trackView setAppId setAccountId setUserId&quot;.split(&quot; &quot;), c = 0; c &lt; b.length; c++) _daAnalytics[b[c]] = a(b[c]); _daAnalytics.setAppId(d); var a = document.createElement(&quot;script&quot;); a.type = &quot;text\/javascript&quot;; a.src = &quot;\/\/az416426.vo.msecnd.net\/scripts\/da.js&quot;; a.async = !0; var b = document.getElementsByTagName(&quot;script&quot;)[0]; b.parentNode.insertBefore(a, b); }; <\/em><\/p>\n<p><em>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _daAnalytics.init(&quot;aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee&quot;);       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _daAnalytics.trackPage();        <br \/>&#160;&#160;&#160; &lt;\/script&gt;<\/em><\/p>\n<p>&lt;\/head&gt;     <br \/>&lt;body&gt;      <br \/>&#160;&#160;&#160; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;      <br \/><font color=\"#008040\">&#160;&#160; &lt;script&gt;<\/font> <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color=\"#008000\">function trackHappy() {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; window.__da.trackEvent(&quot;rating&quot;, window.location.hostname + window.location.pathname, { &quot;Rating&quot;: &quot;<strong>Happy<\/strong>&quot; }, { &quot;RatingValue&quot;: <strong>100 <\/strong>});        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; alert(&quot;Happy! &quot; + window.location.hostname + window.location.pathname);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } <\/font><\/p>\n<p><font color=\"#008000\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; function trackUnhappy() {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; window.__da.trackEvent(&quot;rating&quot;, window.location.hostname + window.location.pathname, { &quot;Rating&quot;: &quot;<strong>Unhappy<\/strong>&quot; }, { &quot;RatingValue&quot;: <strong>0 <\/strong>});        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; alert(&quot;Unhappy! &quot; + window.location.hostname + window.location.pathname);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/script&gt;<\/font><\/p>\n<p>&lt;input type=&quot;button&quot; value=&quot;Happy&quot; name=&quot;button1&quot; <font color=\"#008040\">onclick=&quot;trackHappy()<\/font>&quot;\/&gt;      <br \/>&lt;input type=&quot;button&quot; value=&quot;Unhappy&quot; name=&quot;button2&quot; <font color=\"#008040\">onclick=&quot;trackUnhappy()<\/font>&quot;\/&gt;<\/p>\n<p>&#160;&#160;&#160; &lt;\/div&gt;     <br \/>&#160;&#160;&#160; &lt;\/form&gt;      <br \/>&lt;\/body&gt;      <br \/>&lt;\/html&gt;<\/p>\n<\/blockquote>\n<p>4. Now to find these events in Application Insights: go to the <strong>Usage<\/strong> menu select <strong>Events Insights<\/strong> and find your custom event\u2026AFTER you select your custom event you will see the number of events that occurred and their distribution\u2026For instance you can see the Happy button was selected 20 times and the Unhappy button selected 15 times.&#160; <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/45\/92\/metablogapi\/0045.image_4B6D8A38.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;border-left: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/12\/3276.image_thumb_24334103.png\" width=\"504\" height=\"392\" \/><\/a> <\/p>\n<\/p>\n<p>4. If you wanted to see the values of the custom event you will want to use the \u201cEvents\u201d report. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/45\/92\/metablogapi\/6431.image_66A5E339.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;border-left: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/12\/5857.image_thumb_3F6B9A04.png\" width=\"504\" height=\"400\" \/><\/a> <\/p>\n<p>After selecting your custom event choose the details tab and you will see heuristics about your custom event like the min, max and average values <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/00\/00\/00\/45\/92\/metablogapi\/1651.image_7F358089.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;border-left: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/12\/4774.image_thumb_01DE3C3B.png\" width=\"506\" height=\"450\" \/><\/a> <\/p>\n<\/p>\n<p>(Much of this information is also available at at: <a title=\"http:\/\/msdn.microsoft.com\/library\/dn481098.aspx\" href=\"http:\/\/msdn.microsoft.com\/library\/dn481098.aspx\">http:\/\/msdn.microsoft.com\/library\/dn481098.aspx<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Application Insights Usage reports can supply an incredible wealth of information by simply adding a single line of JavaScript. Such as how many people visit a page, what browsers are they using, what operating systems they are using, where are they visiting from etc etc etc \u2013but this doesn\u2019t tell the entire story.&#160; For instance, [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[224,1],"tags":[],"class_list":["post-9756","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-devops"],"acf":[],"blog_post_summary":"<p>Application Insights Usage reports can supply an incredible wealth of information by simply adding a single line of JavaScript. Such as how many people visit a page, what browsers are they using, what operating systems they are using, where are they visiting from etc etc etc \u2013but this doesn\u2019t tell the entire story.&#160; For instance, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/9756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=9756"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/9756\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=9756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=9756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=9756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}