January 3, 2017

Add google analytics to AMP pages

Hello friends, Today I worked on fixing google analytics problems in AMP pages in a wordpress Blog Website. I found some major problems during making analytics for AMP pages. If don’t know about AMP please refer to below link for adding google analytics to amp pages

WHAT IS AMP?

 

I was working with AMP wordpress plugin and my google webmaster was showing some errors which i needed to fix. Also this plugin doesn’t allows us to add GA(Google Analytics) codes to AMP pages.

Open the /plugins/amp/templates/single.php

  1. Add amp javascript to the header

<script async custom-element=”amp-analytics” src=”https://cdn.ampproject.org/v0/amp-analytics-0.1.js”></script>

2. Add below analytics data to page..Put the code just after BODY tag.

<amp-analytics type=”googleanalytics” id=”analytics2″>

<script type=”application/json”>
{
“vars”: {
“account”: “UA-275390-19” /your GA code**/
},
“triggers”: {
“trackPageviewWithAmpdocUrl”: {
“on”: “visible”,
“request”: “pageview”,
“vars”: {
“title”: “<?php echo wp_kses_data( $this->get( ‘post_title’ ) ); ?>”,
“ampdocUrl”: “<?php echo $this->get( ‘canonical_url’ ); ?>”
}
}
“trackClickOnHeader” : {
“on”: “click”,
“selector”: “#call”, /*click on id #call will be tracked */
“request”: “event”,
“vars”: {
“eventCategory”: “ui-components”,
“eventAction”: “header-click”
}
}
}
}
</script>
</amp-analytics>

 

Now For amp page testing please check here

https://search.google.com/search-console/amp

You may also like...

Leave a Reply