加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

通过ajax记录网站UV、PV数

发布时间:2020-12-16 00:18:41 所属栏目:百科 来源:网络整理
导读: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 =

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

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读