近期的项目中要开发站点流量统计平台,在前端,我们通过在网站页面中引入js脚本的方式对页面进行统计,主要统计三个事件:

  • 网页加载时向统计后台发送相关数据
  • 网页被关闭时向统计后台发送相关数据
  • 页面任何位置被点击时向后台发送坐标位置数据

 

代码如下:

statistics.js

var HOST = "http://statistics.abc.cn/";

/**
 * 设置cookieId
 */
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    var cookieStr = "\""+c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()+ ";")+"\"";
    console.log(cookieStr);
    document.cookie = cookieStr;
}
/**
 * 获取cookieId
 */
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
/**
 * 获取当前时间戳
 */
function getTimestamp() {
    var timestamp = Date.parse(new Date());
    return timestamp;

相关文章:

  • 2022-02-20
  • 2022-12-23
  • 2021-08-18
  • 2021-07-16
  • 2022-02-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
相关资源
相似解决方案