Skip to content

fix(facebook): video/reel analytics via video_insights (no more nonexisting-field crash)#1669

Open
giladresisi wants to merge 1 commit into
mainfrom
fix/facebook-video-insights
Open

fix(facebook): video/reel analytics via video_insights (no more nonexisting-field crash)#1669
giladresisi wants to merge 1 commit into
mainfrom
fix/facebook-video-insights

Conversation

@giladresisi

Copy link
Copy Markdown
Collaborator

Problem

FacebookProvider.postAnalytics always called /{postId}/insights, but that edge only exists on page feed posts. Video/reel and story posts store a bare id whose node has no insights edge, so Facebook returned:

(#100) Tried accessing nonexisting field (insights)  [OAuthException, code 100]

surfaced in prod logs as Error fetching Facebook post analytics: ApplicationFailure: Unknown Error. One bad post could blank the statistics page.

Fix

postAnalytics now branches on the stored releaseId shape (the only discriminator available — no post type is persisted for analytics to read):

Post type Stored id shape Endpoint Analytics returned
Feed post {pageid}_{postid} (has _) /{postId}/insights (unchanged) Impressions, Clicks, Clicks by Type, Reactions
Video / reel bare numeric id /{videoId}/video_insights (new) Impressions (total_video_impressions), Views (total_video_views), Reactions (total_video_reactions_by_type_total)
Story bare id, no usable insights Skipped → clean []
  • Metric names verified against the current Graph API v23.0 video_insights docs (video metrics churn — Meta deprecated post_impressions_unique on 2026-06-15).
  • The video call uses plain fetch (not this.fetch) so a (#100) nonexisting field (video_insights) / empty response yields a quiet [] instead of throwing an ApplicationFailure.
  • Error handling: a nonexisting field error stays silent (expected for stories); any other error (bad metric name, token, permissions) is logged via console.warn so a silently-empty result is diagnosable — without ever throwing to the caller.

Backward compatibility

  • Feed-post analytics are byte-for-byte unchanged — same endpoint, metrics, and mapping. Only new branches were added.
  • Works off already-stored releaseIds — no migration.
  • postAnalytics still returns AnalyticsData[] and never throws.

Testing

Verified locally against a freshly connected Facebook page (read_insights scope):

  1. Published a video post through Postiz; confirmed its releaseId is a bare id (took the new video branch).
  2. Probed the node directly — it's a real video node (length: 4.16).
  3. /{videoId}/video_insights with the three metrics returned {"data":[]} with no error:
    • No (#100) nonexisting field (insights) → the original crash is gone.
    • No (#100) ... must be one of ... → all three v23.0 metric names are valid.
    • Empty data = Facebook has no insights computed yet for a seconds-old clip → app returns a clean [] (the intended no-data-yet case), and no console.warn fired.
  4. facebook.provider.ts typechecks clean (tsc --noEmit).

🤖 Generated with Claude Code

… missing insights edge

postAnalytics always called /{postId}/insights, but that edge only exists on
page feed posts. Video/reel and story posts store a bare id whose node has no
insights edge, so Facebook returned "(#100) Tried accessing nonexisting field
(insights)" — surfaced in prod as "Error fetching Facebook post analytics:
ApplicationFailure: Unknown Error".

postAnalytics now branches on the stored releaseId shape (the only available
discriminator, since no post type is persisted for analytics):

- Feed post ({pageid}_{postid}, contains "_") -> unchanged: /{postId}/insights
  with the existing metrics/mapping. No regression.
- Video/reel (bare numeric id) -> /{videoId}/video_insights on v23.0, mapped to
  AnalyticsData: total_video_impressions -> Impressions, total_video_views ->
  Views, total_video_reactions_by_type_total -> Reactions (metric names verified
  against the current v23.0 docs).
- Story (bare id, no usable insights via this path) -> clean [], no error.

The video call uses plain fetch (not this.fetch) so a "(#100) nonexisting field
(video_insights)" / empty response yields a quiet [] instead of throwing. A
"nonexisting field" error stays silent (expected for stories); any other error
(bad metric name, token, permissions) is logged via console.warn so a silent
empty result is diagnosable without breaking the statistics page. postAnalytics
still never throws.

Tested locally against a freshly connected Facebook page: published a video post,
confirmed its releaseId resolves to a real video node, and that
/{videoId}/video_insights returns {"data":[]} (no data yet) with NO
"(#100) nonexisting field" or "must be one of" error — i.e. the old crash is gone,
the metric names are valid, and the app returns a clean [] for the no-data-yet case.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@postiz-agent

postiz-agent Bot commented Jul 2, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

Comment on lines +961 to +965
const totalReactions = Object.values(
value as Record<string, number>
).reduce((sum: number, v: number) => sum + v, 0);
label = 'Reactions';
total = String(totalReactions);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The check typeof value === 'object' does not exclude null, causing Object.values(null) to throw and discard all video analytics if the API returns null.
Severity: MEDIUM

Suggested Fix

Update the conditional check to explicitly exclude null before checking the type. The guard should be if (value !== null && typeof value === 'object').

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location:
libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts#L961-L965

Potential issue: In the `videoPostAnalytics` function, when processing the
`total_video_reactions_by_type_total` metric, the code checks `typeof value ===
'object'`. In JavaScript, `typeof null` evaluates to `'object'`, so if the Facebook API
returns `null` for this metric, the condition passes. The code then attempts to execute
`Object.values(null)`, which throws a `TypeError`. This error is caught by a broader
`catch` block that returns an empty array, causing all analytics for the video post
(including impressions and views) to be discarded, not just the reaction data.

Did we get this right? 👍 / 👎 to inform future reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant