安装神策分析SDK模块
**二选一即可**
npm install sa-sdk-javascript
yarn add sa-sdk-javascript
初始化配置
- 在src目录下新建sensorsdata(名字自己定义),现在index.js文件
import sensors from 'sa-sdk-javascript'
sensors.init({
server_url: 'http://12.99.100.206:8080/sa?project=test',
is_track_single_page: true,
use_client_time: true,
show_log: true,
heatmap: {
clickmap: 'not_collect',
scroll_notice_map: 'not_collect',
}
})
export const setTrackTime = key => {
if(!key) {
return
}
const time = sensors?.time || {}
time[key] = Date now()
sensors.time= time
return time[key]
}
export const getDisTrackTime = key => {
if(!key) {
return
}
const time = sensors?.time || {}
const startTime = time[key]
const endTime = Date.now()
return endTime - startTime
}
export const useSensors = () => {
const username = window.USER_INFO.username
sensors.login(username)
}
window.$sensors = {
sensors,
setTrackTime,
getDisTrackTime,
}
在项目的app入口文件引入
import { useSensors } from '@/sensorsdata'
const App = () => {
......
useSensors()
.....
}
在需要埋点的地方使用
onClick = () => {
window.$sensors.setTrackTime('lb_click_time')
.....
window.$sensors.sensors.track('lb_click', {
'delay_time': window.$sensors.getDisTrackTime('lb_click_time'),
...
})
}