How to Add Google Analytics to Your AMP Page

AMP or Accelerated Mobile Pages is a project by Google designed to help eliminate slow, clunky and frustrating mobile web experience.
AMP uses a different markup for certain things, one of which is adding Google Analytics to your page.

Firstly, we’ll need to add the amp-analytics component library to your web page. The following code goes in between the <head></head>:

https://cdn.ampproject.org/v0/amp-analytics-0.1.js

Afterwards, you can add the component to your web page:

<amp-analytics type=”googleanalytics”>

{
“vars”: {
“account”: “UA-YYYY-Y”
},
“triggers”: {
“defaultPageview”: {
“on”: “visible”,
“request”: “pageview”,
“vars”: {
“title”: “Name of the Article”
}
}
}
}

</amp-analytics>

In the above code, make sure you change your account tracking ID on line five (5).