A while back I noticed an error in Google Search Console that was flagging Google Analytics code. The error looks like this:
As I mentioned in my post about the Google AMP pages error, on my site, this site, I want to track how many visitors are coming to my site’s AMP pages. Well, it turns out that there is, in fact, an answer to this problem.
— Elena Legeros (@elenalegeros) July 6, 2017
In the Google Analytics Help section, there is a page that explains how to add AMP Analytics to your AMP pages. If you go to that help topic page, you’ll see the sample code and how to implement it in AMP.
Have questions on implementing AMP pages? Feel free to get in touch with me.
The Easy Version of Installing Google Analytics on AMP Pages
So, here’s the shorter version of how to install Google Analytics code on your AMP Pages. I’m using the WordPress AMP plugin, so you’ll need to edit one of the template files.
1. First, you’ll need to use the editor option in WordPress, or you’ll need to go to cPanel for your site and log in there.
2. Then, find the File Manager and find the Plugins directory, then the AMP directory. Look in Templates and you’ll see a file called single.php. It should be located here for most sites: public_html/wp-content/plugins/amp/templates/single.php
3. Once you’ve got that file open, and have the ability to edit it, look for the end of the “head” (just before / head).
4. Add the AMP analytics functionality to your page. This code:
5. Then, you can put put the AMP analytics code on your page (since it’s a short page I put it at the end of the page right after the end body tag. But you can put it, technically, in the head or anywhere else on the page.
The AMP analytics code sample is shown in the Google support doc here:
Or, you can view the source code of my AMP page (put /amp/ at the end of the URL of this post) and you’ll see the code. You’ll want to change your UA- number from my number to your number.
6. Finally, go ahead and test your AMP page URL to see if it’s valid code. Use Google’s AMP Test to test your page. It should look like this if it’s valid AMP code: