The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TrackPageView.cs 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Web.UI;
  9. using Teknik.Configuration;
  10. using Teknik.Utilities;
  11. using Teknik.Piwik;
  12. namespace Teknik.Filters
  13. {
  14. public class TrackPageView : ActionFilterAttribute
  15. {
  16. public override void OnActionExecuting(ActionExecutingContext filterContext)
  17. {
  18. base.OnActionExecuting(filterContext);
  19. }
  20. public override void OnActionExecuted(ActionExecutedContext filterContext)
  21. {
  22. HttpRequestBase request = filterContext.HttpContext.Request;
  23. string doNotTrack = request.Headers["DNT"];
  24. if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
  25. {
  26. string title = filterContext.Controller?.ViewBag?.Title;
  27. string sub = request.RequestContext.RouteData.Values["sub"].ToString();
  28. if (string.IsNullOrEmpty(sub))
  29. {
  30. sub = request.Url?.AbsoluteUri.GetSubdomain();
  31. }
  32. string ipAddress = request.ClientIPFromRequest(true);
  33. string url = string.Empty;
  34. if (request.Url != null)
  35. url = request.Url.ToString();
  36. string urlReferrer = request.UrlReferrer?.ToString();
  37. string userAgent = request.UserAgent;
  38. int pixelWidth = request.Browser.ScreenPixelsWidth;
  39. int pixelHeight = request.Browser.ScreenPixelsHeight;
  40. bool hasCookies = request.Browser.Cookies;
  41. string acceptLang = request.Headers["Accept-Language"];
  42. bool hasJava = request.Browser.JavaApplets;
  43. // Fire and forget. Don't need to wait for it.
  44. Task.Run(() => AsyncTrackPageView(title, sub, ipAddress, url, urlReferrer, userAgent,pixelWidth, pixelHeight, hasCookies, acceptLang, hasJava));
  45. }
  46. base.OnActionExecuted(filterContext);
  47. }
  48. private static void AsyncTrackPageView(string title, string sub, string clientIp, string url, string urlReferrer, string userAgent, int pixelWidth, int pixelHeight, bool hasCookies, string acceptLang, bool hasJava)
  49. {
  50. Tracking.TrackPageView(title, sub, clientIp, url, urlReferrer, userAgent, pixelWidth, pixelHeight, hasCookies, acceptLang, hasJava);
  51. }
  52. }
  53. }