site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Bounce rate statistics: Modify "up" arrow colour when positive (from red to green)

Open WebFactoryLtd opened this issue 7 years ago • 13 comments

Bug Description

The change in bounce rate percentage is calculated correctly, but the arrow beside it, that should indicate a good or bad movement, is inverted.

Steps to reproduce

Open Analytics in Site Kit.

Screenshots

image

Additional Context

I've double-checked the numbers in GA and it's all good. So the only thing to fix is the arrow. It should be green and pointing downward. As clearly shown in the screenshot my bounce rate dropped. So it's good, should be green :)

GKeMYxwHG9n


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

Acceptance criteria

  • When the trend shows that bounce rate is decreasing, display a downward-pointing arrow in green colour.
  • When the trend shows that bounce rate is increasing, display an upward pointing arrow in red colour. (currently, exactly the opposite is happening)

Implementation Brief

Simplest change
  • Update the DataBlock component with a new boolean invertChange prop (default false)
  • Use invertChange internally in place of reverseArrowDirection
  • When invertChange is truthy, multiply the provided change prop by -1 to make a positive change negative, and vice-versa
  • Remove the old reverseArrowDirection prop as it is only used for this purpose (bounce rate specifically)
  • Document why this is done this way in the DataBlock component

Better change

  • Create a new <ChangeArrow> component to replace usage of these svg sprites and provide more control
  • Render <svg> for an up arrow directly to allow for styling via CSS
    • The path fill can inherit its color from currentColor rather than any specific value
  • Accept a direction prop - up or down
    • Add a arrow-pointing-${ direction } class accordingly
      For downward arrows, add a CSS class to rotate it 180 deg
    • Add a class to apply the color based on the direction (by default)
  • Accept an invertColor prop to apply the opposite color class based on the direction
  • Update usage of arrow-positive and arrow-negative svg if others exist outside of DataBlock to use new component
  • Rename DataBlock.props.reverseArrowDirection to invertChangeColor to be passed to the new inner ChangeArrow component's invertColor prop

Changelog entry

  • Fix incorrect change arrow direction and color on "inverted" properties like bounce rate.

WebFactoryLtd avatar Aug 31 '19 12:08 WebFactoryLtd

