Google Tag Manager
Why the base script belongs directly in your <head> and not in GTM, and how to minimize the damage if GTM is your only option.
Short version: put the base snippet directly in your <head>, not in GTM. Use GTM for conversion events if you like, because that part is fine and fully supported.
Why the base script should not go through GTM
GTM loads asynchronously
The GTM container itself is an async script. Anything inside it therefore runs after the container has downloaded and parsed. Optimeleon's bootstrap needs to run during HTML parse, before any body content paints, and that is the entire mechanism by which flicker is prevented.
Put the bootstrap behind GTM and you have moved the anti-flicker guard to a moment after the original page has already had a chance to paint. Visitors see the original, then see it change.
Anti-flicker cannot protect what has painted
The guard hides the body the instant it parses and releases it when the variant has been applied, or after its safety timeout, whichever comes first. Both halves depend on running early. A late guard is worse than no guard: it can hide a page that was already visible.
History triggers misfire on single-page apps
A common GTM setup fires tags on a History Change trigger. On an SPA that fires on every client-side route change, which can re-run the tag and re-initialise the script in a page lifecycle that never expected it.
<head>, as high as you can put it. Conversion events through GTM if that is where your conversion triggers already live. This gets you the flicker-free serving and keeps event management where your team works.If GTM is genuinely your only option
Some organisations cannot touch the site template at all. It will work, with real and visible costs. Do it like this:
Use a Page View trigger, not History Change
Fire the tag on Page View, and ideally on Initialization, the earliest GTM trigger available, so it runs as close to the start of the page as the container permits.
Do not attach a History Change trigger. On an SPA it will re-fire the tag on every route change.
Put the tag first in GTM's firing order
Set a tag firing priority higher than every other tag in the container, so Optimeleon is the first thing GTM executes rather than the last.
Expect flicker, and measure it
Load your page on a throttled connection and watch. If you can see the original before the variant, your visitors can too. That is the cost of this route, and it is worth quantifying before you accept it.
Conversion events through GTM: fully supported
This is the good use of GTM. Create a Custom HTML tag and attach your existing conversion trigger to it:
<script>
window.optimeleon && window.optimeleon('track', 'your_event_name', {});
</script>The guard makes the tag a safe no-op if the base script is missing on that page, and firing more than once is safe. See Conversion events.
