> For the complete documentation index, see [llms.txt](https://polyv.gitbook.io/document/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://polyv.gitbook.io/document/docs/vod/api/statistics/bigdata/get_viewer_analysis.md).

# 查询观众分析

### 接口描述

```
1、通过观众id查询观众分析
2、接口URL中的{userid}为点播账号userid，具体参考【获取密钥】
3、接口支持https协议
```

### 接口URL

```
http://api.polyv.net/v2/advance/viewer/{userid}
```

[在线API调用](https://git.polyv.net/help-center/document-center/-/blob/master/req.html?api=http:/api.polyv.net/v2/advance/viewer/%7Buserid%7D/README.md)

### 请求方式

```
GET
```

### 接口约束

1、接口同时支持HTTP 、HTTPS ，建议使用HTTPS 确保接口安全，接口调用有频率限制，[详细请查看](https://git.polyv.net/help-center/document-center/-/blob/master/vod/api/limit/README.md)\
2、高级分析功能介绍详见： [视频高级分析](https://git.polyv.net/help-center/document-center/-/blob/master/vod/api/videoproduct/manual_statistic/advance_analysis.md)\
3、由于数据量和计算量大，数据分析结果次日才可查询<br>

### 请求参数描述

| 参数名      | 必选   | 类型     | 说明                                                                                                                                                                                                        |
| -------- | ---- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| userId   | true | String | 保利威点播账户id，可以参考【[获取密钥](https://git.polyv.net/help-center/document-center/-/blob/master/vod/api/getSecretKey/README.md)】获取，获取路径：官网->登录->点播（API接口）                                                           |
| ptime    | true | Long   | 当前时间的毫秒级时间戳，3分钟内有效                                                                                                                                                                                        |
| sign     | true | String | 签名，为40位大写的SHA1值,`生成签名的secretkey密钥作为通信数据安全的关键信息，严禁保存在客户端直接使用，所有API都必须通过客户自己服务器中转调用POLYV服务器获取响应数据`【详见[签名生成规则](https://git.polyv.net/help-center/document-center/-/blob/master/vod/api/buildSign/README.md)】 |
| viewerId | true | String | 自定义观众id                                                                                                                                                                                                   |

### 示例

```requestUrl
http://api.polyv.net/v2/advance/viewer/1b448be323?viewerId=1555313336634&sign=2178D4B940BA22F790D642341947E87631C7C455&userid=1b448be323&ptime=1617790781595
```

### 响应参数描述

| 参数名     | 类型      | 说明                                                                                                                                                            |
| ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| code    | Integer | 响应状态码，200为成功返回，非200为失败【详见[全局错误说明](https://git.polyv.net/help-center/document-center/-/blob/master/vod/api/errorInfo/README.md)】                               |
| status  | String  | 响应状态文本信息                                                                                                                                                      |
| message | String  | 响应描述信息，当code为400或者500的时候，辅助描述错误原因                                                                                                                             |
| data    | Object  | 响应成功时返回观众分析详细数据 【详见[data字段说明](https://git.polyv.net/help-center/document-center/-/blob/master/vod/api/statistics/bigdata/get_viewer_analysis.md?id=data字段说明)】 |

#### data字段说明

| 参数名                | 类型      | 说明                                        |
| ------------------ | ------- | ----------------------------------------- |
| userId             | String  | POLYV用户ID，和保利威官网一致，获取路径：官网->登录->点播（API接口） |
| viewerId           | String  | 观众id                                      |
| viewerNickName     | String  | 观众昵称                                      |
| viewerAatar        | String  | 观众头像                                      |
| ip                 | String  | ip地址                                      |
| firstWatchTime     | Long    | 首次观看时间，13位毫秒级时间戳                          |
| lastWatchTime      | Long    | 最后观看时间，13位毫秒级时间戳                          |
| totalVideoCount    | Integer | 观看视频总数                                    |
| totalWatchDuration | Integer | 观看总时长，单位：秒                                |
| avgCompletionRate  | Double  | 平均观看完成度                                   |

**返回错误代码列表**

| 错误代码 | message                                                   | 说明             |
| ---- | --------------------------------------------------------- | -------------- |
| 400  | sign can not be empty.                                    | 加密串为空          |
| 400  | ptime is too old.                                         | 时间戳过期          |
| 400  | ptime is illegal.                                         | ptime超出当前时间3分钟 |
| 400  | Could not find user by userid.                            | userid不存在      |
| 400  | the sign is not right.                                    | 加密串错误          |
| 401  | viewerId is blank.                                        | viewerId为空     |
| 402  | cannot find the viewer analytics data.                    | 查不到对应的观众分析数据   |
| 403  | get viewer analytics failed.                              | 查询观众分析数据失败     |
| 500  | get viewer analytics failed. please check the backend log | 后台抛异常          |

### Java请求示例

快速接入基础代码请下载相关依赖源码， [点击下载源代码](https://git.polyv.net/help-center/document-center/-/blob/master/third_res/util.zip) ,下载后加入到自己的源码工程中即可。测试用例中的**HttpUtil.java 和 VodSignUtil.java** 都包含在下载文件中。

> 强烈建议您使用[点播Java SDK](https://git.polyv.net/help-center/document-center/-/blob/master/vod/java/README.md)完成API的功能对接，点播Java SDK 对API调用逻辑、异常处理、数据签名、HTTP请求线程池进行了统一封装和优化。

```java
private static final Logger log = LoggerFactory.getLogger(VodBigdataTest.class);
/**
 * 查询观众分析
 */
@Test
public void testGetViewerAnalysis() throws Exception, NoSuchAlgorithmException {
    //公共参数,填写自己的实际参数
    String secretKey = super.secretKey;
    String userId = super.userId;
    String ptime = String.valueOf(System.currentTimeMillis());
    //业务参数
    String url = String.format("http://api.polyv.net/v2/advance/viewer/%s", userId);
    String viewerId = "1555313336634";

    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("userid", userId);
    requestMap.put("ptime", ptime);
    requestMap.put("viewerId", viewerId);

    requestMap.put("sign", VodSignUtil.getSign(requestMap, secretKey));
    String response = HttpUtil.get(url, requestMap);
    log.debug("测试查询观众分析，{}", response);
    //do somethings
}
```

### 响应示例

系统全局错误说明详见[全局错误说明](https://git.polyv.net/help-center/document-center/-/blob/master/vod/api/errorInfo/README.md)

成功示例

```json
{
	"code": 200,
	"status": "success",
	"message": "success",
	"data": {
		"userId": "1b448be323",
		"viewerId": "1555313336634",
		"viewerNickName": "polyv_user_junit",
		"viewerAatar": null,
		"ip": "xxxxxxx",
		"firstWatchTime": 1617014826000,
		"lastWatchTime": 1617014871000,
		"totalVideoCount": 1,
		"totalWatchDuration": 68,
		"avgCompletionRate": 0.261
	}
}
```

异常示例

```json
{
  "code": 400,
  "status": "error",
  "message": "ptime is too old.",
  "data": ""
}
```
