title: Managing Application Insights Cookies
body: <p>I'm wondering how application insights work with cookies because I'll like to understand user and session tracking, so I've been researching and...</p>
  
  <p>Here is a brief introduction about the theory:</p>
  
  <ol>
  <li><blockquote>
    <p>Whenever Application Insights SDK get a request that doesn’t have application insights user tracking cookie (set by Application Insights JS snippet) it will set this cookie and start a new session.
    (from <a href="http://apmtips.com/blog/2015/01/02/application-insights-requests-tracking-more-than-just-a-begin-and-end/" rel="noreferrer">apmtips</a> )</p>
  </blockquote></li>
  </ol>
  
  <p>2.</p>
  
  <blockquote>
    <p><strong>UserTelemetryInitializer</strong> updates the <strong>Id</strong> and <strong>AcquisitionDate</strong> properties of <strong>User</strong> context for all telemetry items with values extracted from the <strong>ai_user</strong> cookie generated by the Application Insights JavaScript instrumentation code running in the user's browser.</p>
    
    <p><strong>SessionTelemetryInitializer</strong> updates the <strong>Id</strong> property of the <strong>Session</strong> context for all telemetry items with value extracted from the <strong>ai_session</strong> cookie generated by the ApplicationInsights JavaScript instrumentation code running in the user's browser.
    (from <a href="https://azure.microsoft.com/en-gb/documentation/articles/app-insights-configuration-with-applicationinsights-config/" rel="noreferrer">azure documentation (Configuring the Application Insights SKD with ApplicationInsights.config)</a>)</p>
  </blockquote>
  
  <p>So there are two cookies: <strong>ai_session</strong>, and <strong>ai_user</strong>.</p>
  
  <p>And here comes my questions:</p>
  
  <ol>
  <li>When are they initialized?</li>
  <li>What is doing it?</li>
  <li>How can I stop using them?</li>
  <li>If I wanted to keep them, how could I change their expiration time?</li>
  </ol>
  
  <p>Trying to remove them I made a project using ASP.NET Web Applications using the default template for Web Api, which includes MVC and Web Api.</p>
  
  <p>Doing a research I found <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/7fb8afbd-091a-45c5-9d9a-c5ea512ad369/disable-user-and-session-tracking?forum=ApplicationInsights" rel="noreferrer">this approach</a> to disable them but I don't have any WebSessionTrackingTelemetryModule. So I commented out "UserTelemetryInitializer" and "SessionTelemetryInitializer" and this is what I have:</p>
  
  <pre><code>&lt;TelemetryInitializers&gt;
  
    &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.SyntheticTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
    &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.ClientIpHeaderTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
    &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.UserAgentTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
    &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.OperationNameTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
    &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.OperationIdTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
  
  &lt;!--&lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.UserTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;--&gt;
  &lt;!--&lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.SessionTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;--&gt;
  
   &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.AzureRoleEnvironmentTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
   &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.DomainNameRoleInstanceTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
   &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.BuildInfoConfigComponentVersionTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
   &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.DeviceTelemetryInitializer, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
  
  
  &lt;/TelemetryInitializers&gt;
  </code></pre>
  
  <p>And :</p>
  
  <pre><code>&lt;TelemetryModules&gt;
  
  &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.ApplicationInsights.Extensibility.DependencyCollector" /&gt;
  &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.PerformanceCollectorModule, Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector"/&gt;
  &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.DiagnosticsTelemetryModule, Microsoft.ApplicationInsights" /&gt;
  &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTrackingTelemetryModule, Microsoft.ApplicationInsights.Extensibility.Web"/&gt;
  &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.ExceptionTrackingTelemetryModule, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
  &lt;Add Type="Microsoft.ApplicationInsights.Extensibility.Web.DeveloperModeWithDebuggerAttachedTelemetryModule, Microsoft.ApplicationInsights.Extensibility.Web" /&gt;
  
  &lt;/TelemetryModules&gt;
  </code></pre>
  
  <p>But it doesn't make a difference. Either I leave the modules commented or not, the cookies are still being generated.</p>
  
  <p>Trying to remove the cookies, I commented the steps done in Startup and exclude from my project all the .js files, but the cookies keep appearing after every request.</p>
  
  <p>So at this point I don't understand where the "Application Insights Javascript" takes place and I guess that what I'm missing is something in the backend. Am I wrong?</p>
  
  <p>Finally, my commented Startup.cs looks like:</p>
  
  <pre><code>[assembly: OwinStartupAttribute(typeof(Try001.Startup))]
  namespace Try001
  {
      public partial class Startup
      {
           public void Configuration(IAppBuilder app)
           {   
              //ConfigureAuth(app);
           }
      }
    }
  </code></pre>
  
  <p>And my Global.asax.cs looks like:</p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
  {
      protected void Application_Start()
      {
          //AreaRegistration.RegisterAllAreas();
          //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          //BundleConfig.RegisterBundles(BundleTable.Bundles);
      }
  }
  </code></pre>
  
  <p>Where RegisterRoutes is just doing the default routing. So I aimed to do just the very basic stuff to get it working, but I don't have a clue about where to keep digging. </p>
  
  <p>Could someone enlighten me?</p>
  
  <p>Thanks for reading so far.</p>