﻿// ==UserScript==
// @name                页面流量统计
// @description         页面流量统计
// @created date        2009/10/12   
// @created author      Daniel
// @version             1.0.1
// @version owner       Copyright (C) 2009-2010 keerr.com All rights reserved.
// ==/UserScript==


var _tag = 0;
var _informationid = 0;
var _tablename = "";
var _locations = "";
var _second = 0;
var _onlinesecond = 0;
var _ip = "";
var _address = "";
var _arr;
function OnlineStayTimes() { _second++; }
function OnlineUserStayTimes() { _onlinesecond++; }
function _getIP() {
    window.setInterval("OnlineStayTimes();OnlineUserStayTimes();", 1000);
    $.ajax({
        url: "/ajax/ip.asp",
        type: "post",
        dataType: "text",
        data: "",
        timeout: 5000,
        error: function() {
        },
        success: function(text) {
            _arr = text.split("||");
            _ip = _arr[0];
            _address = _arr[1];
        }
    });
    var str = document.location;
    _locations = (str.toString()).split('?')[0];
}
function pageTotal() {
    var tempid;
    var souceUrl = document.referrer;
    var obj = splitKeyWords(souceUrl);
    _informationid = getUrlInfo(_locations)[0];
    _tablename = getUrlInfo(_locations)[1];
    $.ajax({
        url: "/ajax/GetJson.aspx",
        type: "post",
        data: { type: "pagetotal", ip: _ip, name: _locations, time: _second, address: _address, souce: obj[0], key: obj[1], onlinesecond: _onlinesecond, tagid: _tag, informationid: _informationid, tablename: _tablename },
        dataType: "text",
        timeout: 5000,
        error: function() {
        },
        success: function(text) {
            _onlinesecond = 0;
        }
    });
    //SetLastSixViews();
}

function splitKeyWords(v) {
    var result = new Array();
    var keyWords = "";
    var sUrl = "";
    if (v != "") {
        var temp = v.split('?');
        if (temp.length > 1) {
            sUrl = temp[0].split('/')[2];
            switch (sUrl) {
                case "www.baidu.com":
                    var bai = temp[1].split('&');
                    for (var i = 0; i < bai.length; i++) {
                        var k = bai[i].split('=');
                        if (k[0] == 'wd') {
                            keyWords = k[1];
                            break;
                        }
                    }
                    break;
                case "www.google.cn":
                    var goo = temp[1].split('&');
                    for (var i = 0; i < goo.length; i++) {
                        var k = goo[i].split('=');
                        if (k[0] == 'q') {
                            keyWords = k[1];
                            break;
                        }
                    }
                    break;
                case "cn.bing.com":
                    var bin = temp[1].split('&');
                    for (var i = 0; i < bin.length; i++) {
                        var k = bin[i].split('=');
                        if (k[0] == 'q') {
                            keyWords = k[1];
                            break;
                        }
                    }
                    break;
                case "one.cn.yahoo.com":
                    break;
                case "www.soso.com":
                    break;
                case "www.sogou.com":
                    break;
                case "p.zhongsou.com":
                    break;
                default:
                    break;
            }
        }
        else sUrl = v.split('/')[2];
    } else sUrl = "";
    result.push(sUrl);
    result.push(keyWords);
    return result;
}

function getUrlInfo(s) {
    var us = (s.toString()).split('?')[0];
    var result = [0, ''];
    var temp = us.toString().split('/');
    if (temp.length > 0) {
        if (!isNaN((temp[temp.length - 1].split('.')[0]).replace('sec', '').replace('zf', '').replace('qg', '').replace('qz', '')))
            result[0] = (temp[temp.length - 1].split('.')[0]).replace('sec', '').replace('zf', '').replace('qg', '').replace('qz', '');
     }
     if (temp[temp.length - 2]) {

         if (!isNaN(temp[temp.length - 2]))
             result[1] = temp[temp.length - 2];
 
     }
     return result;
 }

window.onunload = function() {
    pageTotal();
};

//function SetLastSixViews() {
//    if ($.cookie("LastSixViews") == null) {
//        $.cookie("LastSixViews", "[{\"_title\":\"" + document.title + "\",\"_url\":\"" + document.location.href + "\"}]");
//        return true;
//    }
//    else {
//        var LastSixViewsJosn = $.evalJSON($.cookie("LastSixViews"));
//        var newLashSixView = "{\"_title\":\"" + document.title + "\",\"_url\":\"" + document.location.href + "\"},";
//        var j = 1;
//        for (var i = 0; i < LastSixViewsJosn.length; i++) {
//            if (document.title === LastSixViewsJosn[i]._title && document.location.href === LastSixViewsJosn[i]._url) {
//                continue;
//            }
//            newLashSixView = newLashSixView + "{\"_title\":\"" + LastSixViewsJosn[i]._title + "\",\"_url\":\"" + LastSixViewsJosn[i]._url + "\"}";
//            j++;
//            if (j < 10) {
//                newLashSixView = newLashSixView + ",";
//            }
//        }
//        newLashSixView = "[" + newLashSixView + "]";
//        $.cookie("LastSixViews", newLashSixView);
//        return true;
//    }
//}



