Created
October 9, 2012 18:10
-
-
Save bradoyler/3860421 to your computer and use it in GitHub Desktop.
An event tracking ActionFilter for your MVC app using Cloudmine
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
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] | |
public class UserTrackingAttribute : ActionFilterAttribute | |
{ | |
public static readonly string appId = "your CloudMine app Id"; | |
public static readonly string appSecret = "your CloudMine app Secret"; | |
class PageRequest | |
{ | |
public int PageId { get; set; } | |
public Double Created { get; set; } | |
public string Url { get; set; } | |
public string UserName { get; set; } | |
public string UserIP { get; set; } | |
public string Controller { get; set; } | |
public string Action { get; set; } | |
} | |
public override void OnResultExecuting(ResultExecutingContext context) | |
{ | |
PageRequest preq = new PageRequest | |
{ | |
Url = request.RawUrl, | |
UserIP = request.UserHostAddress, | |
UserName = context.HttpContext.User.Identity.Name, | |
Created = ExtensionMethods.JsonDate(DateTime.Now), | |
Action = (string)context.RequestContext.RouteData.Values["action"], | |
Controller = (string)context.RouteData.Values["controller"], | |
PageId = Convert.ToInt32(context.RouteData.Values["Id"]), | |
}; | |
using (var client = new WebClient()) | |
{ | |
client.Headers.Add("X-CloudMine-ApiKey", appSecret); | |
client.Headers.Add("Content-Type", "application/json"); | |
var serializer = new JavaScriptSerializer(); | |
var data = serializer.Serialize(preq); | |
var payload = "{\"PV_" + DateTime.Now.Ticks + "\":" + data + "}"; | |
var bytes = Encoding.Default.GetBytes(payload); | |
client.UploadDataAsync(new Uri("https://api.cloudmine.me/v1/app/" + appId + "/text"), "PUT", bytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment