The provided solution (for Custom UI) works fine for a generalPage in Confluence.
But it seems the localId differs in structure and value for different modules. For example the appId is not available in a macro types.
EDIT: I extracted this to a new question How to retrieve environmentId and (real) appId from context in a macro?