Last active
August 29, 2015 14:22
-
-
Save oschaaf/c23770179328c713c079 to your computer and use it in GitHub Desktop.
Google Analytics C# MVC Local hosting snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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