How to Add Google Analytics Code to AMP Pages WordPress

AMP is a way to build web pages for static content that render fast. Hope you already configured your site AMP comfortable.   There is no need to tell you why Google Analytics is important to our site. Many of us always confuse that Is AMP support Google Analytics . In this article we discuss how to add Google analytics code in WordPress AMP pages.

AMP includes an amp-analytics element that allows you to track user interactions with AMP pages and it has built-in support for Google Analytics.

To show Google Analytics in your WordPress Google AMP pages , just copy paste below code to your theme’s function.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-XXXXXX-1" /*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 } ?>

All set. Now you can monitor your WordPress traffic from Google analytics page. Let me know if you have any questions

Debugging

The AMP Validator can be used to check  whether you placed the Google analytics code in the right way, if your web page doesn’t meet the AMP HTML specification this will show you the error details. Adding #development=1 to the end of the URL of a page will turn on the validator.

The amp-analytics extension provides warning and error messages to help debug and troubleshoot a configuration. Messages are logged to the browser’s console. Add #log=1 to the URL of a page to enable logging.

Bottom Line

Like Google AMP, you can enable Facebook instant article feature too to your WordPress site. Instant articles are only visible for readers reading articles on Facebook’s mobile app (Android or IOS). Apart from host the content in publishers website Facebook Instant Article allows publishers to host their content on Facebook.

For more details please read,

Instant Articles vs Google AMP : A Detailed Comparison

 

 

Leave a Reply