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

Enhancement/7602-parsing-custom-dimensions-report #7697

Merged
merged 23 commits into from
Oct 16, 2023

Conversation

jimmymadon
Copy link
Collaborator

Summary

Addresses issue:

Relevant technical choices

PR Author Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 5.2 and PHP 5.6.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have added a QA Brief on the issue linked above.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

Do not alter or remove anything below. The following sections will be managed by moderators only.

Code Reviewer Checklist

  • Run the code.
  • Ensure the acceptance criteria are satisfied.
  • Reassess the implementation with the IB.
  • Ensure no unrelated changes are included.
  • Ensure CI checks pass.
  • Check Storybook where applicable.
  • Ensure there is a QA Brief.

Merge Reviewer Checklist

  • Ensure the PR has the correct target branch.
  • Double-check that the PR is okay to be merged.
  • Ensure the corresponding issue has a ZenHub release assigned.
  • Add a changelog message to the issue.

@github-actions
Copy link

github-actions bot commented Oct 13, 2023

Build files for c7bb05b are ready:

Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Thanks @jimmymadon – this is off to a good start. I've left some early feedback for you as requested 👍

includes/Core/Util/WP_Entity_Helpers.php Outdated Show resolved Hide resolved
includes/Core/Util/WP_Entity_Helpers.php Outdated Show resolved Hide resolved
includes/Core/Util/WP_Entity_Helpers.php Outdated Show resolved Hide resolved
@@ -42,6 +43,8 @@ public function parse_response( Data_Request $data, $response ) {
return $response;
}

$response = $this->parse_custom_dimensions( $response );
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd suggest a different name for this method since parsing to me is more about extracting one or more values from the input (which is happening here) but more importantly, this is really transforming the response by mapping the IDs to display names. So the response is changing here, but the name doesn't quite reflect that.

foreach ( $response['rows'] as &$row ) {
foreach ( $custom_dimension_map as $dimension_key => $callable ) {
$dimension_value = $row['dimensionValues'][ $dimension_key ]->getValue();
if ( '(not set)' === $dimension_value ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be safer to check ! is_numeric here instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@aaemnnosttv I actually had that check and then wanted to be more "strict" in preventing any string to filter through. But actually, it makes sense to filter through strings which we can't do anything about.

continue;
}

$new_dimension_value = call_user_func( $callable, $dimension_value );
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is going to be rather inefficient to call these functions every time for every row that will likely have many duplicates throughout.

Let's update the process to go through the rows and build a list of all of the IDs for each entity without duplicates (i.e. a Set). Then we can map those to their display names, after which we can go through and do the actual replacements in the dimension values all at once rather than doing everything on every iteration.

includes/Modules/Analytics_4/Report/Response.php Outdated Show resolved Hide resolved
Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Thanks @jimmymadon – this is almost there, just a few points to address 👍

Comment on lines 166 to 167
$this->response->setRows( $rows );
return $this->response;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This operation mutates the rows of the given response, which is evident in the usage but even the tests don't use the return value.

Let's remove the return to make this more explicit (if nothing is returned, the operation must mutate the given report).

Suggested change
$this->response->setRows( $rows );
return $this->response;
$this->response->setRows( $rows );

(also in @return above)

*/
public function swap_custom_dimension_ids_with_names() {
if ( $this->response->getRowCount() === 0 ) {
return $this->response;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

Suggested change
return $this->response;
return;

Comment on lines 52 to 54
public function __construct( Google_Service_AnalyticsData_RunReportResponse $response ) {
$this->response = $response;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This instance is only used in the swap method in which case it could be moved to a dependency of that method rather than the class instance. That way the cache map could be used for multiple responses. It's a minor detail and doesn't need updating right now, just sharing the thought.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was actually worried about the cache map going stale if multiple responses used it - but this actually makes perfect sense. I also initially thought that I'd need the response in other methods - but ended up keeping them decoupled. I've updated this as it is a quick change.

includes/Modules/Analytics_4.php Outdated Show resolved Hide resolved
Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Thanks @jimmymadon – a few final very minor tweaks I've noted otherwise this is good to go! I'll push these now to avoid another back and forth 👍 Nice work!

$dimension_header_post_categories->setName( 'customEvent:googlesitekit_post_categories' );
$response->setDimensionHeaders( array( $dimension_header_post_categories ) );

$rows = array();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This variable is unnecessary :)

* Cache display name results.
*
* @since n.e.x.t
* @var $cache_map
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing the type :)

* @param string|int $user_id User ID of the user to get the display name of.
* @return string|int Display name of the user or their original ID if no name is found.
*/
public function get_post_author_name( $user_id ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

These don't really need to be public I don't think since they're only called internally. Public methods should have tests though, so let's keep them protected for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I actually kept them public so as to add unit tests for them individually. But then ended up creating a sort of larger unit / integration test with the methods. Thanks!

@aaemnnosttv aaemnnosttv merged commit 8aff60e into develop Oct 16, 2023
16 of 17 checks passed
@aaemnnosttv aaemnnosttv deleted the enhancement/7602-parsing-custom-dimensions-report branch October 16, 2023 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants