woocommerce / google-listings-and-ads

Sync your store with Google to list products for free, run paid ads, and track performance straight from your store dashboard.
https://woo.com/products/google-listings-and-ads/
GNU General Public License v3.0
44 stars 21 forks source link

Enable page size in campaigns endpoint #2472

Closed jorgemd24 closed 2 weeks ago

jorgemd24 commented 1 month ago

Changes proposed in this Pull Request:

Part of pcTzPl-2nS-p2

This PR allows setting the per_page parameter in the gla/ads/campaigns endpoint. Previously, we returned all the results, but now it is possible to limit the number of results.

Screenshots:

Detailed test instructions:

  1. Make a request to GET gla/ads/campaigns?per_page=X and check that the number of results matches the per_page parameter.
  2. Make a request to GET gla/ads/campaigns and ensure that you get all the results.

Additional details:

I had to replace iterateAllElements() with getPage()->getIterator() to avoid iterating through all the pages and retrieving all the results at once.

Changelog entry

Update - Enable Page Size Parameter in Campaigns Endpoint

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 64.7%. Comparing base (5069f65) to head (6ff5916).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472/graphs/tree.svg?width=650&height=150&src=pr&token=UROWUPF1LX&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce)](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce) ```diff @@ Coverage Diff @@ ## develop #2472 +/- ## ============================================ + Coverage 63.4% 64.7% +1.3% - Complexity 0 4286 +4286 ============================================ Files 321 459 +138 Lines 5027 16915 +11888 Branches 1219 0 -1219 ============================================ + Hits 3188 10941 +7753 - Misses 1672 5974 +4302 + Partials 167 0 -167 ``` | [Flag](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce) | Coverage Δ | | |---|---|---| | [js-unit-tests](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce) | `?` | | | [php-unit-tests](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce) | `64.7% <100.0%> (?)` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce#carryforward-flags-in-the-pull-request-comment) to find out more. | [Files](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce) | Coverage Δ | | |---|---|---| | [src/API/Google/AdsCampaign.php](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472?src=pr&el=tree&filepath=src%2FAPI%2FGoogle%2FAdsCampaign.php&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce#diff-c3JjL0FQSS9Hb29nbGUvQWRzQ2FtcGFpZ24ucGhw) | `97.3% <100.0%> (ø)` | | | [src/API/Google/Query/AdsCampaignQuery.php](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472?src=pr&el=tree&filepath=src%2FAPI%2FGoogle%2FQuery%2FAdsCampaignQuery.php&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce#diff-c3JjL0FQSS9Hb29nbGUvUXVlcnkvQWRzQ2FtcGFpZ25RdWVyeS5waHA=) | `100.0% <100.0%> (ø)` | | | [src/API/Google/Query/AdsQuery.php](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472?src=pr&el=tree&filepath=src%2FAPI%2FGoogle%2FQuery%2FAdsQuery.php&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce#diff-c3JjL0FQSS9Hb29nbGUvUXVlcnkvQWRzUXVlcnkucGhw) | `87.0% <100.0%> (ø)` | | | [...rc/API/Site/Controllers/Ads/CampaignController.php](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472?src=pr&el=tree&filepath=src%2FAPI%2FSite%2FControllers%2FAds%2FCampaignController.php&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce#diff-c3JjL0FQSS9TaXRlL0NvbnRyb2xsZXJzL0Fkcy9DYW1wYWlnbkNvbnRyb2xsZXIucGhw) | `100.0% <100.0%> (ø)` | | ... and [776 files with indirect coverage changes](https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2472/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=woocommerce)
jorgemd24 commented 1 month ago

Thanks @puntope for reviewing this PR! I addressed your suggestions.

One thing I miss is the page parameter to get the results for a specific page. Not sure if its for an other PR.

Initially, I planned not to set the next page token in the endpoint response for the reasons mentioned in this comment: https://github.com/woocommerce/google-listings-and-ads/issues/1887#issuecomment-2231712265. However, after @mikkamp's comment, I realized it is possible to get the total number of results in the first query. Therefore, I implemented the following headers:

With this, we can fetch the next page and store the page token in the client to navigate backwards. However, it seems that it is not possible to get the forward page tokens until you access the page, (for example jumping from page 1 to page 5) so we need to see if it is feasible to implement this in the front end and the back end.

I found this example that can be useful: https://developers.google.com/google-ads/api/samples/navigate-search-result-pages-caching-tokens but for now, I will leave that implementation/investigation for a different PR as the mobile team is interested in limiting the result to 1 to check if the merchant is active and I think this PR will help us to move forward to implement the pagination in the campaign table.