Advertisement
Guest User

Untitled

a guest
May 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Web;
  8. using FreebooksES.Utilities.Configuration;
  9. using FreebooksES.Utilities.Session;
  10.  
  11. namespace FreebooksES.Utilities
  12. {
  13. public class GAevent
  14. {
  15. public static void TrackEvent(string category, string action, string label, int? value = null)
  16. {
  17. Track(HitType.@event, category, action, label, value);
  18. }
  19.  
  20. private static void Track(HitType type, string category, string action, string label,
  21. int? value = null)
  22. {
  23. if (string.IsNullOrEmpty(category)) throw new ArgumentNullException("category");
  24. if (string.IsNullOrEmpty(action)) throw new ArgumentNullException("action");
  25.  
  26. var request = (HttpWebRequest)WebRequest.Create("http://www.google-analytics.com/collect");
  27. request.Method = "POST";
  28.  
  29. // the request body we want to send
  30. var guid = Guid.NewGuid().ToString();
  31. var postData = new Dictionary<string, string>
  32. {
  33. { "v", "1" },
  34. {
  35. "tid", ConfigContext.GoogleAnalytics
  36. },
  37. { "cid", guid },
  38. { "t", type.ToString() },
  39. { "ec", category },
  40. { "ea", action },
  41. { "cn", SessionData.Current.GoogleAnalytics.Campaign },
  42. { "cs", SessionData.Current.GoogleAnalytics.Source },
  43. { "cm", SessionData.Current.GoogleAnalytics.Medium },
  44. };
  45. if (!string.IsNullOrEmpty(label))
  46. {
  47. postData.Add("el", label);
  48. }
  49. if (value.HasValue)
  50. {
  51. postData.Add("ev", value.ToString());
  52. }
  53.  
  54. var postDataString = postData
  55. .Aggregate("", (data, next) => string.Format("{0}&{1}={2}", data, next.Key,
  56. HttpUtility.UrlEncode(next.Value)))
  57. .TrimEnd('&');
  58.  
  59. request.UserAgent = HttpContext.Current.Request.UserAgent;
  60. // set the Content-Length header to the correct value
  61. request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);
  62.  
  63. // write the request body to the request
  64. using (var writer = new StreamWriter(request.GetRequestStream()))
  65. {
  66. writer.Write(postDataString);
  67. }
  68.  
  69. try
  70. {
  71. var webResponse = (HttpWebResponse)request.GetResponse();
  72. if (webResponse.StatusCode != HttpStatusCode.OK)
  73. {
  74. throw new HttpException((int)webResponse.StatusCode,
  75. "Google Analytics tracking did not return OK 200");
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. // do what you like here, we log to Elmah
  81. // ElmahLog.LogError(ex, "Google Analytics tracking failed");
  82. }
  83. }
  84.  
  85. private enum HitType
  86. {
  87. // ReSharper disable InconsistentNaming
  88. @event,
  89. // ReSharper restore InconsistentNaming
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement