# troila-mobile-tracer-luobo

> 埋点统计

Latest version **4.2.14** (published 2020-01-03) · ISC license · 0 weekly downloads

## Install

```sh
npm install troila-mobile-tracer-luobo
pnpm add troila-mobile-tracer-luobo
yarn add troila-mobile-tracer-luobo
bun add troila-mobile-tracer-luobo
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.2.14 |
| Published | 2020-01-03 |
| First published | 2019-12-30 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 74.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | justjavac |
| Maintainers | troila_mobile |
| Keywords | tracer |

## Links

- npm: https://www.npmjs.com/package/troila-mobile-tracer-luobo
- Repository: http://172.26.1.194/tracer/tracer
- Homepage: http://172.26.1.194/tracer/tracer#README
- Issues: http://172.26.1.194/tracer/tracer/issues
- npm.io page: https://npm.io/package/troila-mobile-tracer-luobo

## Dependencies (4)

- [dayjs](https://npm.io/package/dayjs.md) ^1.7.7
- [debug](https://npm.io/package/debug.md) ^4.0.1
- [warning](https://npm.io/package/warning.md) ^4.0.2
- [@troila/deviceinfo-rn](https://npm.io/package/@troila/deviceinfo-rn.md) ^4.1.0

## Recent versions

- 4.2.14 (latest) — 2020-01-03
- 4.2.13 — 2020-01-03
- 4.2.12 — 2020-01-03
- 4.2.11 — 2020-01-03
- 4.2.10 — 2020-01-02
- 4.2.9 — 2020-01-02
- 4.2.8 — 2020-01-02
- 4.2.7 — 2020-01-02
- 4.2.6 — 2020-01-02
- 4.2.5 — 2019-12-30
- 4.2.4 — 2019-12-30
- 4.2.3 — 2019-12-30

## README

# 行为统计埋点库

[![pipeline status](http://172.26.1.194/tracer/tracer/badges/master/pipeline.svg)](http://172.26.1.194/tracer/tracer/commits/master)
[![coverage report](http://172.26.1.194/tracer/tracer/badges/master/coverage.svg)](http://172.26.1.194/tracer/tracer/commits/master)

用来对用户的操作行为进行埋点统计，支持离线存储和实时上报。

**此版本为萝卜开会专用**。

## 安装

### CDN

暂时还不支持 UMD 模式。

当前只有两种打包方式：commonjs 和 es6 module，后续版本会增加对 UMD 模式的支持。

### npm

使用 npm

```bash
$ npm install troila-mobile-tracer-luobo
```


## 初始化

默认的导出 `tracer` 是一个对象，需要调用 `init` 进行初始化设置。**在项目入口文件中进行初始化一次**。

```js
import tracer from "troila-mobile-tracer-luobo";

tracer.init({
  debug: false, // 是否开启调试模式
  appKey: "test", // app标识
  appVersion: "1.2.3", // 应用版本号
  env: "dev", // 环境
  delay: 1000, // 延迟上报时间，0 为立即上报
  beforeSend: function(report) {
    console.log(report);
  },
  afterSend: function(result) {
    console.log(result);
  },
  userid: "", // 当前登录的用户id
  maxEvents: 100, // 离线最大记录条数，默认 100 条
  offlineLog: true // 是否开启离线记录
});
```

### 示例

```js
import tracer from "troila-mobile-tracer-luobo";

// 初始化
tracer.init({
  appVersion: "1.0.0",
  appKey: "test",
  env: "dev",
  channel: "play store",
  debug: true
});

tracer.push({
  id: "login"
});

