@eclicktech/js-sdk
Version:
The JavaScript browser client library for DataEye
172 lines (156 loc) • 5.02 kB
HTML
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>js-sdk sync</title>
</head>
<body>
<div>
<h3>设置访客ID</h3>
访客ID:
<input id="distinctId" type="text" />
<button onclick="identify()">配置访客ID</button>
<button onclick="getDistinctId()">获取访客ID</button>
</div>
<div>
<h3>设置账号ID</h3>
账号ID:
<input id="accountId" type="text" />
<button onclick="login()">设置账号ID</button>
<button onclick="logout()">清除账号ID</button>
</div>
<div>
<h3>获取设备ID</h3>
<button onclick="getDeviceId()">获取设备ID</button>
</div>
<div>
<h3>上传事件</h3>
<button onclick="track()">上传事件</button>
</div>
<div>
<h3>自采集事件</h3>
<button onclick="autoTrack()">自采集事件</button>
</div>
<div>
<h3>设置页面公共属性</h3>
<button onclick="setPageProperty()">设置页面公共属性</button>
<button onclick="setDynamicSuperProperties()">设置页面动态公共属性</button>
<button onclick="setSuperProperties()">设置静态公共属性</button>
</div>
<div>
<h3>心跳</h3>
<button onclick="setHeartbeat()">开始心跳</button>
<button onclick="clearHeartbeat()">结束心跳</button>
<button onclick="setHeartbeatProps()">设置心跳参数</button>
<button onclick="removeHeartbeatProps()">删除心跳参数</button>
</div>
<script>
function initSDK() {
window.fa = window.funsdata;
// 创建 SDK 配置对象
const config = {
appId: "460",
serverUrl: "https://deapi.funsdata.com/v1/sdk/report",
showLog: false,
// debug模式,
mode: "debug",
// 开启上报加密
// encrypt: true
};
// 用配置对象初始化 SDK
fa.init(config);
}
// 配置访客ID
function identify() {
let distinctId = document.getElementById("distinctId").value;
fa.identify(distinctId);
}
// 获取访客ID
function getDistinctId() {
let distinctId = fa.getDistinctId();
alert(distinctId);
}
// 设置账号ID
function login() {
let accountId = document.getElementById("accountId").value;
fa.login(accountId);
}
// 清除账号ID
function logout() {
fa.logout();
document.getElementById("accountId").value = "";
}
// 获取设备ID
function getDeviceId() {
console.log(fa.getDeviceId());
}
// 开始心跳
function setHeartbeat() {
fa.startHeartbeat(3000);
}
// 结束心跳
function clearHeartbeat() {
fa.clearHeartbeat();
}
// 设置心跳参数
function setHeartbeatProps() {
fa.setHeartbeatProps({
test1: 1,
test2: 1,
});
fa.setHeartbeatProps({
test2: 2,
test3: 3,
});
}
// 删除心跳参数
function removeHeartbeatProps() {
// fa.removeHeartbeatProps() // 删除所有自定义心跳上报参数
// fa.removeHeartbeatProps('test1') // 删除单个心跳上报参数
fa.removeHeartbeatProps(["test1", "test3"]); // 删除多个心跳上报参数
}
// 上传事件
async function track() {
try {
const { res, req } = await fa.track(
"test", // 追踪事件的名称
{
exampleProp1: "testValue1",
exampleProp2: "testValue2",
} // 需要上传的事件属性
);
// 当前请求参数
console.log(JSON.parse(req));
// 响应参数
console.log(JSON.parse(res));
} catch (error) {
// 抛出错误
console.error(error);
}
}
// 自采集事件
function autoTrack() {
fa.quick('autoTrack')
}
// 设置页面公共属性
function setPageProperty() {
fa.setPageProperty({ page: "page10001" });
}
// 设置页面动态公共属性
function setDynamicSuperProperties() {
fa.setDynamicSuperProperties(function() {
const d = new Date();
d.setHours(10);
return { date: d };
});
}
// 设置静态公共属性
function setSuperProperties() {
fa.setSuperProperties({ channel: "渠道名", user_name: "用户名" });
}
</script>
<script async onload="initSDK()" src="../dist/umd.min.js"></script>
</body>
</html>