AMP

Thank you!

  • websites

Tracking Core Web Vitals

Introduction

AMP developers can measure Core Web Vitals metrics through the amp-analytics component. Use variable substitution to add these metrics to any outgoing requests made by amp-analytics.

Learn more about amp-analytics in our guide Analytics: the basics.

Setup

Import the amp-analytics component in the header.

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

Using amp-analytics to track Core Web Vitals

The amp-analytics component provides the following macros:

These macros can attach to any triggers and resolve asynchronously.

<amp-analytics>
  <script type="application/json">
    {
      "requests": {
        "event": "https://amp.dev/documentation/examples/advertising-analytics/tracking_core_web_vitals/ping?user=&account=ampdev&event=${eventId}",
        "pageview": "${event}&lcp=${lcp}&fid=${fid}&cls=${cls}"
      },
      "triggers": {
        "trackPageview": {
          "on": "visible",
          "request": "pageview",
          "vars": {
            "eventId": "pageview",
            "lcp": "${largestContentfulPaint}",
            "cls": "${cumulativeLayoutShift}",
            "fid": "${firstInputDelay}"
          }
        }
      }
    }
  </script>
</amp-analytics>

Tracking Core Web Vitals in AMP using Google Analytics

Analytics must be configured in the body. Here we use Google Analytics to track pageviews and use the extraUrlParams feature to append the Core Web Vitals to the request URL.

<amp-analytics type="googleanalytics">
  <script type="application/json">
    {
      "vars": {
         "gtag_id": "UA-XXXXXXXXX-X",
         "config": {
           "UA-XXXXXXXXX-X": {
              "groups": "default"
           }
         }
      },
      "triggers": {
        "default pageview": {
          "on": "visible",
          "request": "pageview",
          "vars": {
            "title": "{{title}}"
          },
          "extraUrlParams": {
            "lcp": "${largestContentfulPaint}",
            "cls": "${cumulativeLayoutShift}",
            "fid": "${firstInputDelay}"
          }
        }
      }
    }
  </script>
</amp-analytics>

Comparing performance on AMP Cache vs Origin

AMP Caches work to make sure your users have as great an expierence possible, but it is important to make sure that the Origin version of the page is just as performant. Your Core Web Vitals may be negativly impacted if your AMP page performs really great when served from an AMP Cache, but less well on the origin. A good way to make sure you are delivering a great experience for all your users is to keep track of how your pages' Core Web Vitals are performing on and off of the AMP Cache.

We can build on our Google Analytics to do this. One of substitutions provided by amp-analytics is ampdocHost - this lets us track the URL of the page the user visited. We exand our earlier config using extraUrlParams. This lets us define a custom key and value for our analytics events. We need to set up a new Custom Dimension, that will be used as the key.

Once you have created the Custom Dimension to track the AMP Host, make sure you note the Index listed on the analytics page. This number is used in our configuration objects to connect ampdocHost to the Dimension you just created. Below we assume your index was "1", (i.e. cd1). If your Index is 2, then use cd2, etc.

<amp-analytics>
  <script type="application/json">
  {
    "vars": {
       "gtag_id": "UA-XXXXXXXXX-X",
       "config": {
         "UA-XXXXXXXXX-X": {
            "groups": "default"
         }
       }
    },
    "triggers": {
      "default pageview": {
        "on": "visible",
        "request": "pageview",
        "vars": {
          "title": "{{title}}"
        },
        "extraUrlParams": {
          "lcp": "${largestContentfulPaint}",
          "cls": "${cumulativeLayoutShift}",
          "fid": "${firstInputDelay}",
          "cd1": "${ampdocHost}"
        }
      }
    }
  }
  </script>
</amp-analytics>
Perlu penjelasan lebih lanjut?

Jika penjelasan pada halaman ini tidak menjawab semua pertanyaan Anda, silakan hubungi pengguna AMP lainnya untuk mendiskusikan contoh penggunaan Anda yang spesifik.

Kunjungi Stack Overflow
Fitur yang tidak dijelaskan?

Proyek AMP sangat menganjurkan partisipasi dan kontribusi Anda! Kami berharap Anda akan terus ambil bagian dalam komunitas sumber terbuka kami, tetapi kami juga menerima kontribusi satu kali untuk topik tertentu yang Anda minati.

Edit sampel di GitHub