在投入生产之前,先验证你的集成能否正常工作。
注册免费套餐,并从仪表板的 API Keys 中获取你的公钥——它可以立即使用,
并且可以安全地随客户端代码一起发布。请将它用于本地开发和测试。将它传递给
Tracio.init():
import { Tracio } from "@tracio/sdk"
const tracio = Tracio.init({ publicKey: "YOUR_PUBLIC_KEY" })const result = await tracio.getResult()console.log(result.visitorId, result.bot.detected)如果你通过免构建的 edge 代码片段加载 agent,同一个密钥可以用在 k 查询参数中:
<script src="https://edge.tracio.ai/s.js?k=YOUR_PUBLIC_KEY" async></script>在已加载 SDK 的页面的浏览器控制台中运行以下代码,确认它已初始化并返回结果:
import { Tracio } from "@tracio/sdk"
const tracio = Tracio.init({ publicKey: "YOUR_PUBLIC_KEY" })const result = await tracio.getResult()console.assert(typeof result.visitorId === "string", "visitorId should be a string")console.assert(typeof result.bot.detected === "boolean", "bot.detected should be a boolean")console.log("Integration verified:", result.visitorId)在单元测试中,你通常不希望发起真实的网络请求。请模拟(mock)Tracio 模块,并断言你的代码正确地使用了 getResult()。以 Vitest 为例:
import { vi, test, expect } from "vitest"import { Tracio } from "@tracio/sdk"
vi.mock("@tracio/sdk", () => ({ Tracio: { init: () => ({ getResult: async () => ({ visitorId: "test-visitor-id", bot: { detected: false, confidence: 0 }, }), }), },}))
test("renders the visitor id", async () => { const tracio = Tracio.init({ publicKey: "YOUR_PUBLIC_KEY" }) const result = await tracio.getResult() expect(result.visitorId).toBe("test-visitor-id") expect(result.bot.detected).toBe(false)})你也可以通过让被模拟的 getResult() 以一个 TracioError 拒绝(reject),来测试错误处理逻辑。
若要在你自己的服务器上对识别结果采取操作,请以签名的
webhook 形式接收它——每个事件都会附带一个
X-Tracio-Signature HMAC 投递到你的端点,你可以据此进行验证。