-
Notifications
You must be signed in to change notification settings - Fork 908
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
416 implement organic session backend endpoint using the google analytics api #22023
416 implement organic session backend endpoint using the google analytics api #22023
Conversation
…r the Organic Session widget
A merge conflict has been detected for the proposed code changes in this PR. Please resolve the conflict by either rebasing the PR or merging in changes from the base branch. |
Pull Request Test Coverage Report for Build 5e5b5cce9aa343cb367faeb387e1405c684f37a0Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CR 🚧 some questions.
Context
Summary
This PR can be summarized in the following changelog entry:
Relevant technical choices:
dimensionValues[0]->value
of the rows of a request that asked for daily data is in fact a day. To make sure of that, we check if our request haddate
as the only dimension, because otherwise our parsing of responses will break. This means that@TODO
for properly accessing the properties of the responses and created a task to take care of that. The task also describes the same for the Search Console responses that we have already implemented.order_by
Analytics parameter: I have constructed its format based on how Site Kit does it and also how Google docs say. I've even compared a request coming from us with a request coming from Site Kit and it results in the sameorderBy
format reaching external Google APIs. HOWEVER, I couldn't see it working properly with other arguments than the current one, but I might be missing something. I'm mentioning this for the future, if we ever need to order in a different way. For now, the order of the response is how we want it.Test instructions
Test instructions for the acceptance test before the PR gets merged
This PR can be acceptance tested by following these steps:
Note: Wherever it's mentioned that you have to cross-reference the results you got with the ones you see in the Google Analytics/Search Console platforms, you can check the
Google Search Console & Analytics tutorial Feb 11 2025
tutorial we have in our Google Drive.To test the new endpoints:
/wp-json/yoast/v1/time_based_seo_metrics?options[widget]=οrganicSessionsDaily
Make sure that the date range are the last 28 days without including today's date (so, the above example is for a request that happened on the 14/2/2025
Go to the actual Google Analytics platform and search for organic sessions of the last 28 days and confirm that they data you got are the same with the data you see in that platform.
One easy way to check that is to go to the Traffic Acquisition page of the analytics and click on the Organic sessions on the graph:

For getting the complete data about the last 28 days and the complete data of the 28 days before that, do a GET request to
/wp-json/yoast/v1/time_based_seo_metrics?options[widget]=οrganicSessionsCompare
Confirm that the response you get is like:
Acquisition
page in analytics and then enable Compare and selectPreceding period
:To test that Search Console results are actually corrected (prompted by the fix of date ranges for older implementations):
Note: Sometimes I see Search Console treating
last 28 days
as the 28 days BEFORE YESTERDAY for some reason, where we treat it as the 28 days before today, which seems more correct. If you see differences in the test below or in future tests, try to move the date range in Search Console a day forward./wp-json/yoast/v1/time_based_seo_metrics?options[widget]=page
and/wp-json/yoast/v1/time_based_seo_metrics?options[widget]=query
respectively) or actually look at the widgets if they have been implemented in the frontendTo test requesting multiple metrics:
$request_parameters->set_metrics( [ 'sessions' ] );
lines to$request_parameters->set_metrics( [ 'sessions', 'totalUsers' ] );
in both lines.and
Check both results similarly to how you check the sessions above. This time, for users you can look into the User acquisition tab:

Because total users will probably be the same number with sessions, you can also request for a different metric, like Event Counts.
$request_parameters->set_metrics( [ 'sessions' ] );
line to$request_parameters->set_metrics( [ 'sessions', 'eventCount' ] );
elseif ( $metric->name === 'totalUsers' ) {
toelseif ( $metric->name === 'eventCount' ) {
You can check again your response to look like this:
and
This time, the
total_users
refer to the Event Count metric. You should confirm those results with Analytics ⬇️For the first request, I was able to find the relevant graph by searching for

event counts of organic search
in the Analytics search. After going there, you should check the graph points with thetotal_users
points of the first response.For the second request, you can see the Event Count column in the Traffic Acquisition tab (after you have added a comparison date range like described in previous steps):

To test doing an Analytics request while not authorized:
/wp-json/yoast/v1/time_based_seo_metrics?options[widget]=οrganicSessionsDaily
or/wp-json/yoast/v1/time_based_seo_metrics?options[widget]=οrganicSessionsCompare
request as a user that has no viewing rights of Analytics"error": "The Analytics 4 request failed: Site Kit can’t access the relevant data from Analytics because you haven’t granted all permissions requested during setup."
body.To test asking for unexpected requests:
$request_parameters->set_dimensions( [ 'date' ] );
to$request_parameters->set_dimensions( [ 'platform' ] );
and$request_parameters->set_order_by( 'dimension', 'date' );
to$request_parameters->set_order_by( 'dimension', 'platform' );
οrganicSessionsDaily
request and confirm you get a 400 response with a"error": "The Analytics 4 request is invalid: Unexpected parameters for the request"
bodyRelevant test scenarios
Test instructions for QA when the code is in the RC
Impact check
This PR affects the following parts of the plugin, which may require extra testing:
UI changes
Other environments
[shopify-seo]
, added test instructions for Shopify and attached theShopify
label to this PR.Documentation
Quality assurance
Innovation
innovation
label.Fixes https://github.com/Yoast/reserved-tasks/issues/416