AMP
  • 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:

While you can attach these macros to any trigger, they resolve individually and asynchronously. Since a trigger will not complete until all macros have resolved, it is often best to split each macro into it's own seperate trigger to prevent underreporting.

<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}",
        "CWV_EVENT": "${event}&type=CWV"
      },
      "triggers": {
          "cls": {
            "on": "visible",
            "request": "CWV_EVENT",
            "extraUrlParams": {
              "cls": "${cumulativeLayoutShift}"
            }
          },
          "lcp": {
            "on": "visible",
            "request": "CWV_EVENT",
            "extraUrlParams": {
              "lcp": "${largestContentfulPaint}"
            }
          },
          "fid": {
            "on": "visible",
            "request": "CWV_EVENT",
            "extraUrlParams": {
              "fid": "${firstInputDelay}"
            }
          },
        }
      }
    }
  </script>
</amp-analytics>

Tracking Core Web Vitals in AMP using Google Analytics

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

We also set the data-credentials attribute to "include" to enable cookies.

Google Analytics metrics are required to be whole numbers. Since Cumulative Layout Shift is measured from 0 to 1, you can use the built in "$CALC" macro to get greater precision.

$CALC(${cumulativeLayoutShift}, 1000, multiply)

<amp-analytics type="gtag" data-credentials="include">
  <script type="application/json">
    {
      "vars": {
         "gtag_id": "G-XXXXXXXX",
         "config": {
           "G-XXXXXXXX": {
              "groups": "default"
           }
         }
      },
      "requests": {
        "event": "https://amp.dev/documentation/examples/advertising-analytics/tracking_core_web_vitals/ping?user=&account=ampdev&event=${eventId}",
        "CWV_EVENT": "${event}&type=CWV"
      },
      "triggers": {
        "defaultPageview": {
          "on": "visible",
          "request": "pageview",
          "vars": {
            "title": "{{title}}"
          }
        },
        "cls": {
          "on": "visible",
          "request": "CWV_EVENT",
          "extraUrlParams": {
            "cls": "$CALC(${cumulativeLayoutShift}, 1000, multiply)"
          }
        },
        "lcp": {
          "on": "visible",
          "request": "CWV_EVENT",
          "extraUrlParams": {
            "lcp": "${largestContentfulPaint}"
          }
        },
        "fid": {
          "on": "visible",
          "request": "CWV_EVENT",
          "extraUrlParams": {
            "fid": "${firstInputDelay}"
          }
        }
      }
    }
  </script>
</amp-analytics>

Google Analytics 4 and AMP

For more information on how to set up Google Analytics 4 with amp-analytics see amp-analytics dev guide and Tag setup for AMP documentation.

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 type="gtag" data-credentials="include">
  <script type="application/json">
    {
      "vars": {
         "gtag_id": "G-XXXXXXXX",
         "config": {
           "G-XXXXXXXX": {
              "groups": "default"
           }
         }
      },
      "requests": {
        "event": "https://amp.dev/documentation/examples/advertising-analytics/tracking_core_web_vitals/ping?user=&account=ampdev&event=${eventId}",
        "CWV_EVENT": "${event}&type=CWV"
      },
      "triggers": {
        "defaultPageview": {
          "on": "visible",
          "request": "pageview",
          "vars": {
            "title": "{{title}}"
          },
          "extraUrlParams": {
            "cd1": "${ampdocHost}"
          }
        },
        "cls": {
          "on": "visible",
          "request": "CWV_EVENT",
          "extraUrlParams": {
            "cls": "$CALC(${cumulativeLayoutShift}, 1000, multiply)"
          }
        },
        "lcp": {
          "on": "visible",
          "request": "CWV_EVENT",
          "extraUrlParams": {
            "lcp": "${largestContentfulPaint}"
          }
        },
        "fid": {
          "on": "visible",
          "request": "CWV_EVENT",
          "extraUrlParams": {
            "fid": "${firstInputDelay}"
          }
        }
      }
    }
    </script>
</amp-analytics>
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기