跳到正文

分析

产品分析:track、identify、漏斗与同期群。

1. 概览

基础路径: https://api.infrai.cc/v1/analytics
鉴权头: Authorization: Bearer $INFRAI_API_KEY
bash
# Call any /v1/analytics capability over raw HTTP — no SDK to install.
# curl:
curl https://api.infrai.cc/v1/analytics/... \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json"

2. 方法 (8)

2.1analytics.track

POST /v1/analytics/track

追踪一个或多个分析事件。

参数

名称类型必填说明
eventstring
必填
事件名称。
pattern: ^[a-zA-Z][a-zA-Z0-9_.-]{0,127}$
distinct_idstring
必填
操作用户的稳定 id。
propertiesRecord<string, unknown>可选任意事件属性。

返回

{ ok, accepted }
名称类型说明
acceptedbooleanitems accepted for processing数量
event_idstring | nullevent的唯一标识符
reasonstring | null当前状态或操作原因

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/analytics/track \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"event": "sample", "distinct_id": "sample"}'

2.2analytics.identify

POST /v1/analytics/identify

将特征属性关联到某个 distinct id。

参数

名称类型必填说明
distinct_idstring
必填
操作用户的稳定 id。
user_idstring可选连接用户的 id。
traitsRecord<string, unknown>可选要设置的用户特征。

返回

{ ok: boolean }
名称类型说明
distinct_idstring用户或实体的唯一标识
traits_merged_countinteger | nulltraits merged during identification数量
≥ 0

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/analytics/identify \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"distinct_id": "sample"}'

2.3analytics.alias

POST /v1/analytics/alias

合并身份别名

参数

名称类型必填说明
old_idstring
必填
要合并的来源 distinct_id(例如匿名 id)。
new_idstring
必填
合并目标的规范 distinct_id。
idempotency_keystring可选可选幂等键,确保重复请求只生效一次。

返回

AliasResult { merged: boolean }
名称类型说明
old_idstring别名前的先前标识符
new_idstring别名后的新标识符
mergedboolean身份是否已合并

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/analytics/alias \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"old_id": "sample", "new_id": "sample"}'

2.4analytics.batch

POST /v1/analytics/batch

批量上报事件

参数

名称类型必填说明
eventsTrackEvent[]
必填
至多 1000 条 TrackEvent 事件,每条事件自带幂等键。
1–1000 items

返回

{ ok: boolean, accepted: number }
名称类型说明
acceptedintegeritems accepted for processing数量
≥ 0
rejectedintegeritems rejected due to validation errors数量
≥ 0
errorsobject[]批量操作中逐条错误的数组
errors[].indexinteger-
≥ 0
errors[].reasonstring-

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/analytics/batch \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"events": [{"event": "sample", "distinct_id": "sample"}]}'

2.5analytics.group

POST /v1/analytics/group

关联群组

参数

名称类型必填说明
distinct_idstring
必填
要关联到群组的用户 distinct_id。
group_typestring
必填
群组维度,例如 company / team。
group_keystring
必填
在 group_type 下的群组标识符。
traitsRecord<string, unknown>可选可选的群组档案属性。
idempotency_keystring可选可选幂等键,确保重复请求只生效一次。

返回

{ ok: boolean }
名称类型说明
distinct_idstring用户或实体的唯一标识
group_typestring分析群组类型
group_keystring分析群组的键

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/analytics/group \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"distinct_id": "sample", "group_type": "sample", "group_key": "sample"}'

2.6analytics.query.events

POST /v1/analytics/query/events

查询原始事件

参数

名称类型必填说明
sincestring
必填
时间窗口起点(含)。
format: date-time
untilstring
必填
时间窗口终点(不含)。
format: date-time
filterRecord<string, unknown>可选可选属性过滤(键/值匹配)。
limitnumber可选本页返回的最大条数(1-1000,默认 1000)。
1–1000default: 1000
cursorstring可选来自上一页 next_cursor 的不透明游标。

