-
Notifications
You must be signed in to change notification settings - Fork 286
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
Enhancement/7602-parsing-custom-dimensions-report #7697
Conversation
Build files for c7bb05b are ready:
|
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.
Thanks @jimmymadon – this is off to a good start. I've left some early feedback for you as requested 👍
@@ -42,6 +43,8 @@ public function parse_response( Data_Request $data, $response ) { | |||
return $response; | |||
} | |||
|
|||
$response = $this->parse_custom_dimensions( $response ); |
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.
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 ) { |
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.
It might be safer to check ! is_numeric
here instead.
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.
@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 ); |
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.
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.
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.
Thanks @jimmymadon – this is almost there, just a few points to address 👍
$this->response->setRows( $rows ); | ||
return $this->response; |
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.
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).
$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; |
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.
Same here
return $this->response; | |
return; |
public function __construct( Google_Service_AnalyticsData_RunReportResponse $response ) { | ||
$this->response = $response; | ||
} |
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.
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.
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.
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.
tests/phpunit/integration/Modules/Analytics_4/Report/Custom_Dimensions_Response_ParserTest.php
Outdated
Show resolved
Hide resolved
tests/phpunit/integration/Modules/Analytics_4/Report/Custom_Dimensions_Response_ParserTest.php
Outdated
Show resolved
Hide resolved
tests/phpunit/integration/Modules/Analytics_4/Report/Custom_Dimensions_Response_ParserTest.php
Outdated
Show resolved
Hide resolved
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.
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(); |
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.
This variable is unnecessary :)
* Cache display name results. | ||
* | ||
* @since n.e.x.t | ||
* @var $cache_map |
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.
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 ) { |
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.
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.
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.
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!
Summary
Addresses issue:
Relevant technical choices
PR Author Checklist
Do not alter or remove anything below. The following sections will be managed by moderators only.
Code Reviewer Checklist
Merge Reviewer Checklist