npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zg-sdk-bytedance

v1.0.1

Published

诸葛智能字节小程序sdk

Downloads

5

Readme

字节小程序SDK使用指南

配置sdk安全域名

登录「字节小程序开发者平台」在「开发管理-开发设置-服务器域名」的「request合法域名」中,增加诸葛域名:

https://u.zhugeapi.net

https://ubak.zhugeio.com

开发者平台官方地址:https://microapp.bytedance.com/

安装sdk

在小程序中执行命令安装sdk包

npm install --production zg-sdk-bytedance

也可以将sdk文件拷贝到本地,进行本地引入

npm支持方式:https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/framework/npm/

引用sdk

1.在app.js文件头部,引入sdk

import 'zg-sdk-bytedance'
App.zhuge.load('AppKey', {
// load配置参数,具体见下方
})

mpvue引入方式见下方 在yourProject/src/main.js文件顶部,引入SDK文件。

import zhuge from 'zg-sdk-bytedance' // sdk的引用,需要在Vue和App之前
import Vue from 'vue'
import App from './App'

zhuge.load('此处填写您在诸葛申请的APP KEY', { // load方法具体参数,参见下面的接口说明
  pv: true
})

Vue.config.productionTip = false
App.mpType = 'app'

const app = new Vue(App)
app.$mount()

wepy引入方式见下方 在yourProject/app.wpy文件script标签中引入sdk文件

// app.wpy
import 'zg-sdk-bytedance'

App.zhuge.load('此处填写您在诸葛申请的APP KEY', {
  pv: true
})
//.eslintrc.js eslint配置增加App全局忽略
module.exports = {
  globals: { App: true}
}

2.引入成功后,即可在全局任意地方,通过App.zhuge调用sdk函数,如:App.zhuge.track('登录')

API列表

load(appkey, config)

  • appkey <string> 您在诸葛申请的APP KEY
  • config <object> sdk配置信息。不需要自定义的参数可忽略 完整默认配置如下:
{
  debug: false, // 开启实时调试
  serverUrl: { // 事件上报地址。使用的上报地址,应在微信开发设置中,注册为合法域名
    normal: 'https://u.zhugeapi.net', // 主地址
    bac: 'https://ubak.zhugeio.com' // 备用地址
  },
  timeout: 1800000, // 会话超时时间(默认30分钟),单位:毫秒
  did: '', // 自定义设备id,默认首次加载sdk时,sdk会自动为当前设备生成唯一的did(uuid)
  vn: '1.0', // 小程序版本(小程序项目本身的版本)
  pv: false, // 是否自动上报pv
  click: false, // 是否采集tap事件 默认关闭
  utmMode: 'session', // utm模式:session(会话级控制,即以会话开始时的utm为准)、fresh(就近原则)
  superProperty: { // 全局事件属性,设置后除会话、环境及identify事件外,所有事件都会自动加上该配置所有属性
    propA: 'hello'
  },
  requestTimeout: 60000, // 请求超时时间,单位毫秒。微信基础库最低版本要求为2.10.0。默认与最大值为60秒
  sendLimit: 1, // 批量上传限制,默认为1。即事件即时上传。当该配置大于1时,事件会累计达到limit限制再批量上传,也可调用flush方法手动清空
  duration: true, // 自动采集页面停留时长
  beforeDuration: (durationPage) => { // 函数参数为页面对象
    // 时长上报事件之前执行
  },
  afterDuration: (durationPage) => {
    // 时长上报事件之后执行
  },
  countDuration: 3000, // countDuration配置是trackCount函数连续触发的动作停止之后等待的时间,单位毫秒默认值为3000ms。超过等待时间会上传定义的事件和属性包含函数执行的次数和连续触发持续的时间
  exposure: false // 是否启动元素曝光采集 详细请看下文
}

使用示例:

App.zhuge.load('AppKey', {
	pv: true
})
  1. utm就近原则:每次打开小程序时,sdk即会再应用的onShow声明周期中,解析并更新utm信息。如果onShow时没有获取到有效utm信息,则utm信息保持不变。
  2. 当superProperty属性与上传事件时指定的属性冲突,则superProperty被覆盖
  3. 当页面onHide时,sdk会自动flush所有请求

identify(cuid, props)

  • cuid <string> 用户唯一标识
  • props <object> 用户属性

为了保持对用户的跟踪,你需要为他们记录一个识别码,可以使用手机号、email等唯一值来作为用户的识别码。另外,也可以在跟踪用户的时候, 记录用户更多的属性信息,便于你更了解你的用户
使用示例:

App.zhuge.identify('17711111111', {
  '性别': '男',
  '会员级别': '白金'
})

track(eventName, props)

  • eventName <string> 事件名称
  • props <object> 事件属性 使用示例:
App.zhuge.track('加入购物车', {
  '商品名称': '华为P30',
  '商品分类': '3C'
})

