In order to know what you’re doing, you have to analyze what you’re doing.
If you want to learn/analyze a song, the first thing you’re looking at are the types of chords or riffs being used. In order to do that, you have to know how chords and riffs are built. In order to know how chords are built, you need to know intervals. In order to know intervals, you need to know how the major scale lays out up and down the fingerboard. The major scale is the map for everything in music.
Once you know what chords/riffs are being used, you have an idea of whether or not the song is in a certain key. In order to know that, you have to know how the basic major and minor scales are turned into chords/riffs and how these chords/riffs form the basis for the major and minor keys.
Once you have a key or basic tonality established, you have a better idea of where everything else in the song is coming from. So you’re going to look for patterns in the vocal melody, lead licks and solos etc to establish if these follow the key or deviate from the key. In order to know that, you have to be able to identify the commonly used scales and understand how they relate to keys, riffs and chord progressions.
The more you’re able to see all this stuff on the fingerboard, the faster and easier it is to identify the patterns that get used over and over. In order to see this stuff on the fingerboard, you have to spend the time practicing chords, scales and arpeggios and analyzing lots of songs.