In principle, we recommend the use of Google Optimize for A/B tests.
Google recommends the following setup to integrate Google Optimize:
For best performance: Embed Google Optimize directly into the source code of the page. (Google Documentation)
Easiest to configure: Integrate Google Optimize via Google Analytics. (Google Documentation)
Of course, you can also integrate Google Optimize via the Google Tag Manager (Documentation from Google) or via gtag.js (Dokumentation from Google).
For the optimal order of integration of the different Google products like Google Analytics or Google Optimize, please read this Documentation from Google.
The optimal configuration is of course heavily dependent on the respective shop. We recommend that you discuss the optimal setting with your agency.