The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrackPageView.cs 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Teknik.Configuration;
  8. using Teknik.Utilities;
  9. using Teknik.Tracking;
  10. using Microsoft.AspNetCore.Mvc.Filters;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Http.Extensions;
  13. using Microsoft.AspNetCore.Mvc;
  14. namespace Teknik.Filters
  15. {
  16. public class TrackPageViewAttribute : TypeFilterAttribute
  17. {
  18. public TrackPageViewAttribute() : base(typeof(TrackPageView))
  19. {
  20. }
  21. public class TrackPageView : ActionFilterAttribute
  22. {
  23. private readonly IBackgroundTaskQueue _queue;
  24. private readonly Config _config;
  25. public TrackPageView(IBackgroundTaskQueue queue, Config config)
  26. {
  27. _queue = queue;
  28. _config = config;
  29. }
  30. public override void OnActionExecuting(ActionExecutingContext filterContext)
  31. {
  32. }
  33. public override void OnActionExecuted(ActionExecutedContext filterContext)
  34. {
  35. HttpRequest request = filterContext.HttpContext.Request;
  36. string doNotTrack = request.Headers["DNT"];
  37. if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
  38. {
  39. string title = (filterContext.Controller as Controller)?.ViewBag?.Title;
  40. string sub = filterContext.RouteData.Values["sub"].ToString();
  41. if (string.IsNullOrEmpty(sub))
  42. {
  43. sub = request.Host.ToUriComponent().GetSubdomain();
  44. }
  45. string clientIp = request.ClientIPFromRequest(true);
  46. string url = UriHelper.GetEncodedUrl(request);
  47. string urlReferrer = request.Headers["Referer"].ToString();
  48. string userAgent = request.Headers["User-Agent"].ToString();
  49. int pixelWidth = 0;
  50. int pixelHeight = 0;
  51. bool hasCookies = false;
  52. string acceptLang = request.Headers["Accept-Language"];
  53. bool hasJava = false;
  54. // Fire and forget. Don't need to wait for it.
  55. _queue.QueueBackgroundWorkItem(async token =>
  56. {
  57. Tracking.Tracking.TrackPageView(filterContext.HttpContext, _config, title, sub, clientIp, url, urlReferrer, userAgent, pixelWidth, pixelHeight, hasCookies, acceptLang, hasJava);
  58. });
  59. }
  60. }
  61. }
  62. }
  63. }