Optimizely snippet through site code, waiting with tags in Google Tag Manager
We have moved our Optimizely snippet around because we have had some trouble with our implementation. First, we sent the Optimizely snippet through our site code (in the body, after GTM had loaded, while also pushing an event into the dataLayer so that the GTM pageview tag could wait for this) and also the Optimizely ecommerce through the site code (also in the body, nothing waiting for this). This worked OK, although we never trusted the result because when connected with Custom Variable to GA, the data in GA looked absurd (super high conversion, tending to mean that we lost a lot of events without purchases).
So, we moved Optimizely into the GTM code, putting it in a tag instead. This still had no effect on the problem above and after reading a few articles on the net where they were recommending not to use GTM for Optimizely, we moved it back again.
This time, we got an error that the snippet could not be found on the page (which I have read now that it might be OK to ignore this error!?).
Now, to structure this question up a bit, I will add a few questions that would help me. When using Optimizely together with a Google tag manager solution ...
* ... is it better to send the Optimizely snippet through tag manager instead of through the code? Or does it not make any difference?
* ... Optimizely should be placed in the body, after GTM, but before a GA pageview?
* ... Optimizely ecommerce should be placed in the body, after GTM and after Optimizely loaded, but before a GA pageview and before a GA ecommerce?
* ... is it enough to let the GA pageview and the GA ecommerce wait for Optimizely? Or should all of the tags wait for Optimizely (third party scripts etc)?
We have used Optimizely for over 6 months but are still not able to trust the results. Please help!
Using Optimizely / GA with GTM can get pretty messy, you're not alone in your troubles. But luckily, there's a way to get everything working well.
The simplest answer is to implement Optimizely outside GTM. GTM by default loads all scripts asynchronously, which means there's no guarantee when Optimizely runs, and thus when the experiments fire and if data will be captured in GA correctly.
In addition, the snippet should't be placed in the body, but in the <head> portion of the page. In fact, it should be placed as high up in <head> as possible, in front of everything except jQuery. Here's a KnowledgeBase article on where to implement the snippet and why, it talks about GTM as well.
To ensure that information is being sent to GA properly, you'll also have to take some extra steps when it's implemented via GTM. See this thread for the details.
Hi, and thanks so much for the reply!
I have read through the links but whenever they talk about GTM, they say "you can implement it like this if you want to, even though it is a bad idea" and then they explain how to put it in tag manager. But what I don't get is, if it is completely OK to have Optimizely without sending any events to GTM and putting any delays at all in GTM? Then the GA pageview might (or might not, since it is async as well) happen before Optimizely events, right?
And the Optimizely ecommerce event, we can only send this one in through the body (the information has not yet reached us from our third-party checkout solution) until this time. So this would be even more likely to happen after any pageviews via GTM. Still OK?
As I wrote, a solution would be to post Optimizely through the code but also post events to the dataLayer and have a delay on GTM tags (pageview, ecommerce). Should this be done? None of the articles you referred to have stated this kind of solution.
Thanks so much for the reply, code stop tomorrow so I am in a bit of a hurry to get everything done asap
If you're in a hurry, the best solution is to take both Optimizely and GA out of GTM and implement them manually as described in the articles (have the GA call in a separate script tag and have the Optimizely push before the GA push). As for the revenue event, as long as it is fired before GA fires, you should be in the clear. So place that call in the body before GA and you should be all set!
Hope this helps,
Hm, so you recommend that we take everything Analytics-related out of Tag Manager? I really don't like that solution since Tag Manager has given us Analytics numbers closer to what offline tells us, plus it has proven a very good tool for us to use. Backtracking also takes a long time, and makes it more difficult to change to Universal in the future.
I think we will instead put Optimizely back into GTM. It is not ideal at all, but we will try this time to have Opt ecommerce listen to the Opt loaded event, and have GA tags listen to the ecommerce event and loaded event (depending on page), and perhaps we will get more accurate numbers on the confirmation.
Thanks for your time though, sorry for not being able to go the way you recommend!
Thanks for your reply, we will set this up (and the tracker is already clicked since our previous tries with communicating with Optimizely through GTM).
Hopefuly by letting GA wait for optimizely ecommerce script, we might get more reliable conversion numbers in Analytics.