Skip to content

Instantly share code, notes, and snippets.

@oschaaf
Last active August 29, 2015 14:22
Show Gist options
  • Save oschaaf/c23770179328c713c079 to your computer and use it in GitHub Desktop.
Save oschaaf/c23770179328c713c079 to your computer and use it in GitHub Desktop.
Google Analytics C# MVC Local hosting snippet
public class MvcApplication : System.Web.HttpApplication
{
private static object ga_lock_ = new {};
private static bool ga_refreshed_ = false;
private void Application_BeginRequest(object sender, EventArgs e)
{
if (!ga_refreshed_) {
lock(ga_lock_) {
Response.CacheControl = "public";
Response.Expires = minutes;
if (!ga_refreshed_ && application.Context.Request.FilePath.ToLower() == "/scripts/ga.js") {
int minutes = 60 * 24 * 15;
using (WebClient client = new WebClient())
{
client.DownloadFile("http://www.google-analytics.com/analytics.js", Server.MapPath("/Scripts/ga.js"));
}
ga_refreshed_ = true;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment