fix(pinterest): keep analytics within the 90-day window#1726
Conversation
Pinterest's analytics endpoints only serve the last 90 days (evaluated in UTC). postAnalytics requested a 2-year start_date, so Pinterest rejected the call every time and pin analytics were always empty. Clamp postAnalytics to 89 days back and cap the account-level analytics period at 89 days (89 avoids an off-by-one when the server's local date leads UTC). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Contribution-checker quality warning Heuristics that flagged:
If this is a genuine contribution, please add detail to your PR description and tighten the diff scope before reviewers look at it. |
✅ 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. |
| // Pinterest only serves pin analytics for the last 90 days (89 for a UTC safety margin) | ||
| const since = dayjs().subtract(89, 'day').format('YYYY-MM-DD'); |
There was a problem hiding this comment.
Bug: The postAnalytics method for Pinterest hardcodes the analytics date range to 89 days, ignoring the user-selected date parameter from the frontend.
Severity: MEDIUM
Suggested Fix
Modify the postAnalytics method to respect the date parameter when calculating the since date. The implementation should be similar to the analytics method, using dayjs().subtract(Math.min(date, 89), 'day') to ensure the user's selection is used, capped at the 89-day limit imposed by the API.
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/pinterest.provider.ts#L462-L463
Potential issue: The `postAnalytics()` method in the Pinterest provider consistently
ignores the `date` parameter passed from the frontend, instead hardcoding the analytics
window to 89 days. When a user requests analytics for a shorter period, such as 7 or 30
days, the API call still fetches data for the last 89 days. This is inconsistent with
the account-level `analytics()` method, which correctly respects the user's date
selection. As a result, the UI will display pin-level analytics aggregated over an
89-day period, regardless of the user's chosen date range, leading to incorrect data
representation.
Did we get this right? 👍 / 👎 to inform future reviews.
There was a problem hiding this comment.
This method only reads lifetime_metrics, which Pinterest computes over the pin's whole lifetime regardless of start_date/end_date - so clamping to the user's date would return identical numbers. The window just has to satisfy the API's 90-day validity limit, hence the fixed 89 days (also keeps the request valid for unvalidated date values from the public API). The range-respecting view is the account-level analytics().
Problem
Production logs show, for every Pinterest post:
with Pinterest's underlying message: "You can only get data from the last 90 days."
Pinterest's analytics endpoints (
/v5/pins/{pin_id}/analytics,/v5/user_account/analytics) only serve the last 90 days (evaluated in UTC). ButpostAnalytics()requestedstart_date2 years ago, so the call failed every time and pin analytics were always empty. Introduced in5cca81e0("feat: post analytics").Changes
postAnalytics—start_dateis now 89 days back instead of 2 years (89 rather than 90 avoids an off-by-one when the server's local date leads UTC).analytics(account level) — clamps the requested period toMath.min(date, 89)so a longer UI selection can't trip the same error.Replaces #1665: per review there, analytics shouldn't be coupled to
this.fetch/handleErrors, so thehandleErrorsmapping is dropped — #1725 decouples analytics fromthis.fetchacross all providers.Pin metrics older than ~90 days simply aren't available from Pinterest's API; older posts return only the recent-window data.
🤖 Generated with Claude Code