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 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Microsoft.AspNetCore.Http;
  2. using Piwik.Tracker;
  3. using System;
  4. using System.Web;
  5. using Teknik.Configuration;
  6. using Teknik.Utilities;
  7. namespace Teknik.Tracking
  8. {
  9. public static class Tracking
  10. {
  11. public static void TrackPageView(HttpContext context, Config config, string title, string sub, string clientIp, string url, string urlReferrer, string userAgent, int pixelWidth, int pixelHeight, bool hasCookies, string acceptLang, bool hasJava)
  12. {
  13. try
  14. {
  15. if (config.PiwikConfig.Enabled)
  16. {
  17. if (config.DevEnvironment)
  18. {
  19. sub = "dev - " + sub;
  20. }
  21. PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId, config.PiwikConfig.Url, context);
  22. // Set Request Info
  23. tracker.SetIp(clientIp);
  24. tracker.SetTokenAuth(config.PiwikConfig.TokenAuth);
  25. tracker.SetUserAgent(userAgent);
  26. // Set browser info
  27. tracker.SetResolution(pixelWidth, pixelHeight);
  28. tracker.SetBrowserHasCookies(hasCookies);
  29. if (!string.IsNullOrEmpty(acceptLang))
  30. tracker.SetBrowserLanguage(acceptLang);
  31. tracker.SetPlugins(new BrowserPlugins { Java = hasJava });
  32. // Get Referral
  33. if (!string.IsNullOrEmpty(urlReferrer))
  34. tracker.SetUrlReferrer(urlReferrer);
  35. if (!string.IsNullOrEmpty(url))
  36. tracker.SetUrl(url);
  37. // Send the tracking request
  38. tracker.DoTrackPageView(string.Format("{0}/{1}", sub, title));
  39. }
  40. }
  41. catch (Exception)
  42. {
  43. }
  44. }
  45. public static void TrackDownload(HttpContext context, Config config, string userAgent, string clientIp, string url, string urlReferrer)
  46. {
  47. TrackAction(context, config, ActionType.Download, userAgent, clientIp, url, urlReferrer);
  48. }
  49. public static void TrackLink(HttpContext context, Config config, string userAgent, string clientIp, string url, string urlReferrer)
  50. {
  51. TrackAction(context, config, ActionType.Link, userAgent, clientIp, url, urlReferrer);
  52. }
  53. private static void TrackAction(HttpContext context, Config config, ActionType type, string userAgent, string clientIp, string url, string urlReferrer)
  54. {
  55. try
  56. {
  57. if (config.PiwikConfig.Enabled)
  58. {
  59. PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId, config.PiwikConfig.Url, context);
  60. tracker.SetUserAgent(userAgent);
  61. tracker.SetIp(clientIp);
  62. tracker.SetTokenAuth(config.PiwikConfig.TokenAuth);
  63. // Get Referral
  64. if (!string.IsNullOrEmpty(urlReferrer))
  65. tracker.SetUrlReferrer(urlReferrer);
  66. if (!string.IsNullOrEmpty(url))
  67. tracker.SetUrl(url);
  68. tracker.DoTrackAction(url, type);
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. }
  74. }
  75. }
  76. }