3
votes

I've implemented a small experiment on an AMP site following this tutorial: https://developers.google.com/optimize/devguides/amp-experiments

Here's what I've done:

1. amp-analytics and amp-experiment components

<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
<script async custom-element="amp-experiment" src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js"></script>

2. Code for the experiment

<amp-experiment>
  <script type="application/json">
    {
      "AMP_Product_Page": {
        "sticky": true,
        "variants": {
          "Original": 50,
          "Variant_1": 50
        }
      }
    }
  </script>
</amp-experiment>

3. Code for analytics

<amp-analytics id='analytics1' type='googleanalytics'>
    <script type='application/json'>
    {
        "vars": {
        "account": "UA-105350-7"
        },
        "requests": {
        "experiment": "${pageview}&xid=${xid}&xvar=${xvar}"
        },
        "triggers": {
            "trackPageview": {
            "on": "visible",
            "request": "experiment",
            "vars": {
                "xid": "fB2hAs9HS2WgWqe332c6Ow",
                "xvar": "VARIANT(AMP_Product_Page)"
            }
        }
        }
    }
    </script>
</amp-analytics>

4. Created an experiment in Google Optimize

I created an experiment in Google Optimize and used the ID in the code above. I can also see the experiment in Google Analytics, it has 0 sessions, though.


I've also added some basic CSS rules and they are working fine.

The code above is live and I can see the experiment running on the site, also the <body> tag has an attribute amp-x-amp_product_page="Variant_1".


Google Analytics requests

I have a Google Analytics integration deployed using GTM AMP container and it's working fine.

Now, when I look at the requests made by Google Analytics, here's what I see:

  1. Pageview request (OK)
  2. Second pageview request with experiment data (status 302)

enter image description here

enter image description here

enter image description here

That second request seems not to make it to GA.

enter image description here

enter image description here


I tried sending experiment data with an event, but it seems like xid and xvar are not allowed variables for an event in amp-analytics.

1
Have you figured this out? I have the same issue. - chris
Not yet @chris. Still doing some heavy debugging and testing of possible solutions. Will post here if I find something. Oh, and please do the same if you manage to find the solution. - Silver Ringvee
I just got it working by moving the experiment and analytics code blocks (your 2 and 3) from head to body. I hope this helps! - chris
In my case, they've been in the body all the time. I'm currently using events to send data to GA, although this way I can't use the content experiments and Google Optimize features. - Silver Ringvee
Somewhere I read the variants should be numbered instead of named in <amp-experiment>. So in your case they should be "0" and "1" instead of "Original" and "Variant_1". Worth a try... - chris

1 Answers

4
votes

Variants should be numbered instead of named in . So in your case they should be "0" and "1" instead of "Original" and "Variant_1".