Created
May 4, 2012 18:42
-
-
Save sitereactor/2596865 to your computer and use it in GitHub Desktop.
Google Analytics data cacher used with Umbraco to cache stats to local json file
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
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using System.Web; | |
using Google.GData.Analytics; | |
using Google.GData.Client; | |
using ServiceStack.Text; | |
namespace Website.WebApplication | |
{ | |
/// <summary> | |
/// Summary description for GoogleAnalyticsCacher | |
/// </summary> | |
public class GoogleAnalyticsCacher : IHttpHandler | |
{ | |
private const string DataFeedUri = "https://www.google.com/analytics/feeds/data"; | |
public void ProcessRequest(HttpContext context) | |
{ | |
string username = ConfigurationManager.AppSettings["GoogleAnalytics_Username"]; | |
string password = ConfigurationManager.AppSettings["GoogleAnalytics_Password"]; | |
string profileId = ConfigurationManager.AppSettings["GoogleAnalytics_ProfileId"]; | |
string cachepath = ConfigurationManager.AppSettings["GoogleAnalytics_CacheLocation"]; | |
string path = context.Server.MapPath(cachepath); | |
DataQuery dataQuery = new DataQuery(DataFeedUri) | |
{ | |
Ids = "ga:" + profileId, | |
Metrics = "ga:pageviews", | |
Dimensions = "ga:pagePath", | |
Sort = "-ga:pageviews", | |
GAStartDate = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd"), | |
GAEndDate = DateTime.Now.ToString("yyyy-MM-dd") | |
}; | |
AnalyticsService service = new AnalyticsService("Top5CacherAnalytics_v1.0") | |
{Credentials = new GDataCredentials(username, password)}; | |
DataFeed dataFeed = service.Query(dataQuery); | |
//Limit entries to URLs containing the word/string "projects" and take 6 | |
List<string> pages = (from DataEntry entry in dataFeed.Entries | |
where entry.Dimensions[0].Value.Contains("projects") | |
select entry.Dimensions[0].Value).Take(6).ToList(); | |
List<MostViewedProjectsModel> list = | |
pages.Select( | |
page => | |
new MostViewedProjectsModel | |
{ | |
ProjectPageUrl = page, | |
TrimmedProjectTitle = page.Substring(page.LastIndexOf('/')+1).Replace("-", " ").ToTitleCase() | |
}). | |
ToList(); | |
string json = list.ToJson(); | |
if(File.Exists(path)) | |
{ | |
File.WriteAllText(path, json); | |
context.Response.Write("Cache Updated"); | |
} | |
else | |
{ | |
context.Response.Write("Cache File does not exist and can therefor not be updated"); | |
} | |
} | |
public bool IsReusable | |
{ | |
get | |
{ | |
return false; | |
} | |
} | |
} | |
public class MostViewedProjectsModel | |
{ | |
public string ProjectPageUrl { get; set; } | |
public string TrimmedProjectTitle { get; set; } | |
} | |
/// <summary> | |
/// https://github.com/robconery/sugar | |
/// </summary> | |
public static class Inflector | |
{ | |
/// <summary> | |
/// Converts the string to title case. | |
/// </summary> | |
/// <param name="word">The word.</param> | |
/// <returns></returns> | |
public static string ToTitleCase(this string word) | |
{ | |
return Regex.Replace(Humanize(AddUnderscores(word)), @"\b([a-z])", | |
match => match.Captures[0].Value.ToUpper()); | |
} | |
/// <summary> | |
/// Converts the string to human case. | |
/// </summary> | |
/// <param name="lowercaseAndUnderscoredWord">The lowercase and underscored word.</param> | |
/// <returns></returns> | |
public static string Humanize(this string lowercaseAndUnderscoredWord) | |
{ | |
return MakeInitialCaps(Regex.Replace(lowercaseAndUnderscoredWord, @"_", " ")); | |
} | |
/// <summary> | |
/// Adds the underscores. | |
/// </summary> | |
/// <param name="pascalCasedWord">The pascal cased word.</param> | |
/// <returns></returns> | |
public static string AddUnderscores(this string pascalCasedWord) | |
{ | |
return | |
Regex.Replace( | |
Regex.Replace(Regex.Replace(pascalCasedWord, @"([A-Z]+)([A-Z][a-z])", "$1_$2"), @"([a-z\d])([A-Z])", | |
"$1_$2"), @"[-\s]", "_").ToLower(); | |
} | |
/// <summary> | |
/// Makes the initial caps. | |
/// </summary> | |
/// <param name="word">The word.</param> | |
/// <returns></returns> | |
public static string MakeInitialCaps(this string word) | |
{ | |
return String.Concat(word.Substring(0, 1).ToUpper(), word.Substring(1).ToLower()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment