If $A$ and $B$ are commutative we can write $A = \prod_{i \in I} A_i$ where the $A_i$ are connected (have no nontrivial idempotents) and similarly for $B = \prod_{j \in J} B_j$ where $I, J$ are two finite index sets. Then a morphism $f : A \to B$ corresponds geometrically to a morphism
$$\text{Spec } f : \bigsqcup_{j \in J} \text{Spec } B_j \simeq \text{Spec } B \to \text{Spec } A \simeq \bigsqcup_{i \in I} \text{Spec } A_i$$
On connected components this morphism gives a map of sets $g : J \to I$ and the extra data needed to recover it exactly is the data, for each $j \in J$, of a morphism $\text{Spec } f_j : \text{Spec } B_j \to \text{Spec } A_{g(j)}$, dualizing to a morphism $f_j : A_{g(j)} \to B_j$. Then
$$f \left( \prod_{i \in I} a_i \right) = \prod_{j \in J} f_j(a_{g(j)}).$$
This can all be done in algebraic language using idempotents but the geometric picture makes it clearer what's happening intuitively, I think. Formally we're using the fact that the category of affine schemes is extensive.
Every idempotent $e$ of $A$ is now the indicator function $e_S$ of some subset $S \subseteq I$, and $\dim A e_S = \sum_{i \in S} \dim A_i$. Applying $f$ gives an idempotent $f(e_S) = e_{g^{-1}(S)}$ which is the indicator function of $g^{-1}(S) \subseteq J$, and $\dim B f(e_S) = \sum_{g(j) \in S} \dim B_j$. So here the condition we want is that for every subset $S \subseteq I$ we have
$$\frac{\sum_{i \in S} \dim A_i}{\sum_{i \in I} \dim A_i} = \frac{\sum_{g(j) \in S} \dim B_j}{\sum_{j \in J} \dim B_j}.$$
This condition can be cleanly restated as follows: the dimensions $\dim A_i$ normalize to a probability measure on $I$ and similarly the dimensions $\dim B_j$ normalize to a probability measure on $J$, and what we want is that the pushforward of the dimension measure on $J$ along $g$ is the dimension measure on $I$.
In the simplest case that the $A_i$ and $B_j$ are all the ground field $F$ this says that we want $g : J \to I$ to be a map all of whose fibers have the same size (which explains your two examples). It seems tricky to me to relate this condition to standard algebro-geometric conditions because it is sensitive only to the dimensions of the $A_i$ and $B_j$ and to the map $g : J \to I$ and otherwise it is not sensitive to any other features of the maps $f_j : A_{g(j)} \to B_j$, e.g. it does not care whether they are injective, free, flat, etale, whatever. It smells a bit like it might be implied by "finite locally free of degree $d$" or something like that, but I haven't actually checked whether that works when $A$ and $B$ are, say, etale.