### Webhook URL Configuration Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md Example Webhook URL to configure in the console. ```text https://your-domain.com/webhooks/attrikit ``` -------------------------------- ### GET /conversions Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 查询转化数据。用于获取转化相关的数据。 ```APIDOC ## [GET] /conversions ### Description 查询转化数据。用于获取转化相关的数据。 ### Method GET ### Endpoint /conversions ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### GET /events Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 查询事件列表。用于获取事件数据列表。 ```APIDOC ## [GET] /events ### Description 查询事件列表。用于获取事件数据列表。 ### Method GET ### Endpoint /events ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### First Request with curl Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md Example curl command to track an event, including authentication and event payload. ```bash curl -X POST https://api.attrikit.com/v1/events/track \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "event_name": "page_view", "properties": { "page_title": "首页", "page_url": "https://example.com" }, "timestamp": "2024-01-15T10:30:00Z" }' ``` -------------------------------- ### Response to First Request Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md Example response from the event tracking endpoint, showing success and event ID. ```json { "success": true, "data": { "event_id": "evt_1234567890", "received_at": "2024-01-15T10:30:01Z" } } ``` -------------------------------- ### GET /reports/conversion Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 获取转化报表。用于获取转化相关的报表数据。 ```APIDOC ## [GET] /reports/conversion ### Description 获取转化报表。用于获取转化相关的报表数据。 ### Method GET ### Endpoint /reports/conversion ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### GET /reports/performance Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 获取效果报表。用于获取性能相关的报表数据。 ```APIDOC ## [GET] /reports/performance ### Description 获取效果报表。用于获取性能相关的报表数据。 ### Method GET ### Endpoint /reports/performance ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### GET /users Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 获取用户列表。用于获取用户数据列表。 ```APIDOC ## [GET] /users ### Description 获取用户列表。用于获取用户数据列表。 ### Method GET ### Endpoint /users ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### GET /attribution Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 查询归因结果。用于获取归因分析的结果。 ```APIDOC ## [GET] /attribution ### Description 查询归因结果。用于获取归因分析的结果。 ### Method GET ### Endpoint /attribution ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### GET /users/:id/events Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 获取用户事件。用于获取指定用户的事件数据。 ```APIDOC ## [GET] /users/:id/events ### Description 获取用户事件。用于获取指定用户的事件数据。 ### Method GET ### Endpoint /users/:id/events ### Parameters #### Path Parameters - **id** (string) - Required - 用户 ID ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### GET /users/:id Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 获取用户详情。用于获取指定用户的详细信息。 ```APIDOC ## [GET] /users/:id ### Description 获取用户详情。用于获取指定用户的详细信息。 ### Method GET ### Endpoint /users/:id ### Parameters #### Path Parameters - **id** (string) - Required - 用户 ID ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### GET /events/:id Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 获取事件详情。用于获取指定事件的详细信息。 ```APIDOC ## [GET] /events/:id ### Description 获取事件详情。用于获取指定事件的详细信息。 ### Method GET ### Endpoint /events/:id ### Parameters #### Path Parameters - **id** (string) - Required - 事件 ID ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### 安装依赖并启动开发服务器 Source: https://github.com/attrikit/landing/blob/main/README.md 在项目根目录执行这些命令来安装依赖、启动开发服务器(默认端口 4321)、构建生产版本以及预览生产构建。 ```bash # 安装依赖 npm install # 启动开发服务器(localhost:4321) npm run dev # 构建生产版本 npm run build # 预览生产构建 npm run preview ``` -------------------------------- ### 安装小程序 SDK Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/quick-start.md 使用 npm 安装 @attrikit/miniprogram-sdk 包。 ```bash npm install @attrikit/miniprogram-sdk ``` -------------------------------- ### 项目目录结构 Source: https://github.com/attrikit/landing/blob/main/README.md 展示 Astro 项目的目录结构,包括组件、博客内容、页面、数据、工具函数和样式目录。 ```text src/ ├── components/ # Astro UI 组件 ├── content/blog/ # Markdown 博客文章 ├── pages/ # 文件路由(首页、博客、API) ├── data/ # SEO 结构化数据 ├── lib/ # 工具函数(飞书集成、类型定义) └── styles/ # 全局样式 ``` -------------------------------- ### 资源预加载:preconnect 和 dns-prefetch Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 预连接到 CDN 以优化加载性能。 ```html ``` -------------------------------- ### POST /events/batch Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 批量上报事件。用于一次发送多个事件数据。 ```APIDOC ## [POST] /events/batch ### Description 批量上报事件。用于一次发送多个事件数据。 ### Method POST ### Endpoint /events/batch ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### NPM 安装 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/installation.md 使用 npm 安装 Web SDK。 ```bash npm install @attrikit/web-sdk ``` -------------------------------- ### Web SDK 安装与初始化 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/quick-start.md 在网站 中添加此脚本以加载并初始化 AttriKit SDK。需替换 YOUR_APP_ID 为实际 App ID,生产环境建议将 debug 设为 false。 ```html ``` -------------------------------- ### 验证集成成功的控制台日志 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/quick-start.md 刷新网站后,在浏览器控制台看到这些日志表示 SDK 集成成功。 ```text [AttriKit] SDK initialized successfully [AttriKit] Page view tracked ``` -------------------------------- ### 小程序初始化 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/quick-start.md 在 app.js 中导入并初始化 SDK,设置 appId 和 autoTrack。 ```javascript import AttriKit from '@attrikit/miniprogram-sdk'; AttriKit.init({ appId: 'YOUR_APP_ID', autoTrack: true }); App({ onLaunch() { // SDK 已自动初始化 } }); ``` -------------------------------- ### 批量上报参数:高流量 vs 低流量 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 高流量网站建议增加 batchSize 并减少上报频率;低流量网站建议减少 batchSize 并增加上报频率。 ```javascript // 高流量网站 AttriKit.init({ appId: 'YOUR_APP_ID', batchSize: 20, // 增加批量大小 batchTimeout: 5000 // 减少上报频率 }); ``` ```javascript // 低流量网站 AttriKit.init({ appId: 'YOUR_APP_ID', batchSize: 5, // 减少批量大小 batchTimeout: 2000 // 增加上报频率 }); ``` -------------------------------- ### 跟踪自定义转化事件 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/quick-start.md SDK 初始化后,使用 AttriKit('track', ...) 跟踪按钮点击、表单提交和购买行为。 ```javascript // 跟踪按钮点击 AttriKit('track', 'button_click', { button_name: 'purchase_now', page_location: 'homepage' }); // 跟踪表单提交 AttriKit('track', 'form_submit', { form_type: 'contact', form_id: 'main_contact_form' }); // 跟踪购买行为 AttriKit('track', 'purchase', { order_id: 'ORD-12345', revenue: 299.00, currency: 'CNY', product_count: 2 }); ``` -------------------------------- ### NPM 初始化 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/installation.md 导入并初始化 SDK,需要提供 appId。 ```javascript import AttriKit from '@attrikit/web-sdk'; AttriKit.init({ appId: 'YOUR_APP_ID' }); ``` -------------------------------- ### 懒加载 SDK:loadAttriKit 函数 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 仅在需要时动态加载 SDK 脚本,并在加载完成后初始化。 ```javascript // 仅在需要时加载 SDK function loadAttriKit() { if (shouldTrackUser()) { const script = document.createElement('script'); script.src = 'https://cdn.attrikit.com/sdk/latest/attrikit.min.js'; script.onload = () => { AttriKit.init({ appId: 'YOUR_APP_ID' }); }; document.head.appendChild(script); } } ``` -------------------------------- ### 异步上报配置 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md SDK 默认使用异步上报,确保 async: true(默认值)以避免阻塞主线程。 ```javascript // 确保 async: true(默认值) AttriKit.init({ appId: 'YOUR_APP_ID', async: true }); ``` -------------------------------- ### 初始化时机:head 标签 vs 页面底部 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 推荐在 head 标签中尽早初始化 SDK,避免在页面底部或异步加载导致遗漏早期事件。 ```javascript // ✅ 推荐:在 head 标签中尽早初始化 ``` ```javascript // ❌ 避免:在页面底部或异步加载 ``` -------------------------------- ### 验证安装命令 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/installation.md 检查 CDN 加载状态和 NPM 包版本。 ```bash # 检查 CDN 加载状态 curl -I https://cdn.attrikit.com/sdk/latest/attrikit.min.js # 检查 NPM 包版本 npm list @attrikit/web-sdk ``` -------------------------------- ### 事件去重:trackOnce 函数 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 使用 Set 防止重复上报相同事件,基于事件名和属性生成唯一键。 ```javascript // 防止重复上报 const trackedEvents = new Set(); function trackOnce(eventName, properties) { const eventKey = `${eventName}_${JSON.stringify(properties)}`; if (!trackedEvents.has(eventKey)) { AttriKit('track', eventName, properties); trackedEvents.add(eventKey); } } ``` -------------------------------- ### Yarn 安装 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/installation.md 使用 yarn 安装 Web SDK。 ```bash yarn add @attrikit/web-sdk ``` -------------------------------- ### CDN 引入 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/installation.md 通过 CDN 引入 SDK,推荐方式。 ```html ``` -------------------------------- ### 用户同意:条件初始化 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 检查用户是否同意跟踪,仅在同意后初始化 SDK,否则在用户点击接受时初始化。 ```javascript // 检查用户是否同意跟踪 if (userHasConsented()) { AttriKit.init({ appId: 'YOUR_APP_ID' }); } else { // 只在用户同意后初始化 document.getElementById('accept-consent').addEventListener('click', () => { AttriKit.init({ appId: 'YOUR_APP_ID' }); }); } ``` -------------------------------- ### POST /events/validate Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 验证事件数据格式。用于检查事件数据是否符合要求。 ```APIDOC ## [POST] /events/validate ### Description 验证事件数据格式。用于检查事件数据是否符合要求。 ### Method POST ### Endpoint /events/validate ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### POST /reports/export Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 导出报表。用于导出报表数据。 ```APIDOC ## [POST] /reports/export ### Description 导出报表。用于导出报表数据。 ### Method POST ### Endpoint /reports/export ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 ``` -------------------------------- ### 敏感数据处理:避免 vs 推荐 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 避免直接上报敏感信息,推荐使用哈希或匿名化处理。 ```javascript // ❌ 避免:直接上报敏感信息 AttriKit('track', 'purchase', { email: 'user@example.com', phone: '13812345678' }); ``` ```javascript // ✅ 推荐:使用哈希或匿名化 AttriKit('track', 'purchase', { email_hash: hashEmail('user@example.com'), phone_last_4: '5678' }); ``` -------------------------------- ### POST /events/track Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md 上报单个事件。用于将用户行为事件发送到 AttriKit 平台。 ```APIDOC ## [POST] /events/track ### Description 上报单个事件。用于将用户行为事件发送到 AttriKit 平台。 ### Method POST ### Endpoint /events/track ### Request Example { "event_name": "page_view", "properties": { "page_title": "首页", "page_url": "https://example.com" }, "timestamp": "2024-01-15T10:30:00Z" } ### Response #### Success Response (200) - **success** (boolean) - 请求是否成功 - **data** (object) - 返回数据 - **event_id** (string) - 事件 ID - **received_at** (string) - 接收时间 #### Response Example { "success": true, "data": { "event_id": "evt_1234567890", "received_at": "2024-01-15T10:30:01Z" } } ``` -------------------------------- ### Base URL Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md The base URL for all AttriKit API requests. ```text https://api.attrikit.com/v1 ``` -------------------------------- ### 事件命名:推荐 vs 避免 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 推荐使用动词_名词格式,避免模糊命名。 ```javascript // ✅ 推荐:使用动词_名词格式 AttriKit('track', 'view_product'); AttriKit('track', 'add_to_cart'); AttriKit('track', 'complete_purchase'); ``` ```javascript // ❌ 避免:模糊的命名 AttriKit('track', 'action1'); AttriKit('track', 'click'); ``` -------------------------------- ### Authentication Header Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md All API requests must include an Authorization header with a Bearer token and Content-Type header. ```http Authorization: Bearer YOUR_API_KEY Content-Type: application/json ``` -------------------------------- ### Webhook Signature Verification (JavaScript) Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md JavaScript function to verify Webhook signatures using HMAC-SHA256 and timing-safe comparison. ```javascript const crypto = require('crypto'); function verifyWebhook(payload, signature, secret) { const hmac = crypto.createHmac('sha256', secret); const digest = hmac.update(payload).digest('hex'); return crypto.timingSafeEqual( Buffer.from(signature), Buffer.from(digest) ); } ``` -------------------------------- ### Success Response Format Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md Standard success response structure with a success flag and data object. ```json { "success": true, "data": { // 返回数据 } } ``` -------------------------------- ### 数据采样:10% 采样率 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 对于高流量网站,使用随机采样减少上报量,示例中采样率为 10%。 ```javascript const SAMPLING_RATE = 0.1; // 10% 采样 if (Math.random() < SAMPLING_RATE) { AttriKit('track', 'page_view', properties); } ``` -------------------------------- ### 外部链接点击:延迟跳转上报 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 在链接跳转前上报事件,并延迟 100ms 确保事件发送。 ```javascript function trackExternalLink(url) { AttriKit('track', 'click_outbound', { url }); // 延迟跳转,确保事件发送 setTimeout(() => { window.location.href = url; }, 100); } ``` -------------------------------- ### 异常处理:onError 回调 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 在初始化时配置 onError 回调,上报错误到监控系统,并可选择存储到本地队列稍后重试。 ```javascript AttriKit.init({ appId: 'YOUR_APP_ID', onError: (error) => { // 上报错误到监控系统 console.error('[AttriKit Error]', error); // 可选:存储到本地队列稍后重试 saveToOfflineQueue(error); } }); ``` -------------------------------- ### SPA 路由变化:手动上报 page_view Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 监听路由变化并手动上报 page_view 事件,包含页面标题和 URL。 ```javascript router.afterEach((to) => { AttriKit('track', 'page_view', { page_title: to.meta.title, page_url: window.location.href }); }); ``` -------------------------------- ### Error Response Format Source: https://github.com/attrikit/landing/blob/main/src/content/docs/api/overview.md Standard error response structure with error code, message, and details. ```json { "success": false, "error": { "code": "ERROR_CODE", "message": "错误描述", "details": {} } } ``` -------------------------------- ### 浏览器控制台验证 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/installation.md 在浏览器控制台输出版本号。 ```javascript console.log(AttriKit.version); // 输出版本号 ``` -------------------------------- ### 数据一致性检查:订单数量对比 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 定期对比 AttriKit 数据与业务数据,若差异超过 10% 则发出告警。 ```javascript // 每日检查订单数量 const attrikitOrders = await getAttrikitOrderCount(); const actualOrders = await getActualOrderCount(); if (Math.abs(attrikitOrders - actualOrders) / actualOrders > 0.1) { alert('数据差异超过 10%,请检查'); } ``` -------------------------------- ### 环境变量配置 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/installation.md 通过环境变量管理敏感配置,区分生产与开发环境。 ```javascript // .env.production VITE_ATTRIKIT_APP_ID=prod_app_id_xxx VITE_ATTRIKIT_ENDPOINT=https://api.attrikit.com // .env.development VITE_ATTRIKIT_APP_ID=dev_app_id_xxx VITE_ATTRIKIT_ENDPOINT=https://dev-api.attrikit.com ``` ```javascript // config.js const config = { appId: import.meta.env.VITE_ATTRIKIT_APP_ID, endpoint: import.meta.env.VITE_ATTRIKIT_ENDPOINT, debug: import.meta.env.DEV }; ``` -------------------------------- ### 数据验证:validateEvent 函数 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/guides/best-practices.md 上报前验证事件数据,检查必填字段和数据类型,不满足条件时返回 false 并输出错误。 ```javascript function validateEvent(eventName, properties) { // 必填字段检查 if (eventName === 'purchase' && !properties.order_id) { console.error('purchase event requires order_id'); return false; } // 数据类型检查 if (properties.revenue && typeof properties.revenue !== 'number') { console.error('revenue must be a number'); return false; } return true; } ``` -------------------------------- ### CORS 配置 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/installation.md 使用自定义 endpoint 时,服务端需要配置 CORS 响应头。 ```javascript // 服务端配置 Access-Control-Allow-Origin: https://your-domain.com Access-Control-Allow-Methods: POST, OPTIONS Access-Control-Allow-Headers: Content-Type, X-AttriKit-Signature ``` -------------------------------- ### 高级配置 Source: https://github.com/attrikit/landing/blob/main/src/content/docs/getting-started/installation.md 展示初始化参数的高级用法,包括自定义 endpoint、批量上报、用户识别和生命周期钩子。 ```javascript AttriKit.init({ appId: 'YOUR_APP_ID', autoTrack: true, debug: false, // 自定义 endpoint(私有部署时使用) endpoint: 'https://your-domain.com/api/events', // 批量上报配置 batchSize: 20, batchTimeout: 3000, // 用户识别 userId: 'user_12345', // 自定义用户 ID userTraits: { plan: 'premium', signup_date: '2024-01-01' }, // 生命周期钩子 onReady: () => console.log('SDK ready'), onError: (error) => console.error('SDK error:', error) }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.