返回

EventQueryResult { events: object[], next_cursor?: string }
名称类型说明
itemsany[]本页结果条目数组
next_cursorstring | null获取下一页的不透明游标;null 或不存在表示最后一页
total_estimateinteger | null匹配事件的预估总数
≥ 0

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/analytics/query/events \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"since": "sample", "until": "sample"}'

2.7analytics.query.retention

POST /v1/analytics/query/retention

查询留存

参数

名称类型必填说明
born_eventstring
必填
定义群组归属(第 0 天)的事件。
return_eventstring
必填
其再次发生用于衡量留存的事件。
sincestring
必填
时间窗口起点。
format: date-time
untilstring
必填
时间窗口终点。
format: date-time
interval"day" | "week" | "month"可选分桶周期(默认 day)。
default: "day"

返回

RetentionResult { matrix: number[][] }
名称类型说明
cohortsobject[]留存群组数据点
cohorts[].datestring-
format: date
cohorts[].totalinteger-
≥ 0
cohorts[].returningobject[]-
cohorts[].returning[].intervalinteger-
≥ 0
cohorts[].returning[].ninteger-
≥ 0

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/analytics/query/retention \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"born_event": "sample", "return_event": "sample", "since": "sample", "until": "sample"}'

2.8analytics.query.path

POST /v1/analytics/query/path

查询行为路径

参数

名称类型必填说明
start_eventstring
必填
路径树展开的根事件。
sincestring
必填
时间窗口起点。
format: date-time
untilstring
必填
时间窗口终点。
format: date-time
max_depthnumber可选路径树最大深度(1-20,默认 5)。
1–20default: 5

返回

PathResult { root: string, children: object[] }
名称类型说明
rootstring路径树的根节点
childrenobject[]路径树中的子节点
children[].eventstring-
children[].usersinteger-
≥ 0
children[].childrenobject[]-

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/analytics/query/path \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"start_event": "sample", "since": "sample", "until": "sample"}'

3. 全部能力

本模块全部已路由能力——完整的对外 REST 契约。上方方法是带讲解的入门示例,此表是完整参考。

analytics.aliasPOST /v1/analytics/alias

Rename or merge an anonymous distinct_id (old_id) into a known identity (new_id).

参数 (3)
名称类型必填说明
old_idstring必填The distinct_id to rename from (e.g. anonymous id).
new_idstring必填The canonical distinct_id to merge into.
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
analytics.batchPOST /v1/analytics/batch

Ingest up to 1000 TrackEvent events in a single call (idempotency is per-event).

参数 (1)
名称类型必填说明
eventsobject[]必填Up to 1000 TrackEvent items.
1–1000 items
analytics.groupPOST /v1/analytics/group

Associate a distinct_id with a group (organization/team/account) for B2B account-level aggregation.

参数 (5)
名称类型必填说明
distinct_idstring必填Distinct identifier for the user or entity
group_typestring必填Group dimension, e.g. "company" / "team".
group_keystring必填Group identifier within group_type.
traitsobject | null可选Optional group profile traits.
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
analytics.identifyPOST /v1/analytics/identify

Set profile traits on a distinct_id, optionally merging a prior id via alias_for.

参数 (4)
名称类型必填说明
distinct_idstring必填Distinct identifier for the user or entity
traitsobject | null可选Profile traits to merge onto the user.
alias_forstring | null可选Optional prior distinct_id to merge into this one at identify time.
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
analytics.query.eventsPOST /v1/analytics/query/events

Query raw events within a time window, with attribute filters and cursor pagination.

参数 (5)
名称类型必填说明
sincestring必填Window start (inclusive).
format: date-time
untilstring必填Window end (exclusive).
format: date-time
filterobject | null可选Optional property filter (key/value match).
limitinteger可选Maximum number of items to return per page
1–1000default: 1000
cursorstring | null可选Opaque cursor from a prior page's next_cursor.
analytics.query.pathPOST /v1/analytics/query/path

