1、通过jquery记录网站UV、PV数据
util.track = {
log: function () {
var referrer = util.browser.getReferrer(),
host = window.location.host,
pathname = window.location.pathname,
url = window.location.href,
title = document.title,
type = 0,
itemId = null;
var detailRegex = /\/item\/(\d+)/;
if (detailRegex.test(pathname)) {
var result = detailRegex.exec(pathname);
itemId = result[1];
type = 1;
$(".js_spec a").click(function () {
setTimeout(function () {
var skuId = $("#js_skuId").val();
if (skuId != itemId) {
itemId = skuId;
r();
}
}, 100);
});
}
var r = function () {
//alert("visit url:" + url + " title:" + title + " type:" + type);
util.request.get("/ActionHandler.ashx", {
referrer: url,
url: url,
title: title,
type: type,
itemId: itemId,
visit: "visit"
});
};
r();
}
};
$(function () {
//等待500毫秒后执行
setTimeout(function () {
util.track.log();
}, 500);
})
2、后台Handler.aspx处理页面
<%@ WebHandler Language="C#" Class="ActionHandler" %>
using System;
using System.Web;
public class ActionHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
if (context.Request["visit"] != null)
{
string url = context.Request["url"].ToString();
string title = context.Request["title"].ToString();
string referrer = context.Request["referrer"].ToString();
string type = context.Request["type"].ToString();
string itemId = context.Request["itemId"].ToString();
CreateUserTracksLog(url, title, referrer, type.ToInt(0), itemId.ToInt(0));
}
}
private void CreateUserTracksLog(string url, string title, string referrer, int? type, int? itemId)
{
ECS.Model.A_UserTracksLog log = new ECS.Model.A_UserTracksLog();
if (HttpContext.Current.Request.Cookies == null)
{
return;
}
//if (context.Session[User_TRACK_LASTTIME] != null)
//{
// var trackTime = context.Session[User_TRACK_LASTTIME].ToString().ToDateTime();
// if ((DateTime.Now - trackTime).Seconds < 30)
// return;
//}
//context.Session[User_TRACK_LASTTIME] = DateTime.Now;
log.VisitToken = this.VisitToken;
log.UserId = Utils.GetSessionUserID();
log.IsLogin = Utils.GetSessionUserID() > 0 ? true : false;
log.PageUrl = referrer;
log.IP = HttpContext.Current.Request.UserHostAddress.ToString();
log.CreateTime = DateTime.Now;
new ECS.BLL.A_UserTracksLog().Add(log);
}
//访问用户令牌
private const string UserTrackVisittoken = "visitToken";
//访问用户令牌
public string VisitToken
{
get
{
if (HttpContext.Current.Request.Cookies[UserTrackVisittoken] == null)
CreateTrackCookie();
return HttpContext.Current.Request.Cookies[UserTrackVisittoken].Value;
}
}
private static void CreateTrackCookie()
{
HttpCookie trackCookie = new HttpCookie(UserTrackVisittoken);
trackCookie.Value = Guid.NewGuid().ToString();
trackCookie.Expires = DateTime.Now.AddDays(1);
HttpContext.Current.Response.AppendCookie(trackCookie);
HttpContext.Current.Response.Cookies.Add(trackCookie);
}
public bool IsReusable
{
get
{
return false;
}
}
}