fix(facebook): video/reel analytics via video_insights (no more nonexisting-field crash)#1669
Open
giladresisi wants to merge 1 commit into
Open
fix(facebook): video/reel analytics via video_insights (no more nonexisting-field crash)#1669giladresisi wants to merge 1 commit into
giladresisi wants to merge 1 commit into
Conversation
… 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>
✅ Snyk checks have passed. No issues have been found so far.
💻 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); |
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
FacebookProvider.postAnalyticsalways called/{postId}/insights, but that edge only exists on page feed posts. Video/reel and story posts store a bare id whose node has noinsightsedge, so Facebook returned:surfaced in prod logs as
Error fetching Facebook post analytics: ApplicationFailure: Unknown Error. One bad post could blank the statistics page.Fix
postAnalyticsnow branches on the storedreleaseIdshape (the only discriminator available — no post type is persisted for analytics to read):{pageid}_{postid}(has_)/{postId}/insights(unchanged)/{videoId}/video_insights(new)total_video_impressions), Views (total_video_views), Reactions (total_video_reactions_by_type_total)[]video_insightsdocs (video metrics churn — Meta deprecatedpost_impressions_uniqueon 2026-06-15).fetch(notthis.fetch) so a(#100) nonexisting field (video_insights)/ empty response yields a quiet[]instead of throwing anApplicationFailure.nonexisting fielderror stays silent (expected for stories); any other error (bad metric name, token, permissions) is logged viaconsole.warnso a silently-empty result is diagnosable — without ever throwing to the caller.Backward compatibility
releaseIds — no migration.postAnalyticsstill returnsAnalyticsData[]and never throws.Testing
Verified locally against a freshly connected Facebook page (
read_insightsscope):releaseIdis a bare id (took the new video branch).length: 4.16)./{videoId}/video_insightswith the three metrics returned{"data":[]}with no error:(#100) nonexisting field (insights)→ the original crash is gone.(#100) ... must be one of ...→ all three v23.0 metric names are valid.data= Facebook has no insights computed yet for a seconds-old clip → app returns a clean[](the intended no-data-yet case), and noconsole.warnfired.facebook.provider.tstypechecks clean (tsc --noEmit).🤖 Generated with Claude Code