Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable page size in campaigns endpoint #2472

Merged
merged 9 commits into from
Aug 5, 2024

Conversation

jorgemd24
Copy link
Contributor

@jorgemd24 jorgemd24 commented Jul 16, 2024

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

@github-actions github-actions bot added the changelog: update Big changes to something that wasn't broken. label Jul 16, 2024
@jorgemd24 jorgemd24 self-assigned this Jul 16, 2024
Copy link

codecov bot commented Jul 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

Additional details and impacted files

Impacted file tree graph

@@             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 Coverage Δ
js-unit-tests ?
php-unit-tests 64.7% <100.0%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/API/Google/AdsCampaign.php 97.3% <100.0%> (ø)
src/API/Google/Query/AdsCampaignQuery.php 100.0% <100.0%> (ø)
src/API/Google/Query/AdsQuery.php 87.0% <100.0%> (ø)
...rc/API/Site/Controllers/Ads/CampaignController.php 100.0% <100.0%> (ø)

... and 776 files with indirect coverage changes

@jorgemd24 jorgemd24 changed the title Update/enable page size in campaigns endpoint Enable page size in campaigns endpoint Jul 16, 2024
@jorgemd24 jorgemd24 requested a review from a team July 16, 2024 19:39
@jorgemd24 jorgemd24 marked this pull request as ready for review July 16, 2024 19:39
* @param bool $fetch_criterion Combine the campaign data with criterion data (default true).
* @param bool $exclude_removed Exclude removed campaigns (default true).
* @param bool $fetch_criterion Combine the campaign data with criterion data (default true).
* @param array $args Additional arguments.
Copy link
Contributor

Choose a reason for hiding this comment

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

WDYT of being more concrete here?

Maybe

@param array $args Arguments for the Ads Campaign Query.

@@ -152,6 +152,67 @@ public function test_get_campaigns_converted_names() {
$this->assertEquals( 200, $response->get_status() );
}

public function test_get_campaigns_with_args() {
Copy link
Contributor

Choose a reason for hiding this comment

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

In the test added here where are we testing another page_size? For example page_size=1 to see if we don't get all the campaigns and just 1

Copy link
Contributor

@puntope puntope left a comment

Choose a reason for hiding this comment

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

Thanks for the changes @jorgemd24

I left some minor comments but the per_page works as expected.

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

@jorgemd24
Copy link
Contributor Author

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: #1887 (comment). 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:

  • X-WP-Total -> Total number across all pages.
  • X-WP-TotalPages -> Total pages.
  • X-GLA-NextPageToken -> The token to get the next page. (I used the GLA prefix to avoid future conflicts with this header name)

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.

*
* @return array
* @throws ExceptionWithResponseData When an ApiException is caught.
*/
public function get_campaigns( bool $exclude_removed = true, bool $fetch_criterion = true ): array {
public function get_campaigns( bool $exclude_removed = true, bool $fetch_criterion = true, $args = [], $return_pagination_params = false ): array {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the value of NOT returning pagination params? Even if they are not needed I assume they are not harmful. So maybe we can simplify this method by returning them always.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@puntope, the reason for not always returning the pagination params is that we would need to refactor other parts of the code and tests that depend on this method:

Do you think it’s worth refactoring those parts even if they don't require pagination?

Copy link
Contributor

@puntope puntope left a comment

Choose a reason for hiding this comment

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

Thx. @jorgemd24 I think we can go forward with this and evaluate the refactoring you mentioned

@jorgemd24 jorgemd24 merged commit 603211e into develop Aug 5, 2024
12 checks passed
@jorgemd24 jorgemd24 deleted the update/enable-page-size-in-campaigns-endpoint branch August 5, 2024 20:47
@ianlin ianlin mentioned this pull request Aug 6, 2024
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: update Big changes to something that wasn't broken.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants