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

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