Let $\zeta$ be an $n*1$ vector:
$$\zeta=[(GXB)\odot D]l$$ where $G$ is an $n* n$ matrix, $X$ is an $n* k$ matrix, $B$ is a $k* d$ matrix, $D$ an $n* d$ matrix and $l$ a $d*1$ vector
We need to differentiate the scalars $\zeta^T\zeta$ and $\zeta^Ty$ with respect to matrix B ($y$ is an $n*1$ vector). $$\frac{\partial \zeta^T\zeta}{\partial B}=?$$ $$\frac{\partial \zeta^Ty}{\partial B}=?$$
For typing convenience, define $$\eqalign{ H = GXB,\quad\zeta = (H\odot D)\ell }$$ and use a colon to denote the trace/Frobenius product, i.e. $$A:B = {\rm Tr}(AB^T) = {\rm Tr}(A^TB)\\AB:C = B:A^TC = A:CB^T$$
Write the first function in terms of the above, then calculate its gradient. $$\eqalign{ \phi &= \zeta:\zeta \\ d\phi &= 2\zeta:d\zeta \\ &= 2\zeta:(dH\odot D)\ell \\ &= 2(\zeta\ell^T\odot D):dH \\ &= 2(\zeta\ell^T\odot D):GX\,dB \\ &= 2(GX)^T(\zeta\ell^T\odot D):dB \\ \frac{\partial\phi}{\partial B} &= 2X^TG^T(\zeta\ell^T\odot D) \\ }$$ The calculation for the second function is very similar. $$\eqalign{ \psi &= y:\zeta \\ d\psi &= y:d\zeta \\ &= y:(dH\odot D)\ell \\ &= (y\ell^T\odot D):dH \\ &= (y\ell^T\odot D):GX\,dB \\ &= (GX)^T(y\ell^T\odot D):dB \\ \frac{\partial\psi}{\partial B} &= X^TG^T(y\ell^T\odot D) \\ }$$