Campaign metrics are returned for at most one post per stats request

Open Beginner friendly
#4,854 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php, wordpress
Domain
analytics, backend

Research direction

Start in src/rest-api/stats/class-endpoint-posts.php at Endpoint_Posts::fetch_campaign_data() around line 507, then read the posts stats endpoint flow and the campaign response matching logic. Reproduce with the documented two-post request and a mocked response that returns the second post first. Done means each post receives its matching campaign metrics without undefined-key or null-offset warnings.

Written by the indexing model from the issue text.

Description

Bug Feature: Engagement Boost wp-parsely

Describe the bug

Endpoint_Posts::fetch_campaign_data() finds each post's campaign data with array_filter(), which keeps the original keys, and then reads the match as $campaign_post[0] (src/rest-api/stats/class-endpoint-posts.php:507). That only works for the post whose data is the first item of the campaign response, so at most one post per request gets its campaign metrics.

For every other post, $campaign_post[0] is undefined, all four campaign metrics become null, and GET /wp-parsely/v2/stats/posts returns "campaign": [] for it. The Engagement Boost dashboard reads campaign.views for its "+N" smart-link views and its "BOOSTED" percentage (src/content-helper/dashboard-page/components/posts-table/components/single-post-row.tsx:106), so neither appears for those posts.

To Reproduce

  1. Request GET /wp-parsely/v2/stats/posts?campaign_id=wp-parsely&campaign_medium=smart-link&period_start=30d for two posts that both have smart-link views, as the Engagement Boost dashboard does.
  2. Have the campaign request return the second post first. Mocking the API with pre_http_request makes this deterministic.

The post whose campaign data comes second gets "campaign": []. PHP raises Undefined array key 0 at class-endpoint-posts.php:507, followed by eight Trying to access array offset on null warnings at :510–:513.

Expected behavior

Each post gets the campaign metrics that match its link, wherever they appear in the campaign response.

Environment

wp-parsely 3.26.1 and current develop, PHP 8.3. PHP 7.4 raises notices instead of warnings.

Additional context

Possible fixes: take the match with reset(), or index the campaign response by link once before the loop, which also avoids filtering the whole response for every post.

Dominant language
PHP
Stars
67
Forks
38
Avg merge
1d 5h
Merged PRs (30d)
79

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Parsely/wp-parsely

All issues in Parsely/wp-parsely

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.