通过ajax记录网站UV、PV数

前端之家收集整理的这篇文章主要介绍了通过ajax记录网站UV、PV数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、通过jquery记录网站UV、PV数据

  1. util.track = {
  2. log: function () {
  3. var referrer = util.browser.getReferrer(),host = window.location.host,pathname = window.location.pathname,url = window.location.href,title = document.title,type = 0,itemId = null;
  4.  
  5.  
  6. var detailRegex = /\/item\/(\d+)/;
  7. if (detailRegex.test(pathname)) {
  8. var result = detailRegex.exec(pathname);
  9. itemId = result[1];
  10. type = 1;
  11.  
  12. $(".js_spec a").click(function () {
  13. setTimeout(function () {
  14. var skuId = $("#js_skuId").val();
  15. if (skuId != itemId) {
  16. itemId = skuId;
  17.  
  18. r();
  19. }
  20. },100);
  21. });
  22. }
  23.  
  24. var r = function () {
  25. //alert("visit url:" + url + " title:" + title + " type:" + type);
  26. util.request.get("/ActionHandler.ashx",{
  27. referrer: url,url: url,title: title,type: type,itemId: itemId,visit: "visit"
  28. });
  29. };
  30.  
  31. r();
  32. }
  33. };
  34.  
  35. $(function () {
  36. //等待500毫秒后执行
  37. setTimeout(function () {
  38. util.track.log();
  39. },500);
  40. })


2、后台Handler.aspx处理页面

  1. <%@ WebHandler Language="C#" Class="ActionHandler" %>
  2.  
  3. using System;
  4. using System.Web;
  5.  
  6. public class ActionHandler : IHttpHandler,System.Web.SessionState.IRequiresSessionState
  7. {
  8.  
  9. public void ProcessRequest(HttpContext context)
  10. {
  11. context.Response.ContentType = "text/plain";
  12. context.Response.Write("Hello World");
  13. if (context.Request["visit"] != null)
  14. {
  15.  
  16. string url = context.Request["url"].ToString();
  17. string title = context.Request["title"].ToString();
  18. string referrer = context.Request["referrer"].ToString();
  19. string type = context.Request["type"].ToString();
  20. string itemId = context.Request["itemId"].ToString();
  21. CreateUserTracksLog(url,title,referrer,type.ToInt(0),itemId.ToInt(0));
  22. }
  23. }
  24.  
  25. private void CreateUserTracksLog(string url,string title,string referrer,int? type,int? itemId)
  26. {
  27. ECS.Model.A_UserTracksLog log = new ECS.Model.A_UserTracksLog();
  28. if (HttpContext.Current.Request.Cookies == null)
  29. {
  30. return;
  31. }
  32.  
  33. //if (context.Session[User_TRACK_LASTTIME] != null)
  34. //{
  35. // var trackTime = context.Session[User_TRACK_LASTTIME].ToString().ToDateTime();
  36. // if ((DateTime.Now - trackTime).Seconds < 30)
  37. // return;
  38. //}
  39.  
  40. //context.Session[User_TRACK_LASTTIME] = DateTime.Now;
  41.  
  42. log.VisitToken = this.VisitToken;
  43. log.UserId = Utils.GetSessionUserID();
  44. log.IsLogin = Utils.GetSessionUserID() > 0 ? true : false;
  45. log.PageUrl = referrer;
  46. log.IP = HttpContext.Current.Request.UserHostAddress.ToString();
  47. log.CreateTime = DateTime.Now;
  48.  
  49. new ECS.BLL.A_UserTracksLog().Add(log);
  50. }
  51.  
  52. //访问用户令牌
  53. private const string UserTrackVisittoken = "visitToken";
  54. //访问用户令牌
  55. public string VisitToken
  56. {
  57. get
  58. {
  59. if (HttpContext.Current.Request.Cookies[UserTrackVisittoken] == null)
  60. CreateTrackCookie();
  61.  
  62. return HttpContext.Current.Request.Cookies[UserTrackVisittoken].Value;
  63. }
  64. }
  65.  
  66. private static void CreateTrackCookie()
  67. {
  68. HttpCookie trackCookie = new HttpCookie(UserTrackVisittoken);
  69. trackCookie.Value = Guid.NewGuid().ToString();
  70. trackCookie.Expires = DateTime.Now.AddDays(1);
  71. HttpContext.Current.Response.AppendCookie(trackCookie);
  72. HttpContext.Current.Response.Cookies.Add(trackCookie);
  73. }
  74.  
  75.  
  76. public bool IsReusable
  77. {
  78. get
  79. {
  80. return false;
  81. }
  82. }
  83.  
  84. }

猜你在找的Ajax相关文章