instagram-private-api 帖子画像数据解析:深入 InsightsServicePostResponseActions 接口与主页动作(Profile Actions)指标

原创2026-10-15 14:25:531,726 阅读
文章标签:后端

instagram-private-api 帖子画像数据解析:深入 InsightsServicePostResponseActions 接口与主页动作(Profile Actions)指标

本文聚焦 instagram-private-api 项目(NodeJS Instagram 私有 API SDK,TypeScript 编写)中帖子(Post)数据洞察(Insights)的核心响应结构 InsightsServicePostResponseActions。该接口承载 Instagram 商业账号帖子的「主页动作」(Profile Actions)聚合数据,是 InsightsService.post() 返回对象中 profile_actions.actions 字段的类型定义。读完本文,你将掌握该接口的字段语义、它在完整洞察响应树中的嵌套位置、底层的 GraphQL 请求实现,以及如何在真实项目中获取并解读这一指标。

一、接口全景:InsightsServicePostResponseActions 是什么

InsightsServicePostResponseActions 定义于 src/responses/insights.service.post.response.ts,属于 InsightsServicePostResponseRootObject 响应体系中的叶子级聚合接口。它表示「浏览这条帖子之后,用户对主页产生的一系列动作」的汇总,例如从帖子点进主页、关注账号、点击主页链接等,是衡量一条帖子转化能力的关键数据。

完整的接口定义(对应文档 InsightsServicePostResponseActions.md)只有两个属性:

属性 类型 说明
value number 主页动作的总次数,即所有细分动作节点的数值累加口径(由 Instagram 端计算返回)
nodes InsightsServicePostResponseNodesItem[] 按动作类别拆分的明细节点数组,每个节点记录单一动作类型的名称与数值

从源码结构看,nodes 中每个元素 InsightsServicePostResponseNodesItem 包含三个字段:必填的 __typename(GraphQL 节点类型标识)、必填的 value(该动作的计数)、可选的 name(动作名称,用于标注节点含义):

export interface InsightsServicePostResponseNodesItem {
  __typename: string;
  value: number;
  name?: string;
}

典型的节点示例可能包括 FOLLOW(关注)、PROFILE_VIEW(主页浏览)、BIO_LINK_CLICK(主页链接点击)等动作类别——这一点与项目在 PostsInsightsFeedOptions.dataOrdering 中暴露的枚举值相互印证(见下文第四节)。

二、该接口在洞察响应树中的嵌套位置

InsightsServicePostResponseActions 并不是孤立存在的,它是整棵帖子洞察响应树的叶子节点。根据 src/responses/insights.service.post.response.ts 的 10 余个嵌套接口定义,完整的嵌套结构如下:

InsightsServicePostResponseRootObject
└── data ── InsightsServicePostResponseData
    └── media ── InsightsServicePostResponseMedia
        ├── id / creation_time / comment_count / like_count / save_count ...
        ├── inline_insights_node ── InsightsServicePostResponseInline_insights_node
        │   ├── state: string
        │   └── metrics ── InsightsServicePostResponseMetrics
        │       ├── impression_count: number
        │       ├── reach_count: number
        │       ├── owner_profile_views_count: number
        │       ├── owner_account_follows_count: number
        │       ├── share_count ── { tray, post }(tray 与 post 均含 value + nodes)
        │       ├── reach ── { value, follow_status.nodes }
        │       ├── impressions ── { value, surfaces.nodes }
        │       └── profile_actions ── InsightsServicePostResponseProfile_actions
        │           └── actions ── InsightsServicePostResponseActions ★
        │               ├── value: number
        │               └── nodes: InsightsServicePostResponseNodesItem[]
        └── hashtags_impressions ── { organic, hashtags }

即要读取主页动作数据,访问路径为:

response.data.media.inline_insights_node.metrics.profile_actions.actions.value
response.data.media.inline_insights_node.metrics.profile_actions.actions.nodes

InsightsServicePostResponseProfile_actions(定义于同一源码文件第 62-64 行)仅有一个 actions 字段,类型正是本篇文章的主角 InsightsServicePostResponseActions,相关文档见 InsightsServicePostResponseProfile_actions.md。

