数据分析 /访问趋势 /获取用户访问小程序数据周趋势 # 获取用户访问小程序数据周趋势 > 接口应在服务器端调用,详细说明参见[服务端API](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/backend-api.html)。 > 本接口支持[云调用](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html)。需开发者工具版本 >= `1.02.1904090`(最新[稳定版下载](https://developers.weixin.qq.com/miniprogram/dev/devtools/stable.html)),`wx-server-sdk` >= `0.4.0` ## 接口说明 ### 接口英文名 getWeeklyVisitTrend ### 功能描述 该接口用于获取用户访问小程序数据周趋势 ### 注意事项 限定查询一个自然周的数据,时间必须按照自然周的方式输入: 如:20170306(周一), 20170312(周日) ## 调用方式 ### HTTPS 调用 ```text POST https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token=ACCESS_TOKEN ``` ### 云调用 - 出入参和 HTTPS 调用相同,调用方式可查看[云调用说明文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html) - 接口方法为: openapi.analysis.getWeeklyVisitTrend ### 第三方调用 - 调用方式以及出入参和 HTTPS 相同,仅是调用的 token 不同 - 该接口所属的权限集 id 为:18、21 - 服务商获得其中之一权限集授权后,可通过使用[authorizer_access_token](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/api_authorizer_token.html)代商家进行调用 ### 请求参数 | 属性 | 类型 | 必填 | 说明 | | :------------------------------------ | :----- | :--- | :----------------------------------------------------------- | | access_token / cloudbase_access_token | string | 是 | 接口调用凭证,该参数为 URL 参数,非 Body 参数。access_token和cloudbase_access_token二选一 其中access_token可通过[getAccessToken](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html)接口获得; 如果是第三方代调用请传入[authorizer_access_token](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/api_authorizer_token.html); cloudbase_access_token可通过[getOpenData](https://developers.weixin.qq.com/miniprogram/dev/wxcloudrun/src/guide/weixin/token.html)接口获得 | | begin_date | string | 是 | 开始日期,为周一日期。格式为 yyyymmdd | | end_date | string | 是 | 结束日期,为周日日期,限定查询一周数据。格式为 yyyymmdd | ### 返回参数 | | 属性 | 类型 | 说明 | | :--- | :----------------------------------------------------------- | :------------ | :------- | | | list | array<object> | 数据列表 | | | 属性类型说明ref_datestring时间,格式为 yyyymmdd-yyyymmdd,如:"20170306-20170312"session_cntnumber打开次数(自然周内汇总)visit_pvnumber访问次数(自然周内汇总)visit_uvnumber访问人数(自然周内去重)visit_uv_newnumber新用户数(自然周内去重)stay_time_uvnumber人均停留时长 (浮点型,单位:秒)stay_time_sessionnumber次均停留时长 (浮点型,单位:秒)visit_depthnumber平均访问深度 (浮点型) | | | ## 调用示例 > 示例说明: HTTPS调用 ### 请求数据示例 ```json { "begin_date" : "20170306", "end_date" : "20170312" } ``` ### 返回数据示例 ```json { "list": [ { "ref_date": "20170306-20170312", "session_cnt": 986780, "visit_pv": 3251840, "visit_uv": 189405, "visit_uv_new": 45592, "stay_time_session": 54.5346, "visit_depth": 1.9735 } ] } ``` > 示例说明: 云函数调用 ### 请求数据示例 ```text const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV, }) exports.main = async (event, context) => { try { const result = await cloud.openapi.analysis.getWeeklyVisitTrend({ "beginDate": '20170306', "endDate": '20170312' }) return result } catch (err) { return err } } ``` ### 返回数据示例 ```json { "list": [ { "refDate": "20170306-20170312", "sessionCnt": 986780, "visitPv": 3251840, "visitUv": 189405, "visitUvNew": 45592, "stayTimeSession": 54.5346, "visitDepth": 1.9735 } ], "errMsg": "openapi.analysis.getWeeklyVisitTrend:ok" } ``` ### 错误码 | 错误码 | 错误码取值 | 解决方案 | | :----- | :------------------------------------------------------ | :----------------------------------------------------------- | | -1 | system error | 系统繁忙,此时请开发者稍候再试 | | 40001 | invalid credential access_token isinvalid or not latest | 获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口 | | | | | | :--- | :--- | :--- | | | | | | | | | | | | |