jessepinho
jessepinho

Reputation: 5772

How do I track multiple Google Optimize experiments in Google AMP?

The amp-experiment docs demonstrate tracking multiple experiments in AMP. However, how do I configure the amp-analytics properly?

In the reporting section of the docs, it mentions a VARIANTS placeholder that will be replaced with a serialized list of experiment and variant names. This answer from the Google Optimize team seems to indicate that it could be added to requests in <amp-analytics> like so:

<amp-analytics type="googleanalytics">
  <script type="application/json">
    {
      "vars": {
        "account": "..."
      },
      "requests": {
        "experiment": "${pageview}&exp=VARIANTS"
      },
      "triggers": {
        "pageview": {
          "on": "visible",
          "request": "experiment"
        }
      }
    }
  </script>
</amp-analytics>

When I try this, though, Google Optimize doesn't register any visits. It seems to require that the experiment request include xid/xvar (for experiment ID/variant) instead:

<amp-analytics type="googleanalytics">
  <script type="application/json">
    {
      ...
      "requests": {
        "experiment": "${pageview}&xid=${xid}&xvar=${xvar}"  <-- modified
      },
      "triggers": {
        "pageview": {
          "on": "visible",
          "request": "experiment",
          "vars": {                                          <-- added
            "xid": "skdjEjia23_1ksjd",                       <-- 
            "xvar": "VARIANT(my-experiment-name)"            <-- 
          }                                                  <-- 
        }
      }
    }
  </script>
</amp-analytics>

This works fine, but then I can only run one experiment at a time, since the xid/xvar are hard-coded to a specific experiment.

Any ideas how I can run multiple experiments?

Upvotes: 1

Views: 666

Answers (1)

jessepinho
jessepinho

Reputation: 5772

OK, I figured it out. I got an answer from the Google Optimize team in the Google Optimize forum.

The VARIANTS variable gets replaced with serialized experiment names and variants, but what Google Analytics apparently needs is experiment IDs and variants. Thus, the correct solution was to configure <amp-analytics> this way:

<amp-analytics type="googleanalytics">
  <script type="application/json">
    {
      "vars": {
        "account": "..."
      },
      "requests": {
        "experiment": "${pageview}&exp=a1b2c3.VARIANT(my-first-experiment)!d4e5f6.VARIANT(my-second-experiment)"
      },
      "triggers": {
        "pageview": {
          "on": "visible",
          "request": "experiment",
        }
      }
    }
  </script>
</amp-analytics>

Note that a1b2c3 and d4e5f6 are the experiment IDs, not the names. The .VARIANT(...) portions following them indicate the index of the variant selected for those experiments. No idea why the IDs are used in the first place and the names in the VARIANT() callbacks 🤷‍♂️

Upvotes: 2

Related Questions