Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 334
Dawid Janczak Avatar asked Feb 21 '20 02:02

Dawid Janczak


People also ask

Is Google optimize server side?

Yes, Optimize offers both server-side and JavaScript APIs. The Optimize server-side API uses the Google Analytics Measurement Protocol to measure experiences that you serve. You can also measure AMP experiments with Optimize.

What can you do with Google optimize?

Optimize allows you to test variants of web pages and see how they perform against an objective that you specify. Optimize monitors the results of your experiment and tells you which variant is the leader.


1 Answers

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.

like image 85
jon_wu Avatar answered Sep 30 '22 18:09

jon_wu