Dawid Janczak
Dawid Janczak

Reputation: 81

Multiple server side experiments with Google Optimize and GTM

I was wondering if it's possible to set up multiple server side experiments using Google Optimize and Google Tag Manager. We followed https://stackoverflow.com/a/52157837/12936081 and it seems to be working just fine with the values sent from the data layer, but given the Analytics variable names (expId and expVar) it feels like we can only do that for one experiment at any given time.

Upvotes: 8

Views: 1605

Answers (1)

jon_wu
jon_wu

Reputation: 1152

Instead of sending a separate expId and expVar with the Google Analytics - Universal Analytics Pageview, set exp instead to contain both, and separate multiple experiments by !.

Combine that with info found in the referenced SO question and in the official docs, and we have . to separate the experiment ID and the experiment variants, and - to separate any variants in a multivariate test.

For example, if you're submitting experiment ID A (much longer in real life) with variant index 1 (the first non-control variant), and experiment ID B with variant 0 (the control) in the first section, and variant 2 (the second non-control variant) for the second section, you'd set exp to A.1!B.0-2.

Setting up the data layer

If you're creating a test server side, there's no need to resort to any JavaScript in GTM. Simply set up the dataLayer with the exp variable set, then create a variable that references that from the dataLayer in GTM. I found that even though GTM and the Google Analytics Pageview hit run asynchronously, I had to set by data early enough for the GA to pick it up. You could add a separate script tag for this anywhere, or add it to the GTM snippet itself if that's easy to modify. In my case, I just injected this to the end of the <head> via a Cloudflare Worker using HTMLRewriter, since my test is implemented at the edge.

<script>var dataLayer=dataLayer||[];dataLayer.push({"exp":"A.1!B.0-2"})</script>

How I determined this

I found this format by temporarily adding the client-side Google Optimize JavaScript, and launching two concurrent experiments, and then looking at the network request being sent. I've tested this briefly on a dev instance and data seems to come in.

Upvotes: 4

Related Questions