根据业务需求提出的统计代码。

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>统计测试</title>
</head>
</head>

<body>

    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"> </script>
    <script src="https://cdn.wilddog.com/js/client/current/wilddog.js"></script>
    <script type="text/javascript" src="http://www.coding123.net/getip.ashx?js=1"></script>
    <script>

// <summary>
// 时间类
// </summary>
Date.prototype.format = function(format){
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp("("+ k +")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
        }
    }
    return format;
}

// <summary>
// Guid
// </summary>
function newGuid()
{
    var guid = "";
    for (var i = 1; i <= 32; i++){
        var n = Math.floor(Math.random()*16.0).toString(16);
        guid +=   n;
        if((i==8)||(i==12)||(i==16)||(i==20))
            guid += "-";
    }
    return guid.replace(/-/g,"");
}

// <summary>
// 全局变量
// </summary>
var data =  new Wilddog("https://tongjiwx.wilddogio.com/"); //野狗对象
var Mydate = new Date();    //Date对象
var day = Mydate.format("yyyy年MM月dd日"); //日期
var time = Mydate.toLocaleTimeString(); //当前时间
var times = Mydate.getTime(); //时间戳
var ip = ip.replace("|",""); //ip地址
var PV = 0; //浏览次数
var UV = 0; //独立访客
var guid = newGuid(); //guid
var lock = false; //单向锁(ip群锁)
var lock2 = false; //单项锁(获取pv、uv锁)

// <summary>
// 插入新的ip群
// </summary>
insert =  function()
{
    //alert(guid);
    data.child("IP").child(day).child(guid).set
    ({
        "ip":ip,
        "time":time
    })
} 

// <summary>
// 更新pv次数和uv次数
// </summary>
update = function()
{
    data.child("N").child(day).set
    ({
        "PV":PV,
        "UV":UV
    })
}

// <summary>
// 获取pv次数和uv次数
// </summary>
Get_PVUV = function()
{
    data.child("N").child(day).on('value',function(snapshot)
    {
        if(lock2 == false)
        {
            lock2 = true;

            var json = snapshot.val();
            console.log("我是Get_PVUV");console.log(json);
            //当天第一次读取的时候,会返回null
            if(json == null)
            {
                PV = 1;
                UV = 1;
            }
            else
            {
                PV = json.PV;
                UV = json.UV;
            }
            console.log(PV+ "|" +UV);
            Get_IPJSON();
        }
    })
}

// <summary>
// 获取IPJSON
// </summary>
Get_IPJSON = function()
{
    data.child("IP").child(day).on('value',function(snapshot)
    {
        if(lock == false)
        {
            lock = true; //加上单向锁,只能执行一遍
            var jsonstr = JSON.stringify(snapshot.val());
            console.log("我是Get_IPJSON");console.log(jsonstr);
            if(jsonstr != "null") //当天首次监听则会返回Null,那么直接按照Get_PVUV()的初始化值(pv=1|uv=1)来更新即可
            {
                if(jsonstr.indexOf(ip) < 0)
                {
                    UV = UV + 1;
                }
                PV = PV + 1;
            }
            update();
            insert();
        }
    })
}

// <summary>
// 函数调用区(Main)
// </summary>
Get_PVUV();

</script>

</body>
</html>

Wilddog - 野狗统计的更多相关文章

  1. Wilddog - 野狗常用知识点

    https://www.wilddog.com/examples/chat-var1 https://z.wilddog.com/web/quickstart 增加或者修改替换整条数据(注意,upda ...

  2. 『原创』手把手教你搭建一个实用的油耗App(一)

    前言: 入行快10年,有点积蓄,三年前买了代步车.于是乎,汽车油耗开销就成了每个月都必须关注的问题.三年来,用过了无数油耗记录软件,比如最知名的“小熊油耗”,从第一次用,一直到最新一版,感觉越来越“臃 ...

  3. Ionic3 Demo

    本文为原创文章,转载请标明出处 最近又开源了一个小 Demo,基于 Ionic 3.9.2.注册登录功能使用的是 WildDog 野狗通信云,大家可以放心的注册登录玩.电影相关数据来源自"某 ...

  4. Ionic3学习笔记(十六)上传头像至图床

    本文为原创文章,转载请标明出处 个人做的开源 Demo 登录注册模块采用的是 Wilddog 野狗通讯云的身份认证服务,不得不说各方面和 Google 收购的 Firebase 很像,十分简单易用.其 ...

  5. 使用野狗(Wilddog)云setValue写入数据

    - (void)viewDidLoad { [super viewDidLoad]; //创建野狗实例化对象 用于随时监听数值变化 Wilddog *myRootRef = [[Wilddog all ...

  6. 基于React,Redux以及wilddog的聊天室简单实现

    本文主要是使用ReactJs和Redux来实现一个聊天功能的页面,页面极其简单.使用React时间不长,还是个noob,有不对之处欢迎大家吐槽指正. 还要指出这里没有使用到websocket等技术来实 ...

  7. spark处理大规模语料库统计词汇

    最近迷上了spark,写一个专门处理语料库生成词库的项目拿来练练手, github地址:https://github.com/LiuRoy/spark_splitter.代码实现参考wordmaker ...

  8. TFS 测试用例步骤数据统计

    TFS系统集成了一套BI系统,基于SQL Server的Analysis Service进行实现的.通过这几年的深入使用,能够感触到这个数据数据仓库模型是多么的优秀,和微软官方提供的数据仓库示例Adv ...

  9. java统计字符串单词的个数

    在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下. 本人没怎么写个播客,如果有啥说的不对的地方,你来打我啊 不说废话了直接贴代码: 实现代码 ...

随机推荐

  1. Java入门的程序汇总

    Java入门的基础程序,虽然很简单,也要多练习,下面有重点的总结一下 1.HelloWorld这个不说了 2.常量与变量部分 基本数据类型使用 public class Javashujuleixin ...

  2. 七维互联(www.7wei.com)

    七维互联     http://www.7wei.com/ 黄云贵的Delphi   http://www.cnblogs.com/huangygdelphi/articles/2232171.htm ...

  3. Java for LeetCode 077 Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  4. Java for LeetCode 033 Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  5. codeforces B. Xenia and Spies 解题报告

    题目链接:http://codeforces.com/problemset/problem/342/B 题目意思:有n个spy,编号从1-n,从左到右排列.现在的任务是,spy s要把信息传递到spy ...

  6. cf50A(水题)

    题意:m*n的地板最多能铺多少2*1的地板砖,不能重复... 水题.. 上代码... #include <iostream> #include <stdio.h> using ...

  7. Log4Net异常日志记录在asp.net mvc3.0的应用(转载)

    这篇博客写的很好:http://www.cnblogs.com/qianlifeng/archive/2011/04/22/2024856.html 前言 log4net是.Net下一个非常优秀的开源 ...

  8. ASP.NET Web.Config 读写辅助类

    using System; using System.Configuration; using System.Web; using System.Web.Configuration; namespac ...

  9. Struts2的@ResultPath

    转自:http://blog.csdn.net/yandufeng/article/details/8105495 这里我要补充一下:要理解Annotation,最好的方法还是看源码,struts中c ...

  10. Fragment基础讲解

    //新建一个碎片public class LeftFragment extends Fragment { @Override public View onCreateView(LayoutInflat ...