tracer.push({
  id: "login",
  category: "event",
  desc: "用户首次登录"
});
```

### 配置项

| 属性            | 类型        | 是否必填 | 默认值          | 说明                                       |
| --------------- | ----------- | -------- | --------------- | -------------------------------------- |
| `appVersion`    | `string`    | 是       | -               | 应用版本号                               |
| `appKey`        | `string`    | 是       | -               | 应用标识                                 |
| `debug`         | `boolean`   | 否       | `false`         | 是否开启调试模式                          |
| `delay`         | `number`    | 否       | `0`             | 上报延迟的毫秒数，0 为立即上报              |
| `env`           | `string`    | 否       | `"dev"`         | 环境 `"dev", "test", "release"`         |
| `channel`       | `string`    | 否       | `""`            | 渠道                                    |
| `beforeSend`    | `function`  | 否       | -               | 发送之前执行的回调函数                     |
| `afterSend`     | `function`  | 否       | -               | 发送之后执行的回调函数                     |
| `userid`        | `string`    | 否       | `""`            | 当前登录的用户 id                         |
| `offlineMaxNum` | `number`    | 否       | `100`           | 离线存储的最大值，超过最大值后则上报到服务器   |
| `offlineLog`    | `boolean`   | 否       | `true`          | 是否开启离线存储                          |

## API

### `tracer.init(option)`

初始化。

如果未初始化而调用了后面的 api，则抛出异常。

### `tracer.setUserid(userid)`

设置当前登录用户 ID。在 SDK 初始化的时候 `userid` 是非必传项，如果当用户登录后需要统计当前登录用户，可以使用此 API 设置 userid。

### `tracer.push(action)`

记录用户行为。

#### 参数

action

| 属性       | 类型       | 是否必填  | 默认值     | 说明                                 |
| --------- | ---------- | -------- | --------- | ----------------------------------- |
| `id`      | `string`   | 是       | -         | 动作id                              |
| `name`    | `string`   | 否       | 默认为id   | 动作名                              |
| `category`| `string`   | 否       | `'event'` | 动作分类                             |
| `desc`    | `string`   | 否       | -         | 描述                                 |
| `ext`     | `string`   | 否       | -         | 附加信息                             |
| `tags`    | `string[]` | 否       | -         | 标签，用于给行为分类，方便以后的统计 |

- 如果 `name` 没有设置，则默认使用 `id` 作为动作名。
- 如果 `category` 没有设置，则默认使用 `'event'`。

内置有 3 种行为：`event`、`eventStart`、`eventEnd`。`event` 为瞬时行为，`eventStart`、`eventEnd` 结合使用用来统计具有持续时间的行为。

### `tracer.nav(navigation)`

记录页面跳转行为。

#### 参数

navigation

| 属性       | 类型       | 是否必填  | 默认值     | 说明                                 |
| --------- | ---------- | -------- | --------- | ----------------------------------- |
| `id`      | `string`   | 是       | -         | 页面id                              |
| `name`    | `string`   | 否       | 默认为页面id| 页面名                              |
| `from`    | `string`   | 是       | -         | 跳转来源                              |
| `desc`    | `string`   | 否       | -         | 描述                                 |
| `ext`     | `string`   | 否       | -         | 附加信息                             |
| `tags`    | `string[]` | 否       | -         | 标签，用于给行为分类，方便以后的统计 |

- 如果 `name` 没有设置，则默认使用 `id` 作为页面名。
- 如果没有页面来源，`from` 传空字符串。

### `tracer.reportOffline()`

上报离线日志。

如果不调用此方法，则用户日志条数达到 `offlineMaxNum` 了会自动上报。

## 开发

此项目使用 typescript 开发。

### 日志格式

```json
{
  "timestamp": "2017-04-24T09:36:01.000Z",
  "id": "login",
  "name": "login",
  "category": "event",
  "ext": "",
  "userid": "",
  "desc": "",
  "tags": [...]
}
```

### 上报格式

```json
{
  "appVersion": "1.2.3",
  "env": "release",
  "channel": "play store",
  "uniqueId":"c3a3bd65caf0015c",
  "logs": [
    {
      "timestamp": "2017-04-24T09:36:01.000Z",
      "id": "clickBuy",
      "name": "点击购买",
      "category": "event",
      "ext": "",
      "userid": "",
      "desc": "",
      "tags": ["a", "b"]
    },
    {
      "timestamp": "2017-04-24T09:36:01.000Z",
      "id": "login",
      "category": "event",
      "ext": "",
      "userid": "",
      "desc": "",
      "tags": ["a", "b"]
    },
    {
      "timestamp": "2017-04-24T09:36:01.000Z",
      "id": "login",
      "name": "登录",
      "category": "navigation",
      "from": "",
      "ext": "",
      "userid": "",
      "desc": "",
      "tags": ["a", "b"]
    },
    {
      "timestamp": "2017-04-24T09:36:01.000Z",
      "id": "view",
      "name": "查看",
      "category": "navigation",
      "from": "main",
      "ext": "",
      "userid": "",
      "desc": "",
      "tags": ["a", "b"]
    }
  ]
}
```

## TODO

使用 [lerna](https://github.com/lerna/lerna) 将每个功能放到单独的 npm 包进行管理。

例如：

- @tracer/core
- @tracer/storage
- @tracer/storage-memery
- @tracer/storage-localstorage
- @tracer/storage-indexeddb
- @tracer/storage-xxxxxx
- @tracer/reporter
- @tracer/reporter-fetch
- @tracer/reporter-sendbeacon
- @tracer/reporter-axios
- @tracer/reporter-xxxxx


### luobo-tracer 说明文档

> 现地址：http://172.26.1.194/troila-cloudvideo/luobomeeting/data-platform/frontend/tracer

> 原地址：~~http://172.26.1.194/troila-cloudvideo/luobomeeting/data-platform/frontend/tracer~~


---
#### 发布流程

1. 登录npm账号

    登录npm淘宝镜像

    ```
    npm login --registry http://registry.npmjs.org
    ```

    > 账号：troila_mobile  

    > 密码：dfqwe5335458

    > 邮箱：yulianlin@troila.com

2. ts转js文件

    执行下面命令

    ```
    tsc --p tsconfig.json
    ```

    **如果assets文件下有文件更新，需要手动copy过去**

3. 修改version

    版本管理，修改version版本号，基本原则如下：

    > 主版本号（Major）：当你做了不兼容的API修改

    > 次版本号（Minor）：当你做了向下兼容的功能性新增

    > 修订号（Patch）：当你做了向下兼容的问题修正


 4. 发布

    ```
    npm publish
    ```

5. 检查

    访问地址 *https://npm.taobao.org/package/troila-mobile-tracer-luobo*，
    查看Current Tags 是否为最新版本，如果不是，需要手动同步

    a. 手动同步
    
    ![手动同步](./sync.png)

    b. 同步成功

    ![同步成功](./sync_success.png)

6. 通知业务童鞋去拉去最新版本啦

---
_Source: https://npm.io/package/troila-mobile-tracer-luobo · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
