All developers struggle with something universal, be it any skill level or any language, being productive is where most developers face problems and have a hard time achieving, at least in some point in their careers. It is imperative to be less random, there are a lot of ways to put this, you can call it managing your time better or being more organised, but for most developers, it should be a work ethic where they take on work as it comes and not lay down a concrete plan for everything, this applies especially to people who either work from home or have a lot of freedom in their workday, even if you work for a company which allows you a lot of flexibility in the work pattern.
It does take a lot to be consistent, but at the end of the day it pays off, even from the moment you wake up, so if you have a routine, it can be beneficial. Try to start work at the same time every day and schedule your breaks on regular intervals; these breaks will be identical for the whole week, this kind of structure is essential in gaining consistency in work.
Most of the developers lose productivity because they do not keep track of time and waste most of it on social media sites like Facebook, Instagram, checking analytics, scrolling through news and other sites that you generally surf. We all have websites we are addicted to, you can be really into sports or politics, so you are continually checking news sites or watching youtube videos, it is advised to schedule you time on social media, statistics checking, news and emails in the morning before you start working, this will get things out of your way, and you can work uninterrupted.
Taking breaks is essential, but they shouldn’t be spurting the sporadic need to check Facebook or Twitter. You definitely want to turn off notifications if you are in the middle of programming or doing something that requires your full attention. You get these desktop notifications as someone commented on a post, this like these tend to throw you off your work zone.
If you find it hard to stop yourself from clicking on those notifications, these are hundreds of apps and extensions which will help you in that, and it will actually prevent you from going on those sites at certain times. You should also have a well-planned project structure, and if you work for a company which has a strict workflow then you already have it mapped out for you, they will have projects which are well designed, and they will chart most of your work pattern.