Since \begin{align*} \left(\frac{x+2}{x+4}\right)^2 &= \frac{x^2+4x+4}{(x+4)^2} \\ &= \frac{4}{(x+4)^2}+\frac{x(x+4)}{(x+4)^2} \\ &= \frac{4}{(x+4)^2}+\frac{x}{x+4} \\ &=f'(x)+f(x), \\ \end{align*} let $$ \color{blue}{f(x)=\frac{x}{x+4}}. $$
Now for $C$ constant, since $$ \dfrac{d}{dx}(e^x f(x)+C) = (e^x f(x)+C)' = e^x f'(x)+e^x f(x), $$ we have $$ \int \left(e^x f'(x)+e^x f(x)\right)dx = e^x f(x)+C. $$ So for the above indefinite integral, $$ \begin{align*} \color{green}{\int e^x\left(\frac{x+2}{x+4}\right)^2 dx} &= \int e^x \left(f'(x) + f(x)\right) dx \\ &= \int \left(e^x f'(x)+e^x f(x)\right) dx \\ &= e^x f(x)+C \\ &= \color{green}{\frac{x\: e^x}{x+4} + C}. \end{align*} $$