How to Install Google Analytics in WordPress AMP pages


Install Google Analytics in WordPress AMP pages

Install Google Analytics in WordPress AMP pages

How to Install Google Analytics in WordPress AMP pages

In our previous, we Learn about How to a Custom CSS in AMP pages this tutorial we are going to see about How to Install Google Analytics in WordPress AMP pages.

We can add Google Analytics in WordPress AMP – Accelerated Mobile Pages by two methods

  • Manual Method adding a GA Code from theme functions.php file
  • Install GA from AMP pages extension plugin

Method 1

Using AMP head and footer function to install Analytics code on AMP pages

  • amp_post_template_head – For Header part </head>
  • amp_post_template_footer – For Footer part </body>

Just follow this below Method

  • Copy and paste the Below code in your themes functions.php file
//Add Analytics to AMP Pages
add_action('amp_post_template_footer','awtsamp_google_analytics');
function awtsamp_google_analytics() { ?>
<amp-analytics type="googleanalytics" id="analytics1">
<script type="application/json">
{
"vars": {
"account": "UA-XXXXX-Y" /*replace with your GA tracking id*/
},
"triggers": {
"trackPageview": {
"on": "visible",
"request": "pageview"
}
}
}
</script>
</amp-analytics>
<?php }
/*
* Adding Google Analytics JS Code on AMP Head
*/
add_action('amp_post_template_head','awts_amp_google_analytics_head');
function awts_amp_google_analytics_head() {
?>
<!-- Google Analytics for AMP pages -->
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
<?php } ?>
  • Replace UA-XXXXX-Y with your Google Analytics Tracking ID
  • That’s all successfully we install the Google Analytics on WordPress AMP pages

Output

Google Analytics Track script printed above </head> tag

Install Google Analytics in WordPress AMP pages

Google Analytics Track ID code printed above </body> tag

 Install Google Analytics in WordPress AMP pages

Method 2

Add Google analytics code From Plugin Install AMP pages Extension plugin  – AMP for WP

  • Install and activate the AMP extension plugin
  • Go to Extension setting page and add your Google Analytics Tracking code

https://wordpress.org/plugins/accelerated-mobile-pages/

  • Output same as Above Method

From the Editor’s Desk

If you Have any Doubts and Queries related to AMP (Accelerated Mobile Pages) Analytics Installation Feel Free to comment here, I will Guide you

Learn AMP Installation in WordPress From scratch

 



Was this article helpful?
Thanks!

Your feedback helps us improve Allwebtuts.com