Hi @August. Actually with almost all situations, including the one you describe, the macros I provided will properly track the Current and Previous Desktop Space. This is done using the macro Go to Previous Desktop with the trigger: The focused window: The focused window changes
Notice that I said almost all situations. I recommend using the macros Go Left a Desktop, Go Right a Desktop, etc. when doing explicit space changes because this will guarantee that the Current and Previous Desktop Spaces are properly tracked within the logic of the macro set.
From a practical standpoint, I've found that the rare situations that invisibly change the Desktop Space (from the standpoint of the macro set) cause minimal inconvenience. (I use these macros hundreds of times during the typical week.)
With the above said, my macros could be greatly simplified if @peternlewis was able to add a Space Change trigger to Keyboard Maestro.
If you are looking to roll your own space change detector, I suggest you consider digging into the WhichSpace code. Obviously the developer, George Christou, has determined a method.