值得注意的是,share_count、reach、impressions 这几个指标采用了与 actions 完全一致的「value + nodes」双子结构(详见 InsightsServicePostResponseShare_count.md、InsightsServicePostResponseReach.md、InsightsServicePostResponseImpressions.md),这是 Instagram 洞察 GraphQL 响应的通用设计:一个总数值 value,配一组可展开的明细节点 nodes,便于前端既展示汇总又下钻到明细。

三、源码级实现:这条数据是怎么请求回来的

InsightsServicePostResponseRootObject 是 InsightsService.post() 方法的返回类型。看 src/services/insights.service.ts 的实现:

public post(mediaId: string): Promise<InsightsServicePostResponseRootObject> {
  return this.client.ads.graphQL<InsightsServicePostResponseRootObject>({
    surface: {
      name: 'post',
      friendlyName: 'IgInsightsPostInsightsQuery',
    },
    documentId: '2009845309144121',
    variables: {
      query_params: {
        access_token: '',
        id: mediaId,
      },
    },
  });
}

该方法通过 client.ads(即 AdsRepository)发起一次带 documentId 的 GraphQL 请求。请求本身由 src/repositories/ads.repository.ts 中的 graphQL<T> 方法完成:

public async graphQL<T extends { data: any }>(options: GraphQLRequestOptions): Promise<T> {
  const { body } = await this.client.request.send<T>(
    {
      url: '/api/v1/ads/graphql/',
      method: 'POST',
      qs: {
        locale: this.client.state.language,
        vc_policy: 'insights_policy',
        ...(options.surface.name ? { surface: options.surface.name } : {}),
      },
      form: {
        access_token: options.accessToken,
        fb_api_caller_class: 'RelayModern',
        fb_api_req_friendly_name: options.surface.friendlyName,
        doc_id: options.documentId,
        variables: JSON.stringify(options.variables),
      },
    },
    true,
  );
  return body;
}

从实现可以确认几个关键事实:

  • 请求地址为 /api/v1/ads/graphql/,携带 vc_policy: 'insights_policy',即该接口属于 Instagram 的商业洞察(Insights)专用通道;
  • 请求表单中的 doc_id: 2009845309144121 是 Instagram 端 IgInsightsPostInsightsQuery 查询的文档标识;surface=post 与 fb_api_req_friendly_name=IgInsightsPostInsightsQuery 共同标记查询类别;
  • options.surface、documentId、variables 的类型约束来自 src/types/graphql-request.options.ts,其中 InsightsSurface 联合类型为 'account' | 'post' | 'story',InsightsFriendlyName 中定义了 'IgInsightsPostInsightsQuery' 等取值。

InsightsService 本身继承自 Repository(构造函数注入 IgApiClient,见 docs/classes/services/InsightsService.md),并在 src/core/client.ts 中以 public insights = new InsightsService(this) 挂载到 IgApiClient 实例上,因此实际调用入口为 ig.insights.post(mediaId)。

四、实战:获取并解读帖子的主页动作数据

4.1 获取 mediaId

post() 方法接收的 mediaId 是字符串形式的帖子媒体 ID。通常有两个来源:

  1. 通过 ig.media.info(mediaId) 或各类 Feed(如 UserFeed、TimelineFeed)取得帖子对象,取其 media.pk 字段;
  2. 直接使用已知的 Instagram 媒体短码对应的数字 ID。

4.2 调用与读取示例

以下代码演示如何在完成登录后获取一条帖子的洞察数据,并打印 profile_actions 中的主页动作汇总与明细:

import { IgApiClient } from 'instagram-private-api';

async function main() {
  const ig = new IgApiClient();
  ig.state.generateDevice('你的设备种子');
  await ig.account.login('用户名', '密码');

  const mediaId = '帖子媒体数字ID';
  const insights = await ig.insights.post(mediaId);

  const media = insights.data.media;
  const actions = media.inline_insights_node.metrics.profile_actions.actions;

  console.log('主页动作总次数 value:', actions.value);
  for (const node of actions.nodes) {
    console.log(
      `动作类别: ${node.name ?? node.__typename},次数: ${node.value}`,
    );
  }

  // 同层级的其他画像指标也可以一并读取
  const metrics = media.inline_insights_node.metrics;
  console.log('曝光 impression_count:', metrics.impression_count);
  console.log('触达 reach_count:', metrics.reach_count);
  console.log('分享 share_count:', metrics.share_count.post.value);
}

