1# User Action Guidelines 2 3This document gives the best practices on how to use user actions in code and 4how to document them for the dashboard. User actions come with only a name and 5a timestamp. They are best used when you care about a sequence--which actions 6happen in what order. If you don't care about the order, you should be using 7histograms (likely enumerated histograms). 8 9Often, you want both user actions and histogram logging in your code. They 10enable different analyses. They're complementary. 11 12[TOC] 13 14## Coding (Emitting to User Actions) 15 16Generally you'll want to call `base::RecordAction()`, which is defined in 17[user_metrics.h](https://cs.chromium.org/chromium/src/base/metrics/user_metrics.h). 18 19 20### Emit at a High-Level, not Deep in the Implementation 21 22Prefer to emit at the highest level reasonable, closest to the code that handles 23the UI interaction. Emitting deep in implementation code can cause problems 24because that code may get reused (and thus called more times in more places) or 25may get called fewer times (due to caching for example). In cases like this, 26the logged user action will not longer correspond with a meaningful action by 27the user. 28 29### Don't Use Same String in Multiple Places 30 31Generally a logged user action should correspond with a single, uh, action by 32the user. :-) As such, they should probably only appear in a single place in the 33code. If the same user action needs to be logged in multiple places, consider 34whether you should be using different user action names for these separate call 35paths. 36 37That said, if you truly need to record the same user action in multiple places, 38that's okay. Use a compile-time constant of appropriate scope that can be 39referenced everywhere. Using inline strings in multiple places can lead to 40errors if you ever need to revise the name and you update one one location and 41forget another. 42 43### Efficiency 44 45Due to the practices about when and how often to emit a user action, actions 46should not be emitted often enough to cause efficiency issues. (If actions are 47emitted often enough to cause a problem, they're not being emitted at 48appropriate times. See advice below.) 49 50## Emitting Strategies 51 52### Emit Once Per Action 53 54A user action should be tied to an actual action taken by a user. Each 55meaningful unit of action should cause one emit. 56 57### Do Not Emit Redundantly 58 59Generally a meaningful user action should cause only one emit. For example, if 60the browser already has a "Back" user action, it's poor practice to add a 61"BackViaKeyboardShortcut" user action. This is mostly redundant. (If you're 62trying to determine the breakdown of keyboard-shortcut backs versus all backs, 63use a histogram.) 64 65### Do Not Emit Excessively 66 67Again, choose an appropriately-sized meaningful unit. For example, emit 68"DragScrolled" for a whole scroll action. Don't emit this action every time the 69user pauses scrolling if the user remains in the process of scrolling (mouse 70button still down). 71 72As another example, you may want to emit "FocusOmnibox" (upon focus), 73"OmniboxEditInProgress" (upon first keystroke), and "OmniboxUse" (upon going 74somwhere) but forswear "OmniboxKeystroke". That's probably more detailed than 75you need. 76 77### Generally, Do Not Emit Impressions 78 79It's okay to emit user actions such as "ShowTranslateInfobar" or 80"DisplayedImageLinkContextMenu". However, more detailed impression information, 81especially those not caused by the user (as in the second example) and not as 82attention-grabbing (as in the first example), is often not useful for analyzing 83sequences of user actions. For example, don't emit 84"ShowedSecureIconNextToOmnibox". 85 86## Testing 87 88Test your user actions using *chrome://user-actions*. Make sure they're being 89emitted when you expect and not emitted at other times. 90 91If this is a general UI surface, please try to check every platform. In 92particular, check Windows (Views-based platforms), Mac (non-Views), Android 93phone (yet other UI wrapper code), Android tablet (often triggers lookalike but 94different menus), and iOS (yet more different UI wrapper code). 95 96Also, check that your new user action is not mostly redundant with other user 97actions (see [advice above](#Do-Not-Emit-Redundantly)) and not emitted 98excessively (see [advice above](#Do-Not-Emit-Excessively)). 99 100In addition to testing interactively, you can have unit tests check the number 101of times a user action was emitted. See [user_action_tester.h](https://cs.chromium.org/chromium/src/base/test/metrics/user_action_tester.h) 102for details. 103 104## Interpreting the Resulting Data 105 106The top of [go/uma-guide](http://go/uma-guide) has good advice on how to go 107about analyzing and interpreting the results of UMA data uploaded by users. If 108you're reading this page, you've probably just finished adding a user action to 109the Chromium source code and you're waiting for users to update their version of 110Chrome to a version that includes your code. In this case, the best advice is 111to remind you that users who update frequently / quickly are biased. Best take 112the initial statistics with a grain of salt; they're probably *mostly* right but 113not entirely so. 114 115## Revising User Actions 116 117When changing the semantics of a user action (when it's emitted), make it into 118a new user action with a new name. Otherwise the dashboard will be mixing two 119different interpretations of the data and make no sense. 120 121## Documenting User Actions 122 123Document user actions in [actions.xml](./actions.xml). There is also a 124[google-internal version of the file](http://go/chrome-user-actions-internal) 125for the rare case when the user action is confidential (added only to Chrome 126code, not Chromium code; or, an accurate description about how to interpret the 127user action would reveal information about Google's plans). 128 129### Add User Actions and Documentation in the Same Changelist 130 131If possible, please add the actions.xml description in the same changelist in 132which you add the user-action-emitting code. This has several benefits. One, 133it sometimes happens that the actions.xml reviewer has questions or concerns 134about the user action description that reveal problems with interpretation of 135the data and call for a different recording strategy. Two, it allows the user 136action reviewer to easily review the emission code to see if it comports with 137these best practices, and to look for other errors. 138 139### Understandable to Everyone 140 141User actions descriptions should be understandable to someone not familiar with 142your feature. Please add a sentence or two of background if necessary. 143 144It is good practice to note caveats associated with your user actions in this 145section, such as which platforms are supported (if the set of supported 146platforms is surprising). E.g., a desktop feature that happens not to be logged 147on Mac. 148 149### State When It Is Emitted 150 151User action descriptions should clearly state when the action is emitted. 152 153### Owners 154 155User actions need to have owners, who are the current experts on the metric. The 156owners are the contact points for any questions or maintenance tasks. It's a 157best practice to list multiple owners, so that there's no single point of 158failure for such communication. 159 160Being an owner means you are responsible for answering questions about the 161metric, handling the maintenance if there are functional changes, and 162deprecating the metric if it outlives its usefulness. If you are using a metric 163heavily and understand it intimately, feel free to add yourself as an owner. 164@chromium.org email addresses are preferred. 165 166If an appropriate mailing list is available, it's a good idea to include the 167mailing list as a secondary owner. However, it's always a best practice to list 168an individual as the primary owner. Listing an individual owner makes it clearer 169who is ultimately most responsible for maintaining the metric, which makes it 170less likely that such maintenance tasks will slip through the cracks. 171 172Notably, owners are asked to evaluate whether user actions have outlived their 173usefulness. The metrics team may file a bug in Monorail. It's important that 174somebody familiar with the user action notices and triages such bugs! 175 176### Beware `not_user_triggered="true"` 177 178actions.xml allows you to annotate an action as `not_user_triggered="true"`. This 179feature should be used rarely. If you think you want to annotate your action 180thusly, please re-review the best practices above. 181 182## Cleaning Up User Action Entries 183 184Do not delete actions from actions.xml. Instead, mark unused user actions as 185obsolete, annotating them with the associated date or milestone in the obsolete 186tag entry. 187 188If the user action is being replaced by a new version: 189 190* Note in the `<obsolete>` message the name of the replacement action. 191 192* Make sure the descriptions of the original and replacement user actions 193 are different. It's never appropriate for them to be identical. Either 194 the old description was wrong, and it should be revised to explain what 195 it actually measured, or the old user action was measuring something not 196 as useful as the replacement, in which case the new user action is 197 measuring something different and needs to have a new description. 198 199A changelist that marks a user action as obsolete should be reviewed by all 200current owners. 201 202Deleting user action entries would be bad if someone accidentally reused your 203old user action name and which therefore corrupts new data with whatever old 204data is still coming in. It's also useful to keep obsolete user action 205descriptions in actions.xml--that way, if someone is searching for a user action 206to answer a particular question, they can learn if there was a user action at 207some point that did so even if it isn't active now. 208