I think tracking goals is good, but when you're getting to the level of tracking key strokes that's way too much. How would keystroke or app tracking account for things like time spent thinking about or talking about a problem anyway? It's measuring the wrong thing while putting stress on yourself.
There are two things I did that have had a big positive impact. I use a website blocker - I use Inmotion.app, which also has some other bells and whistles and that was very helpful.
Another thing that helped was tracking my time in a spreadsheet in 15 minute intervals. I color code the time - green for coding and building, red for 'unproductive', purple for necessities - family, working out, meditation etc. I don't make entries every 15 minutes, just whenever I remember but the sheet is open in front of me all the time. This has finally brought the discipline I sometimes lacked.
There is a power to looking back over the last x blocks of 15 minutes and coloring them red. It makes me want to make my day as green as possible and I find myself competing with myself - I highly recommend it.
Just the fact that the productivity market is an entire self-sustaining industry should be a hint that there is a fundamental problem there.