@aaemnnosttv Since you worked on a related item (#461), can you tell where this is coming from? Do we calculate the delta value incorrectly maybe (as in negative instead of positive)? The component has the arrow itself inverted, so that's fine.

felixarntz avatar Sep 12 '19 08:09 felixarntz

Do we calculate the delta value incorrectly maybe (as in negative instead of positive)?

Yes, that seems to be the cause. When the value is negative, the color is red, and when positive, it's green. Average bounce rate is the only statistic where we invert the logic for arrow direction, so this hasn't come up with another stat yet. It seems we also need to invert change value.

diff --git a/assets/js/modules/analytics/util.js b/assets/js/modules/analytics/util.js
index d0d87d8..9e6aafb 100644
--- a/assets/js/modules/analytics/util.js
+++ b/assets/js/modules/analytics/util.js
@@ -216,7 +216,7 @@ export const calculateOverviewData = ( reports ) => {
 	const totalPageViews = lastMonth[ 5 ];
 	const totalUsersChange = changeToPercent( previousMonth[ 0 ], lastMonth[ 0 ] );
 	const totalSessionsChange = changeToPercent( previousMonth[ 1 ], lastMonth[ 1 ] );
-	const averageBounceRateChange = changeToPercent( previousMonth[ 2 ], lastMonth[ 2 ] );
+	const averageBounceRateChange = changeToPercent( previousMonth[ 2 ], lastMonth[ 2 ] ) * -1;
 	const averageSessionDurationChange = changeToPercent( previousMonth[ 3 ], lastMonth[ 3 ] );
 	const goalCompletionsChange = changeToPercent( previousMonth[ 4 ], lastMonth[ 4 ] );
 	const totalPageViewsChange = changeToPercent( previousMonth[ 5 ], lastMonth[ 5 ] );

aaemnnosttv avatar Sep 12 '19 09:09 aaemnnosttv

This appears to be correct now, is it not?

image

aaemnnosttv avatar Jan 29 '20 09:01 aaemnnosttv

Nevermind, the large number is the average. After looking at the actual data I can confirm this is incorrect.

aaemnnosttv avatar Jan 29 '20 09:01 aaemnnosttv

@aaemnnosttv I don't think the IB table is correct:

  • The arrow direction should always match whether the change is positive or negative. E.g. if +50%, show upward arrow, regardless of whether inversed or not.
  • The arrow color is the only thing that should change: If inversed, an upward arrow should be red, and a downward arrow should be green.

As such, maybe we should have inverseColor instead.

felixarntz avatar Jan 31 '20 10:01 felixarntz

You're right, good catch @felixarntz! As for the prop name, I think invertChange or invertValue may be a bit more apt as it's more of an inversion of the logic as to what is good and bad rather than color specifically. The only thing we need to do in this case is to multiply the provided value by -1 and the color will change, but that's more of an implementation detail. Color could be represented by a 👍 / 👎 , etc. In the end the prop name isn't all that important, just sharing my thoughts. I'll update the IB.

aaemnnosttv avatar Jan 31 '20 10:01 aaemnnosttv

@aaemnnosttv I'm not sure about multiplying the change with -1, wouldn't this result in both the arrow direction and the color to be inverted? Only the color should be inverted.

felixarntz avatar Jan 31 '20 14:01 felixarntz

@felixarntz digging into this a bit more, the implementation is a bit odd. There are 2 separate svgs used for the arrows; one for positive and one for negative which include the color and the direction in the svg itself. The current reverse arrow direction prop then adds a class to rotate the arrow by 180 deg using css.

The idea of multiplying by -1 was something I found that solved the problem when looking into it initially (as the code is now). So for bounce rate specifically, multiplying the change makes a positive change (increase - bad) negative (red pointing down) and then the added prop reverses the direction making it point up which is correct (hence the confusion).

Ideally, we would only have a single arrow svg which pointed up or down based the change being positive or negative using css for the direction. The color of the arrow should also be determined by CSS by styling the fill of the svg's path element. Given these changes, a single invertColor prop makes sense and no multiplication or reversal of the arrow direction is necessary.

However, since the svgs are implemented as a sprite, we can't apply styles to the svg itself in this way. For this reason, the invert prop (while replacing the old reverse direction prop) needs to both invert direction and the change value to achieve the desired result (a negative/red arrow reversed to point up, or a positive/green arrow reversed to point down).

I hope that makes more sense now. I'll update the IB.

aaemnnosttv avatar Jan 31 '20 16:01 aaemnnosttv

@aaemnnosttv Oh thanks for clarifying! That existing implementation seems a bit odd indeed.

Let's go with your "Better change" variant - I'll move this to the estimate column so you can confirm/adjust the estimate based on that implementation.

IB ✅

felixarntz avatar Jan 31 '20 16:01 felixarntz

Sounds good. I'll keep it as-is. One thing I forgot to mention is that we'll still need a prop of some kind on DataBlock to pass through to the new component. Perhaps an invertChangeColor prop? I'll add that to the IB 👍

aaemnnosttv avatar Jan 31 '20 17:01 aaemnnosttv

@aaemnnosttv

Sounds good. I'll keep it as-is. One thing I forgot to mention is that we'll still need a prop of some kind on DataBlock to pass through to the new component. Perhaps an invertChangeColor prop? I'll add that to the IB 👍

SGTM

felixarntz avatar Jan 31 '20 17:01 felixarntz

I'm not sure this is fixed. See below. I'm seeing a decrease from month to month yet the arrow shows an up arrow in red.

Also noting the X axis showing multiples of the same day with different values

Another note, (see 2nd shot) both lines are showing the same month

image

image

Can I get a 2nd set of eyes on this as I feel there's a lot to work on with this functionality.

cole10up avatar Feb 11 '20 06:02 cole10up

Good catch @cole10up , there was indeed a bug in the way the arrow direction was being set. There is a PR to fix this now in #1119. Using this branch you can see that the arrow is pointing correctly now

image

Also noting the X axis showing multiples of the same day with different values Another note, (see 2nd shot) both lines are showing the same month

Thanks for mentioning these. These are unrelated to the changes introduced here but should be further investigated. Can you please open a new issue for these?

aaemnnosttv avatar Feb 11 '20 11:02 aaemnnosttv