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.3KB

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