In a previous M.Y. answer. someone cited Ramba"m's classification of 3 types of individuals who view Midrash. The 3rd one is the one that, I think, is the most applicable:
The third category, and there are so few individuals here that they
barely constitute a category, is those who assume Chazal were wise and
moderate, and seek whatever explanation to an Aggada fits accordingly.
In other words, I don't believe that anyone has stated a specific set of rules to follow. It is up to the individual to make his own interpretation, but, at the same time not be foolish about it.
A Midrash says that Ya'akov spent years in yeshiva before he went to Padan Aram to Lavan. Questions that I ask when I see this. The "Yeshiva shel Shem V'Ever" mystifies me. What did they learn? There was no Gemara; the Torah, perhaps, wasn't "completed" as the stories didn't yet occur, and there wasn't yet a nation to receive it. Many other questions can be asked about this "yeshiva" based on what we think of a yeshiva. And, if Ya'akov was literally, in some yeshiva learning something, we have no proof of this, elsewhere, anywhere in Tana"ch.
To take things, literally based on the Torah's story, we assume that Ya'kov was an obedient son. Rivka and Yitzhak send him to Padan Aram to find a wife, and the Torah says, he went there. Did Ya'akov's parents expect him to delay his travel to study? Wasn't he already a "Yoshev Ohalim", which based on another Midrash says that means that he studied Torah? What more did he need to learn? (Again, I'm assuming if we argued toward just pshat of the events).
Conclusion - We could translate this, literally, but, I think a literal translation, here would lead to additional questions as above. Perhaps, there is something "un"literal in this Midrash that we have to find.
Midrashim state that the Avot performed all the Mitzvot. How is this possible? Many mitzvot apply only in the land of Israel, which didn't exist, yet. There's a mitzvah to appoint a king - they couldn't do that. Then, there are the mitzvot that they could have done but didn't. Most notable, perhaps - Ya'akov married 2 sisters.
Conclusion: A literal translation of this Midrash seems impossible.
Your example about G-d wearing tefillin, has to be allegorical as it contradicts Rambam's principle of faith that G-d has no body.