main().catch((err) => console.error(err));

4.3 动作类别的可能取值

虽然 nodes 节点的 name 由 Instagram 端动态返回,但项目在 src/types/insights.options.ts 的 PostsInsightsFeedOptions.dataOrdering 中定义了一组与主页/内容动作强相关的排序枚举,可帮助理解可能出现的动作类型:

'CALL' | 'COMMENT_COUNT' | 'EMAIL' | 'ENGAGEMENT_COUNT' | 'FOLLOW'
| 'GET_DIRECTIONS' | 'IMPRESSION_COUNT' | 'LIKE_COUNT' | 'PROFILE_VIEW'
| 'REACH_COUNT' | 'SAVE_COUNT' | 'SHARE_COUNT' | 'TEXT' | 'BIO_LINK_CLICK'

其中 FOLLOW(新增关注)、PROFILE_VIEW(主页浏览)、BIO_LINK_CLICK(主页链接点击)等正是典型的「主页动作」,与 profile_actions 的语义一致。需要强调的是,InsightsServicePostResponseActions.nodes 的具体节点内容由 Instagram 服务端决定,开发者应以其返回的实际数据为准。

五、关联能力:PostsInsightsFeed 与账号级画像

除单条帖子的 InsightsService.post() 之外,项目还提供了帖子画像的分页 Feed:src/feeds/posts-insights.feed.ts 中的 PostsInsightsFeed。它通过 documentId: '1981884911894608'(IgInsightsPostGridSurfaceQuery)拉取当前账号的帖子列表画像,支持 timeframe(ONE_WEEK ~ TWO_YEARS)、dataOrdering(按上节枚举排序)、postType(ALL | IMAGE | VIDEO | CAROUSEL_V2 | SHOPPING)三个配置项,并实现标准 Feed 的 nextCursor / moreAvailable 分页语义(见 src/types/insights.options.ts 的 PostsInsightsFeedOptions)。

其响应结构 src/responses/posts-insights.feed.response.ts 中同样存在 metrics.profile_actions.actions 字段,只不过这里的 actions 直接是 edges 数组(PostsInsightsFeedResponseActions),而非「value + nodes」结构——这是 Feed 版与单帖版响应形态的一个差异点,阅读时需注意区分。

账号级的聚合画像则由 InsightsService.account()(对应 IgInsightsAccountInsightsWithTabsQuery,documentId 2552829571413315)提供,其中 account_actions_graph 等字段(见 src/responses/insights.service.account.response.ts)可用于对比账号整体的主页动作走势。

六、使用注意事项与前提

  • 账号前提:帖子洞察数据仅对商业账号(Business / Creator)开放。在项目中,AccountRepository 登录响应的用户对象带有 can_see_organic_insights、show_insights_terms 字段(见 src/responses/account.repository.login.response.ts),可据此先判断当前账号是否具备查看有机洞察(Organic Insights)的权限。
  • 请求前提:必须先在 IgApiClient 上完成登录并持有有效的 session 状态,AdsRepository.graphQL 依赖 client.request 携带的会话凭据,否则会触发 IgLoginRequiredError 等认证类错误。
  • 字段稳定性:nodes 中节点的 name 与 __typename 取值由 Instagram 服务端返回,属于弱类型 any[] 之外的语义数据,接入时应以实际响应为准并做好防御性取值。
  • 数据时效:洞察指标为 Instagram 端聚合结果,非实时计数,value 的统计口径由服务端维护,SDK 仅做透传。

七、小结

InsightsServicePostResponseActions 虽是一个仅含 value 与 nodes 两个字段的轻量接口,却是整棵帖子洞察响应树中「主页动作」指标的承载者。结合 src/services/insights.service.ts 与 src/repositories/ads.repository.ts 的源码,可以完整还原其从 ig.insights.post() → /api/v1/ads/graphql/ → doc_id: 2009845309144121 → metrics.profile_actions.actions 的全链路。理解该接口及其相邻的 Reach、Impressions、Share_count 结构,即可在此基础上构建帖子级转化分析、主页引流归因等业务逻辑。

登录后查看全文
instagram-private-api