js 实现 页面统计

  • Post author:
  • Post category:其他




js 代码



<script>

    var d = new Date().getTime();
    var uuidDate = Math.round((d / 1000));
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    uuid = uuidDate + '|' + uuid;
    let url = '/Tracker';

    $.post(url, {
        url: window.location.href,
        uuid: uuid,
        referrer: document.referrer
    }, function (e) {}, 'json');

    //     /*用户在线时长*/
    setInterval(function (e) {
        var tracker = true;
        // document.hidden == true的话 离开了页面
        if (document.hidden === true) {
            document.addEventListener('visibilitychange', () => {
            })
        } else {
            let data = {
                url: window.location.href,
                uuid: uuid,
                referrer: document.referrer
            };
            $.post(url, data, function (e) {
            }, 'json');
        }

    }, 5000)

</script>





后端代码1


<?php
/**
 * Created by.
 * User: Jim
 * Date: 2020/11/17
 * Time: 8:53
 */

namespace app\index\controller;

use app\index\controller\master\Common;
use think\Controller;
use think\Db;

/**
 * 上传用户浏览页面的信息
 * Class Tracker
 * @package app\index\controller
 */
class Tracker extends Common
{

    /**
     * 上传数据
     */
    public function index()
    {

        $user_id = session('user_id');
        $uuid = $this->request->post('uuid');
        $ip = $this->request->ip();
        $url = $this->request->post('url');
        $user_agent =$this->request->header('User-Agent');
        $referer =$this->request->header('Referer'); // 从哪里点击的
        $data = [
            'user_id'=>$user_id,
            'uuid'=>$uuid,
            'ip'=>ip2long($ip),
            'url'=>$url,
            'user_agent'=>$user_agent,
            'referer'=>$referer,
            'create_time'=>time(),
        ];
        Db::name('tracker')->insert($data);
        return json_encode([
            'code'=>1,
            'message'=>'succes'
        ]);
    }


}



后端代码 – 分库处理

<?php
/**
 * Created by.
 * User: Jim
 * Date: 2020/11/17
 * Time: 8:53
 */

namespace app\index\controller;

use app\index\controller\master\Common;
use think\Controller;
use think\Db;
use think\facade\Config;

/**
 * 上传用户浏览页面的信息
 * Class Tracker
 * @package app\index\controller
 */
class Tracker extends Common
{

    static $tableName = '';
    static $tablePrefix = '';
    static $tableSuffix = '';

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        self::$tablePrefix = Config::get('database.analysis.prefix');
        self::$tableSuffix = date('Ym', time());
        self::$tableName = self::$tablePrefix . 'tracker_' . self::$tableSuffix;

    }

    /**
     * 上传数据
     */
    public function index()
    {

        $user_id = session('user_id');
        $uuid = $this->request->post('uuid');
        $ip = $this->request->ip();
        $url = $this->request->post('url');
        $user_agent = $this->request->header('User-Agent');
        $referer = $this->request->post('referrer'); // 从哪里点击的
        $data = [
            'user_id' => $user_id,
            'uuid' => $uuid,
            'ip' => ip2long($ip),
            'url' => $url,
            'user_agent' => $user_agent,
            'referer' => $referer,
            'create_time' => time(),
        ];
        $this->detectTable();
        Db::connect('analysis')->table(self::$tableName)->insert($data);
        return json_encode([
            'code' => 1,
            'message' => 'success'
        ]);
    }


    private function detectTable()
    {
        $check =  Db::connect('analysis')->query("show tables like '" . self::$tableName . "'");
        if (empty($check)) {
            $sql = $this->getCreateSql();
            Db::connect('analysis')->execute($sql);
        }
        return true;
    }

    /**
     * 根据后缀获取创建表的sql
     * @return string
     */
    protected function getCreateSql()
    {
        $tableName = self::$tableName;
        return <<<EOT
CREATE TABLE `{$tableName}` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) DEFAULT '0',
  `uuid` char(47) DEFAULT '',
  `ip` bigint(20) DEFAULT '0',
  `url` varchar(200) DEFAULT '',
  `user_agent` varchar(255) DEFAULT '',
  `referer` varchar(200) DEFAULT '',
  `create_time` int(11) DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `url` (`url`),
  KEY `referer` (`referer`),
  KEY `ip` (`ip`),
  KEY `uuid` (`uuid`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COMMENT='追踪用户前端浏览的信息';
EOT;
    }

}



版权声明:本文为qq_19598963原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。