Either or. You start by asking about gym hair care and then it sounds more general. Given the small variations between one person and the next don't base how often you wash your hair on the next person.
Wear a hat in the gym if you're worried about it getting grubby there. Clean it as often as you think it looks greasy. Google will probably tell you the average number of times and most bottles will recommend a similar number.