google-listings-and-ads icon indicating copy to clipboard operation
google-listings-and-ads copied to clipboard

Add endpoint for getting price benchmarks summary data

Open joemcgill opened this issue 8 months ago • 1 comments

User story

Part of https://github.com/woocommerce/google-listings-and-ads/issues/2824

In order to display price benchmark competitiveness summary described in #2827, we need a REST API endpoint that can be used to fetch the summary data across all the eligible products.

Acceptance criteria

  • [ ] Price benchmark summary data can be fetched from /wp-json/wc/gla/mc/price-benchmarks/summary
  • [ ] GET requests to this endpoint are not publicly accessible
  • [ ] The response for authenticated requests returns the following data:
    • total products: (int) total number of products represented in the Google report
    • price similar: (int) total number of products with similar prices to benchmark data
    • price higher: (int) total number of products with higher prices to benchmark data
    • price lower: (int) total number of products with lower prices to benchmark data
    • price unknown: (int) total number of products without price benchmark data
  • [ ] Error handling must be included and return an exception response

Implementation brief

  • The REST API endpoint should be implemented in the same PriceBenchmarks controller that is defined in #2825, with a new rest field for 'mc/price-benchmarks/summary'
  • The REST endpoint should use the BaseController::get_permission_callback() method to ensure the endpoint is not public
  • A new get_price_benchmarks_summary_callback() callback should use the [TBD service class] from the container to query for summary data.
  • [TBD] To query for benchmark data, we may be able to reuse or extend the MerchantPriceBenchmarksQuery described in #2825. See these docs.

Out of bounds/rabbit holes

  • Price benchmark data should only be expected for products with GTINs that have been approved in MC.

Test coverage

  • PHPUnit test coverage should be in place for the API callback function that mocks query data from Google APIs

joemcgill avatar Mar 28 '25 20:03 joemcgill

As far as I can see to get this summary it means getting the total result set from PriceCompetitivenessProductView, possibly multiple pages of data. If that's going to be a bottle neck (or cause timeouts) for larger sites with a lot of products, then we might need to consider moving it to a background fetch.

We can see an example of how we do this for the Product Statuses which populate this view:

Image

The statuses are fetched in a background job, in this case the status is saved as product meta and the issues with the products get pulled into a separate table wp_gla_merchant_issues. Only once we have that data locally do we show the summary, as to pull it directly from the MC API in one request is too resource intensive.

So here we might just be better off syncing the full result set into a DB table and then we can fetch both a summary and paginated results directly from the local table.

mikkamp avatar Apr 01 '25 07:04 mikkamp

@mikkamp this is one of the things that I wanted to try to confirm with @fblascogarma, to see if there was a way to query this aggregate data without the need for manually getting data for all the products individually and compiling the summary in the plugin. He confirmed that the API won't calculate the percentages, so I know that part will need to be calculated in the plugin, but need to do some more investigation here.

However, if we do end up fetching this data as a daily background job and storing it locally, we could also use the same data to drive the responses for #2825, which could be better. We'll just need to confirm how often we should refresh the list.

joemcgill avatar Apr 01 '25 15:04 joemcgill

I've confirmed that we'll need to fetch all of the benchmarks data to the local DB in order to drive this endpoint. I've updated the implementation details with the approach that we'll need to take to set up the architecture to fetch this data in a background task and store it locally.

joemcgill avatar Apr 10 '25 15:04 joemcgill

@joemcgill Since we’ve mocked the price benchmarks in https://github.com/woocommerce/google-listings-and-ads/blob/feature/2824-price-benchmarks/tests/Unit/API/Site/Controllers/MerchantCenter/PriceBenchmarksControllerTest.php#L64-L81, how should we use that mocked data when working with the custom table?

Specifically, I’m wondering about the correct way to map the mocked benchmark data into the custom table structure. Could you please guide me on that?

mukeshpanchal27 avatar Apr 17 '25 11:04 mukeshpanchal27

@mukeshpanchal27

Specifically, I’m wondering about the correct way to map the mocked benchmark data into the custom table structure. Could you please guide me on that?

I've left some ideas on the PR: https://github.com/woocommerce/google-listings-and-ads/pull/2857#pullrequestreview-2776834528

joemcgill avatar Apr 17 '25 20:04 joemcgill