Add Site Health section
Feature Description
Add the new “Analytics 4 site created audiences” section to Site Health.
See Site Health in the design doc.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- When the
audienceSegmentationfeature flag is enabled, the Site Health section for Site Kit should contain a new section. - The title for the section should be: Analytics site created audiences.
- The value of the section should a list of the audiences that Site Kit has created, i.e. the "new visitors" and "returning visitors" audiences, if they exist.
- The names of these audiences should be displayed as they are in Analytics (i.e. their display names should be presented in the list).
Implementation Brief
- [ ] In
includes/Modules/Analytics_4.php, inside theget_debug_fieldsmethod:- [ ] Check if the
audienceSegmentationfeature flag is enabled. - [ ] If it is enabled, add a new entry in
$debug_fieldswith following:- [ ]
labelasAnalytics site created audiences. - [ ]
valueas comma separated string, containing thedisplayNamefields from the audiences stored in theavailableAudiencessetting whoseaudienceTypeis'SITE_KIT_AUDIENCE'. Feel free to add an additional method to retrieve these, or it can be inlined. - [ ]
debugshould be the same value.
- [ ]
- [ ] An example of adding a debug field conditionally can be seen here.
- [ ] Check if the
Test Coverage
- In
tests/phpunit/integration/Modules/Analytics_4Test.phpfix thetest_get_debug_fieldsstest. - Fix any other failing tests.
QA Brief
-
Ensure that you have access to analytics 4 property where you can create the audiences. This will require write permission. Connect such a property in Google analytics in site kit.
-
Run the following command in browser console.
const audience = {
displayName: 'SiteKit Audience',
description: 'This is the test audiences.',
membershipDurationDays: 7,
filterClauses: [
{
"clauseType": "INCLUDE",
"simpleFilter": {
"scope": "AUDIENCE_FILTER_SCOPE_ACROSS_ALL_SESSIONS",
"filterExpression": {
"andGroup": {
"filterExpressions": [
{
"orGroup": {
"filterExpressions": [
{
"dimensionOrMetricFilter": {
"atAnyPointInTime": null,
"fieldName": "newVsReturning",
"inAnyNDayPeriod": null,
"stringFilter": {
"caseSensitive": null,
"matchType": "EXACT",
"value": "returning"
}
}
}
]
}
},
{
"orGroup": {
"filterExpressions": [
{
"notExpression": {
"dimensionOrMetricFilter": {
"fieldName": "groupId",
"stringFilter": {
"matchType": "EXACT",
"value": "created_by_googlesitekit:returning_visitors"
}
}
}
}
]
}
}
]
}
}
}
}
],
};
const res = googlesitekit.data.dispatch('modules/analytics-4').createAudience(audience);
console.log(res);
This will trigger a request to modules/analytics-4/data/create-audience in the browser which should create a new audience SiteKit Audience in google analytics-4.
- Once above request executed successfully with 200 response code, run following command in browser console.
googlesitekit.data.dispatch('modules/analytics-4').syncAvailableAudiences();
It will sync audiences from analytics to site kit.
- Go to
Tools > Site Health > Info > Site kit by Google, you must see the sectionAnalytics site created audienceswith valueSiteKit Audience.
Changelog entry
Hi @ankitrox, thanks for drafting this IB. However, the approach you've suggested is not really necessary - we can add to the Analytics_4::get_debug_fields() method instead, where the other Analytics related Site Health fields are defined.
For example, see the "available custom dimensions" entry:
https://github.com/google/site-kit-wp/blob/e023adfaabad7ba4ebc2fb46a761e87cd05b8021/includes/Modules/Analytics_4.php#L428-L440
Thanks @ankitrox, this is looking better, one thing though - we try to avoid including literal code blocks in IBs. I would say this IB can be written without the code block, please can you remove/rewrite that aspect? You can simply say to take the existing custom dimension entry as a reference, for example and permalink to the entry.
Thank you @techanvil for reviewing the IB.
I agree that adding the literal code block is not good idea as the execution should be at the engineer's discretion.
As per your suggestion, I have removed the code block and added the list wide pointers for the implementation.
Assigning it to you for the review again.
Thank you.
Thanks @ankitrox!
I've made some additional changes to the IB. Notably I've removed the explicit definition for the new method and left this optional, and also removed the point about calling redact_debug_value() for the debug field, this is not needed here as the names are not personal data, rather they are standard names that all Site Kit users may see. I've also made a few minor grammatical tweaks.
Please review the changes and if you are happy feel free to move this to the EB.
IB :white_check_mark:
Looking everything good. Thanks @techanvil
Moving this to EB.
Hi @ankitrox, as well as addressing the feedback left on the PR, please can you update the QAB for this issue as follows:
- Rather than providing an audience configuration for "SiteKit Audience", provide instructions for creating audiences with the configuration for the "new visitors" and "returning visitors" audiences. This will be more representative of the real world expectations, and also allow testing the comma separated list rather than a single audience.
- Ensure that names are correctly formatted with uppercase initials. In this case "Analytics" (referring to Google Analytics) and "Site Kit" are lowercased where they should be name-cased.
Hi @techanvil ,
Thank you for the review.
I have addressed your feedback on the PR and also updated QAB so that we create New visitors and Returning visitors audiences in Google Analytics.
Assigning this to you for the re-review.
Thank you.
Thanks @ankitrox! I've left a few further comments on the PR, please take a look.
I've also tweaked the code example in the QAB slightly to follow our usual naming conventions (not essential, but might as well!), and to be slightly more verbose - we don't need to aim for DRY code in QABs, it's generally better if it's slightly more straightforward.
Thank you @techanvil for reviewing the PR.
I have addressed the comments in the PR and QAB code is looking good.
Assigning this back to you for review.
Thanks.
Thanks @ankitrox! The PR needs one last update and this should be good to go.
Thank you @techanvil for reviewing the PR.
I've made the suggested change and assigning this to you for the review.
Thanks.
QA Update ❌
Hi @ankitrox , tested this and I was able to do all the steps but in the end, there was not section with the values 'New visitors' and 'Returning visitors' in the Tools > Info section. The details are below:
-
I created the 2 audiences successfully.
All requests were with 200 status:
-
I synced the audiences accordingly with status 200:
-
However, I could not see the expected values in the Tools > Site Health > Info > Site Kit by Google section
Assigning ticket to you for further check.
QA Update ✅
After reviewing together with @ankitrox , it seems like my branch was not properly connected to Develop. I re-set it to 'Develop' and it worked fine then.
- All the scripts are successfully executed
- Going to Tools > Site Health > Info > Site Kit by Google, I can see the section Analytics site created audiences with value New visitors, Returning visitors.
Moving this ticket to Approval
Audiences created successfully:
Values appear at Tools > Site Health > Info > Site Kit by Google