注意:在添加事件属性时,需注意事件属性类型。如果事件属性类型为「数值型属性」,需要在上传数据时修改数据类型为「数值型」,并且在诸葛io后台埋点管理中修改为「数值型」。

trackCount

  • eventName <string> 事件名称
  • props <object> 事件属性
App.zhuge.trackCount('like', {
  '商品名称': '华为P30',
  '商品分类': '3C'
})

trackCount是用来记录事件触发的次数,适用于频繁触发事件的场景比如视频直播点赞。

函数连续触发时会记录执行的次数,当函数停止执行会等待集成sdk设置的countDuration时间,超时后会上传事件和事件属性,通过trackCount函数上传的事件会附带两个自定义属性count和countTime,分别代表执行的次数和连续触发持续的时间。

注意 函数执行的过程中如果事件名称和事件属性发生变化trackCount函数会立刻上传上次的事件和属性,然后重新开始计数。

setUtm(utm)

  • utm <object> 需要设置的utm信息。utm包括:utm_source, utm_medium, utm_campaign, utm_content, utm_term 使用示例:
App.zhuge.setUtm({
  utm_source: '扫码',
  utm_medium: '长按图片识别二维码'
})

调用setUtm方法时,utm信息会强制更新,不会受utmMode限制。设置后的utm信息,会在后续事件上传中生效。该方法本身不会上传任何事件。

setSuperProperty(superProperty)

  • superProperty <object> 全局事件属性

extendSuperPropertys(property)

  • 合并property到全局事件属性

removeSuperProperty(delList)

  • 移除全局事件属性,函数参数为数组

getSuperProperty

  • 获取全局事件属性

flush()

手动批量上传已缓存的所有事件

trackRevenue(props)

  • props <object> 收入数据属性
{
  price: 0, // <number>价格
  productID: null, // <string>商品id
  productQuantity: 0, // <number>商品数量
  revenueType: null // <string>收入类别
}

收入数据采集 使用示例:

App.zhuge.trackRevenue({
  price: 2000,
  productID: 'product001',
  productQuantity: 2,
  revenueType: '3c'
})

price和productQuantity属性,只能为数值类型。

记录转发分享

转发分享数据默认不采集,如需采集,需要在调用load方法时,设置forwardShare为true

启动元素曝光采集

用户使用约定的规则标记元素,元素由不可见变为可见时(页面滚动,显示隐藏)sdk会自动采集配置在元素上的自定义事件名称和事件属性,会以自定义事件的方式采上传数据。

将sdk配置exposure设置为true,在小程序元素中增加zhuge-expo-track类名,使用data-expo-xxx="yyy"的形式设置自定义事件名称和事件属性,其中data-expo-name="evt"会被识别为事件名其他标记会识别为事件属性。

zhuge.load('test-appkey', {
  exposure: true
})
<view 
  class="zhuge-expo-track" 
  data-expo-name="首页banner曝光" // 事件名称 “首页banner曝光”
  data-expo-pos="页头" // 事件属性 pos: "页头"
  data-expo-type="商品" // 事件属性 type: "商品"
>
  xxxx
</view>

setPushClient(clientId: string, channel: string)

推送id上报

  • clientId <string> 客户端id
  • channel <string> 推送渠道。默认个推

推送渠道支持:jpush(极光推送), umeng(友盟), xiaomi(小米), baidu(百度), xinge(信鸽), getui(个推)

zhuge.setPushClient('xxx', 'getui')

常见问题

1. 集成后报错:Uncaught ReferenceError: regeneratorRuntime is not defined

检查小程序开发工具是否开启增强编译,没开启的话,开启后即可避免报错。如果由于生产环境原因, 不能开启增强编译,可以使用es5版本的sdk:

npm install --production zg-sdk-bytedance@es5 

es5版本的sdk文件会略大于latest版本

2. 页面事件无法上传,或duration无法监听

这类问题通常是一些小程序框架集成中,sdk无法有效监听页面生命周期导致的,如taro1.x、2.x版本, 此时可以手动在各个页面组件中,对生命周期进行监听,示例代码如下:

import { Component } from '@tarojs/taro';
import { View } from '@tarojs/components';
class Index extends Component {
  constructor(props) {
    super(props);
  }

  componentDidShow() {
    App.zhuge.lifecycle.Page.onShow();
  }

  componentDidHide() {
    App.zhuge.lifecycle.Page.onHide();
  }

  componentDidMount() {
    App.zhuge.lifecycle.Page.onLoad(this.$router.params);
  }

  componentWillUnmount() {
    App.zhuge.lifecycle.Page.onUnLoad();
  }

  render() {
    return (
      <View>首页</View>
    );
  }
}

export default Index;

3. sendLimit批量上传时,上传时机是什么时候?

当设置了sendLimit时,触发的所有埋点请求,会达到limit指定数量时,将所有埋点数据合并为一个请求进行上传。另外为避免数据丢失的可能性,当页面触发Page的onHide、onUnload生命周期时,不管请求数量是否达到limit限制,都会将已缓存数据进行上传。