How to get HttpContextBase from HttpContext.Current in the Global.asax?
In order to implement server-side google analytics tracking into our API for request monitoring we decided to give this library a try:
https://github.com/maartenba/GoogleAnalyticsTracker
In order to get this example below to work inside the Global.asax file
Tracker tracker = new Tracker("UA-XXXXXX-XX", "www.example.org");
tracker.TrackPageView(HttpContext, "My API - Create");
I needed to somehow cast HttpContext.Current into the abstract class HttpContextBase.
Turns out it is as simple as:
new HttpContextWrapper(HttpContext.Current)
so the example becomes:
Tracker tracker = new Tracker("UA-XXXXXX-XX", "www.example.org");
tracker.TrackPageView(new HttpContextWrapper(HttpContext.Current), "My API - Create");
Written by Mike Olsen
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#.net
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#