Build behavioral path/sequence trees rooted at a start event within a time window.

参数 (4)
名称类型必填说明
start_eventstring必填Root event the path tree branches from.
sincestring必填ISO 8601 date when the current tier or state became effective
format: date-time
untilstring必填ISO 8601 date/time for the end of the query range
format: date-time
max_depthinteger可选Maximum depth of the path tree to return
1–20default: 5
analytics.query.retentionPOST /v1/analytics/query/retention

Compute a retention matrix bucketed by period from a born event to a return event.

参数 (5)
名称类型必填说明
born_eventstring必填Event that defines cohort membership (day 0).
return_eventstring必填Event whose recurrence defines retention.
sincestring必填ISO 8601 date when the current tier or state became effective
format: date-time
untilstring必填ISO 8601 date/time for the end of the query range
format: date-time
interval"day" | "week" | "month"可选Time interval for retention buckets (e.g. day, week)
default: "day"
analytics.trackPOST /v1/analytics/track

Ingest a single product analytics event with properties for a distinct user; idempotent.

参数 (6)
名称类型必填说明
eventstring必填Event name you are recording (e.g. 'signup', 'purchase') — the verb of the action.
pattern: ^[a-zA-Z][a-zA-Z0-9_.-]{0,127}$
distinct_idstring必填Distinct identifier for the user or entity
propertiesobject | null可选Additional properties for risk assessment
timestampstring | null可选Unix timestamp of the data point
format: date-time
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
contextobject | null可选{ip, ua, session_id}

4. 完整示例

本模块的生产级端到端范例:先一次性配置,再运行业务流程,尽量覆盖本模块的多数 API。

单文件可运行 Python 程序(仅标准库、无 SDK):拷贝后填入 INFRAI_API_KEY 运行,即可按真实业务流逐步体验本模块核心 API——每一步都真实调用并计费,后续步骤复用前一步返回的真实字段。12 行 helper 就是全部集成代码。

python
#!/usr/bin/env python3
"""Infrai · analytics — runnable real-app example (single file, zero deps).

Copy this file, set your key, run it: every step is a REAL call to
api.infrai.cc, billed at the real (tiny) per-call price, printing the
live JSON response. Get a key at https://infrai.cc/login (Google/
GitHub sign-in grants $2 free credit); add funds at
https://infrai.cc/billing. No SDK — the 12-line helper below is the
entire integration."""
import json
import os
from urllib import error, request

KEY = os.environ.get("INFRAI_API_KEY") or "ifr_..."  # <- your key
BASE = "https://api.infrai.cc"


# Same raw HTTPS POST/GET as every per-method example on this page —
# wrapped once for reuse. There is nothing else to it: no SDK.
def infrai(method, path, body=None):
    req = request.Request(
        BASE + path, method=method,
        data=json.dumps(body).encode() if body is not None else None,
        headers={"Authorization": f"Bearer {KEY}",
                 "Content-Type": "application/json"})
    try:
        with request.urlopen(req, timeout=60) as r:
            return json.loads(r.read())
    except error.HTTPError as e:
        return json.loads(e.read())


def show(label, resp):
    print(f"\n== {label} ==")
    print(json.dumps(resp, indent=2, ensure_ascii=False))
    return resp


# 1) analytics.track — POST /v1/analytics/track · Ingest a single product analytics event with properties for a distinct user; idempotent.
r1 = show("analytics.track", infrai("POST", "/v1/analytics/track", {"event":"checkout_completed","distinct_id":"user_42","properties":{"amount":19.99}}))

# 2) analytics.identify — POST /v1/analytics/identify · Set profile traits on a distinct_id, optionally merging a prior id via alias_for.
r2 = show("analytics.identify", infrai("POST", "/v1/analytics/identify", {"distinct_id":"sample"}))