forked from Teknikode/Teknik
12 changed files with 235 additions and 19 deletions
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Teknik.Configuration |
||||
{ |
||||
public class PiwikConfig |
||||
{ |
||||
public bool Enabled { get; set; } |
||||
|
||||
public string Url { get; set; } |
||||
|
||||
public int SiteId { get; set; } |
||||
|
||||
public PiwikConfig() |
||||
{ |
||||
Enabled = false; |
||||
Url = string.Empty; |
||||
SiteId = 1; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
using System.Web; |
||||
using System.Web.Mvc; |
||||
using System.Web.UI; |
||||
using Teknik.Configuration; |
||||
using Teknik.Helpers; |
||||
|
||||
namespace Teknik.Filters |
||||
{ |
||||
public class TrackingFilterAttribute : ActionFilterAttribute |
||||
{ |
||||
public override void OnActionExecuting(ActionExecutingContext filterContext) |
||||
{ |
||||
base.OnActionExecuting(filterContext); |
||||
} |
||||
|
||||
public override void OnActionExecuted(ActionExecutedContext filterContext) |
||||
{ |
||||
Config config = Config.Load(); |
||||
if (config.PiwikConfig.Enabled) |
||||
{ |
||||
try |
||||
{ |
||||
string sub = filterContext.HttpContext.Request.RequestContext.RouteData.Values["sub"].ToString(); |
||||
if (string.IsNullOrEmpty(sub)) |
||||
{ |
||||
sub = filterContext.HttpContext.Request.Url.AbsoluteUri.GetSubdomain(); |
||||
} |
||||
string title = config.Title; |
||||
Page page = filterContext.HttpContext.Handler as Page; |
||||
|
||||
if (page != null) |
||||
{ |
||||
title = page.Title; |
||||
} |
||||
Tracking.TrackPageView(filterContext.HttpContext.Request, title, sub); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
|
||||
} |
||||
} |
||||
|
||||
base.OnActionExecuted(filterContext); |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Net; |
||||
using System.Net.Http; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
using System.Web; |
||||
|
||||
namespace Teknik.Helpers |
||||
{ |
||||
public static class HttpRequestExtensions |
||||
{ |
||||
public static string GetClientIpAddress(this HttpRequestMessage request) |
||||
{ |
||||
if (request.Properties.ContainsKey("MS_HttpContext")) |
||||
{ |
||||
return IPAddress.Parse(((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress).ToString(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
using Piwik.Tracker; |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
using System.Web; |
||||
using Teknik.Configuration; |
||||
|
||||
namespace Teknik.Helpers |
||||
{ |
||||
public static class Tracking |
||||
{ |
||||
public static void TrackPageView(HttpRequestBase request, string title, string sub) |
||||
{ |
||||
Config config = Config.Load(); |
||||
// Handle Piwik Tracking if enabled
|
||||
if (config.PiwikConfig.Enabled) |
||||
{ |
||||
try |
||||
{ |
||||
PiwikTracker.URL = config.PiwikConfig.Url; |
||||
PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId); |
||||
|
||||
tracker.setForceVisitDateTime(DateTime.Now); |
||||
tracker.setUserAgent(request.UserAgent); |
||||
|
||||
tracker.setResolution(request.Browser.ScreenPixelsWidth, request.Browser.ScreenPixelsHeight); |
||||
tracker.setBrowserHasCookies(request.Browser.Cookies); |
||||
|
||||
string ipAddress = request.UserHostAddress; |
||||
|
||||
tracker.setIp(ipAddress); |
||||
|
||||
tracker.setUrl(request.Url.ToString()); |
||||
tracker.setUrlReferrer(request.UrlReferrer.ToString()); |
||||
|
||||
tracker.doTrackPageView(string.Format("{0} / {1}", sub, title)); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
|
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue