通过ajax记录网站UV、PV数

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;
        }
    }

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值