# @qiov2/tgp-mp-sdk2

> Mini program sdk for TGP(Tencent Growth Platform).

Latest version **4.0.9** (published 2022-02-16) · 0 weekly downloads

## Install

```sh
npm install @qiov2/tgp-mp-sdk2
pnpm add @qiov2/tgp-mp-sdk2
yarn add @qiov2/tgp-mp-sdk2
bun add @qiov2/tgp-mp-sdk2
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.9 |
| Published | 2022-02-16 |
| First published | 2022-02-16 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 29.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | astarliu, perfaywang |
| Keywords | tgp-mp-sdk |

## Links

- npm: https://www.npmjs.com/package/@qiov2/tgp-mp-sdk2
- Repository: https://git.code.oa.com/beacon/beacon_sdk_js/tree/autoTrack/packages/mp-app
- npm.io page: https://npm.io/package/@qiov2/tgp-mp-sdk2

## Dependencies (1)

- [@qiov/beacon-main-core](https://npm.io/package/@qiov/beacon-main-core.md) 0.1.4

## Recent versions

- 4.0.9 (latest) — 2022-02-16
- 4.0.8 — 2022-02-16

## README

# 1. SDK 初始化

引入 SDk 后，需初始化 SDK

在应用的入口app.js引入SDK，添加代码:
```javascript

import BeaconAction from '@qiov2/tgp-mp-sdk2/beacon_mp.min.js'

var success = e => {
  console.log('onReportSuccess : ' + e);
};
var fail = e => {
  console.log('onReportFail : ' + e);
};
var beacon = new BeaconAction({
  appkey: 'appkey', //小程序appKey，从灯塔官网获取,必填
  reportUrl: 'https://xxx', // 上报url, 必填
  versionCode: 'versionCode', //小程序版本号，选填
  channelID: 'channelID', //小程序渠道号，选填
  openid: 'openid', // 用户标示符号，选填
  unionid: 'unionid', // 用户唯一标示符号，选填
  isDebug :false ,//是否测试环境，选填
  strictMode: true,//严苛模式开关, 打开严苛模式会主动抛出异常, 上线请务必关闭!!选填
  delay: 2000, // 普通事件延迟上报时间(单位毫秒), 默认2000(2秒),选填
  onPullDownRefresh: true,//下拉刷新事件统计，默认开启，选填
  onReachBottom: true,//页面下拉触底统计，默认开启，选填
  onReportSuccess: success, // 上报成功回调，选填
  onReportFail: fail, // 上报失败回调，选填
  oneidApi: 'http://yyy', // oneid生成地址，选填
});

```


# 2.API

### 2.1设置appkey
``` 
beacon.setAppKey('setAppKey');

```

### 2.2普通事件上报接口,小程序环境问题,性能较低,注意!!!! value 类型只能是string 或者 number 类型
``` 
beacon.onUserAction('eventCode', {
  'city': 'shenzhen'
});

```

### 2.3 实时事件上报接口,注意!!!! value 类型只能是string 或者 number 类型
``` 
beacon.onDirectUserAction('eventCode', {
  'city': 'shenzhen'
});


``` 

### 2.4 获取小程序beacon id
``` 
beacon.getDeviceId();

``` 
### 2.5 设置公共参数, 注意 !!!! , 相比老接口(setAdditionalParams)新接口是追加,老接口为替换
``` 
beacon.addAdditionalParams({
  'additionalParams': 'params'
});
``` 
### 2.6 手动上报PV
``` 
beacon.reportPV();

``` 
### 2.7 设置用户id
``` 
beacon.setOpenId('openid');


``` 
### 2.8 设置唯一id
``` 
beacon.setUnionid('setUnionid');

``` 

### 2.9 设置渠道id
``` 
beacon.setChannelId('setChannelId');

``` 
### 2.10 设置用户信息
``` 
// 获取用户信息
wx.getSetting({
  success: res => {
    if (res.authSetting['scope.userInfo']) {
      // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
      wx.getUserInfo({
        success: res => {
          beacon.setUserInfo(res);//灯塔会获取用户昵称
        }
      })
    }
  }
})

``` 
### 2.11 设置位置信息
``` 
wx.getLocation({
  type: "wgs84",
  success: function (res) {
    beacon.setLocation(res);
  }
})

``` 

### 2.12 用于小程序 与 H5 打通时，返回 H5 事件上报需携带的小程序预置属性，注意 ！！！，这里以拼接字符串的方式给出
```
wx.getWeappInfo() // 返回'beacon_uin=${uin}&beacon_uid=${uid}&beacon_is_first=${isFirst}'

```
## 3 小程序 与 H5 打通

### 3.1 方案概述

小程序与 H5 的数据打通，是小程序将必要预置属性（这里支持用户标识、用户ID和是否为新用户） 通过 URL 方式传递给 H5 端，H5 端解析URL替换对应事件属性，达到统一用户标识和其他必要属性的目的。

### 3.2 方案实现

1、调用 wx.setWebViewUrl(URL), 拼接好包含 distinctID 的 URL，微信小程序 V1.14.12 及以上版本支持；
2、URL 参数是绑定的 web-view 的 src 属性值

```
// webview.js
Page({
  data: {
    webUrl: ''
   },
  onLoad: function (options) {
    const URL = 'https://example/demo.html' 必填项
    this.setData({
      webUrl: beacon.setWebViewUrl(URL)
    });
  }
});
// webview.wxml
<web-view src="{{ webUrl }}"></web-view>
```

---
_Source: https://npm.io/package/@qiov2/tgp-mp-sdk2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
