za-js-sdk
v4.19.2
Published
## 使用
Downloads
38
Readme
za-js-sdk
使用
从 npm 引入是推荐的方式,便于安装和管理:
import za from 'za-js-sdk'
// 记录页面展现
za.trackPageShow(view, extra)
// 记录元素展现
za.trackShow(ElementInfo)
// 记录元素点击
za.trackClick(ElementInfo)
// 记录s事件
za.trackEvent({...ElementInfo, eventType})
// 记录链接打开
za.trackOpenUrl(ElementInfo, ExtraInfo)
备选的引用方式是从 CDN 引入,然后使用全局对象 za
来访问接口:
<script src="https://unpkg.zhimg.com/za-js-sdk@2.24.6/dist/loader.js"></script>
注:可以使用 npm dist-tag za-js-sdk
来查询最新的版本号。
ElementInfo
的字段定义如下:
| 名称 | 类型 | 必填 | 说明 |
| ------- | -------- | ------- | ----------------------------------------------------------------------------------------- |
| type
| string
| true
| 元素类型,可能的值为 Button\|Text\|Image\|Page\|Audio\|Video\|Block\|Popup\|File\|Input
|
| block
| string
| false
| 元素标识,可以为任意值 |
| text
| string
| false
| 元素文本,可以为任意值 |
ExtraInfo
的字段定义如下:
| 名称 | 类型 | 必填 | 说明 |
| ----- | -------- | ------- | ------------------------ |
| url
| string
| false
| 元素标识,可以为任意网址 |
样例
// 记录页面展示
za.trackPageShow({
element_location: {
page: {page_id: '111', page_level: 4},
url: 'https://www.zhihu.com#hash',
},
})
// 记录 Reaction 弹层展示
za.trackShow({type: 'Popup', block: 'ReactionList'})
// 记录 Reaction 按钮点击
za.trackClick({type: 'Button', block: 'AddReaction', text: '赞'})
za.trackClick({type: 'Button', block: 'AddReaction', text: '踩'})
// 记录上划事件
za.trackEvent({eventType: 'SwipeUp', type: 'Card'})
// 记录返回首页
za.trackOpenUrl({type: 'Button', block: 'Home'}, {url: 'https://www.zhihu.com'})
线上的示例程序可以查看:https://codesandbox.io/s/za-js-sdk-yl7cs