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.

Tracking.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Piwik.Tracker;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using Teknik.Configuration;
  9. namespace Teknik.Helpers
  10. {
  11. public static class Tracking
  12. {
  13. public static void TrackPageView(HttpRequestBase request, Config config, string title)
  14. {
  15. // Handle Piwik Tracking if enabled
  16. if (config.PiwikConfig.Enabled)
  17. {
  18. try
  19. {
  20. // Follow Do Not Track
  21. string doNotTrack = request.Headers["DNT"];
  22. if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
  23. {
  24. string sub = request.RequestContext.RouteData.Values["sub"].ToString();
  25. if (string.IsNullOrEmpty(sub))
  26. {
  27. sub = request.Url.AbsoluteUri.GetSubdomain();
  28. }
  29. if (config.DevEnvironment)
  30. {
  31. sub = "dev - " + sub;
  32. }
  33. PiwikTracker.URL = config.PiwikConfig.Url;
  34. PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId);
  35. // Get Request Info
  36. string ipAddress = request.ClientIPFromRequest(true);
  37. tracker.setIp(ipAddress);
  38. tracker.setTokenAuth(config.PiwikConfig.TokenAuth);
  39. tracker.setUrl(request.Url.ToString());
  40. tracker.setUserAgent(request.UserAgent);
  41. // Get browser info
  42. tracker.setResolution(request.Browser.ScreenPixelsWidth, request.Browser.ScreenPixelsHeight);
  43. tracker.setBrowserHasCookies(request.Browser.Cookies);
  44. if (!string.IsNullOrEmpty(request.Headers["Accept-Language"]))
  45. tracker.setBrowserLanguage(request.Headers["Accept-Language"]);
  46. BrowserPlugins plugins = new BrowserPlugins();
  47. plugins.java = request.Browser.JavaApplets;
  48. tracker.setPlugins(plugins);
  49. // Get Referral
  50. if (request.UrlReferrer != null)
  51. tracker.setUrlReferrer(request.UrlReferrer.ToString());
  52. // Send the tracking request
  53. tracker.doTrackPageView(string.Format("{0}/{1}", sub, title));
  54. }
  55. }
  56. catch (Exception ex)
  57. {
  58. }
  59. }
  60. }
  61. public static void TrackDownload(HttpRequestBase request, Config config, string url)
  62. {
  63. TrackAction(request, config, url, PiwikTracker.ActionType.download);
  64. }
  65. public static void TrackLink(HttpRequestBase request, Config config, string url)
  66. {
  67. TrackAction(request, config, url, PiwikTracker.ActionType.link);
  68. }
  69. private static void TrackAction(HttpRequestBase request, Config config, string url, PiwikTracker.ActionType type)
  70. {
  71. // Handle Piwik Tracking if enabled
  72. if (config.PiwikConfig.Enabled)
  73. {
  74. try
  75. {
  76. // Follow Do Not Track
  77. string doNotTrack = request.Headers["DNT"];
  78. if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
  79. {
  80. PiwikTracker.URL = config.PiwikConfig.Url;
  81. PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId);
  82. tracker.setUserAgent(request.UserAgent);
  83. string ipAddress = request.ClientIPFromRequest(true);
  84. tracker.setIp(ipAddress);
  85. tracker.setTokenAuth(config.PiwikConfig.TokenAuth);
  86. tracker.doTrackAction(url, type);
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. }
  92. }
  93. }
  94. }
  95. }