Bounce rate statistics: Modify "up" arrow colour when positive (from red to green)
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

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 :)

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
DataBlockcomponent with a new booleaninvertChangeprop (defaultfalse) - Use
invertChangeinternally in place ofreverseArrowDirection - When
invertChangeis truthy, multiply the providedchangeprop by-1to make a positive change negative, and vice-versa - Remove the old
reverseArrowDirectionprop as it is only used for this purpose (bounce rate specifically) - Document why this is done this way in the
DataBlockcomponent
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
currentColorrather than any specific value
- The path fill can inherit its color from
- Accept a
directionprop -upordown- Add a
arrow-pointing-${ direction }class accordingly
For downward arrows, add a CSS class to rotate it180 deg - Add a class to apply the color based on the direction (by default)
- Add a
- Accept an
invertColorprop to apply the opposite color class based on thedirection - Update usage of
arrow-positiveandarrow-negativesvg if others exist outside ofDataBlockto use new component - Rename
DataBlock.props.reverseArrowDirectiontoinvertChangeColorto be passed to the new innerChangeArrowcomponent'sinvertColorprop
Changelog entry
- Fix incorrect change arrow direction and color on "inverted" properties like bounce rate.
@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.
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 ] );
This appears to be correct now, is it not?

Nevermind, the large number is the average. After looking at the actual data I can confirm this is incorrect.
@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.
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 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 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 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 ✅
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
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
DataBlockto pass through to the new component. Perhaps aninvertChangeColorprop? I'll add that to the IB 👍
SGTM
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


Can I get a 2nd set of eyes on this as I feel there's a lot to work on with this functionality.
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

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?