zoukankan      html  css  js  c++  java
  • 百度统计api获取数据

    需求场景


    想要了解每天多少人访问了网站,多少个新增用户,地域分布,点击了哪些页面,停留了多久,等等。。。

    国内用的最多的就是百度统计吧,傻瓜式的注册然后插一段代码到项目里就行了。

    最近也在自己的博客里使用了百度统计,但是当想要获取这些数据时,看到官方文档,简直想骂人。网上也不是没有代码示例,但清一色的都是java代码,而官网给出的demo也是php,这是要逼死前端吗?


    来吧,直接上代码:

    1.  获取站点    https://api.baidu.com/json/tongji/v1/ReportService/getSiteList  

    const router = require('koa-router')()
    const fetch = require('node-fetch');   
    
    router.get('/siteList', async (ctx, next) => {
        let res = await fetch('https://api.baidu.com/json/tongji/v1/ReportService/getSiteList', {
            method: 'POST',
            body: JSON.stringify({
                "header": {
                    "account_type": "1",
                    "username": "百度统计账号",
                    "password": "百度统计登录密码",
                    "token": "token",
                }
            })
        }).then(res => {
            return res.json();
        }).then(res => {
            ctx.body = {
                code: 0,
                flag: true,
                rows: res.body.data[0].list,
                obj: {},
                total: 0
            }
        }).catch(e =>{
            ctx.body = {
                code: 0,
                flag: false,
                rows: [],
                obj: {},
                total: 0
            }
        })
    });
    
    
    module.exports = router

    2. 获取站点数据    https://api.baidu.com/json/tongji/v1/ReportService/getData

    const router = require('koa-router')()
    const fetch = require('node-fetch');
    
    
    router.get('/statistics', async (ctx, next) => {
        let res = await fetch('https://api.baidu.com/json/tongji/v1/ReportService/getData', {
            method: 'POST',
            body: JSON.stringify({
                "header": {
                    "account_type": "1",
                    "username": "百度统计账号",
                    "password": "百度统计登录密码",
                    "token": "token",
                },
                "body": {
                    "site_id": "12847821",
                    "method": "overview/getTimeTrendRpt",
                    "start_date": "20181128",
                    "end_date": "20251212",
                    "metrics": "pv_count,visitor_count,ip_count,avg_visit_time",
                    "gran": "day",
                    "max_results": "0"
                }
            })
        }).then(res => {
            return res.json();
        }).then(res => {
            ctx.body = {
                code: 0,
                flag: true,
                rows: res.body.data[0].result,
                obj: {},
                total: 0
            }
        }).catch(e =>{
            ctx.body = {
                code: 0,
                flag: false,
                rows: [],
                obj: {},
                total: 0
            }
        })
    });
    
    module.exports = router

    以上是基于 Koa2 的代码,不懂也没关系,主要是 绿色参数部分,这是官网文档没有写的,前端其它请求方式 ajaxaxiosfetch 都可以参考。

  • 相关阅读:
    二叉搜索树和双向链表 --剑指offer
    复杂链表的复制 --剑指offer
    二叉树中和为某一值的路径 --剑指offer
    二叉搜索树的后序遍历序列 --剑指offer
    从上往下打印二叉树 --剑指offer
    栈的压入、弹出序列 --剑指offer
    顺时针打印矩阵 --剑指offer
    树的子结构 --剑指offer
    JSON.toJSONString(joinPoint.getArgs())报错getOutputStream() has already been called for this response
    常用linux命令
  • 原文地址:https://www.cnblogs.com/hcxy/p/10107682.html
Copyright © 2011-2022 走看看