Viewing 3 replies - 1 through 3 (of 3 total)
  • I followed the guide here:

    https://developers.google.com/analytics/devguides/collection/amp-analytics/

    We’ll see if it works…

    Put this in your child_theme function.php file:

    //AMP Analytics

    function my_amp_scripts( $data ) {
    $data[‘amp_component_scripts’] = array(
    ‘amp-analytics’ => ‘https://cdn.ampproject.org/v0/amp-analytics-0.1.js’
    );
    return $data;
    }
    add_filter( ‘amp_post_template_data’, ‘my_amp_scripts’ );

    function my_amp_analytics( $amp_template ) {
    ?><amp-analytics type=”googleanalytics”>
    <script type=”application/json”>
    {
    “vars”: {
    “account”: “UA-XXXXXXX-XX”
    },
    “triggers”: {
    “trackPageview”: {
    “on”: “visible”,
    “request”: “pageview”
    }
    }
    }
    </script>
    </amp-analytics><?php
    }
    add_action( ‘amp_post_template_footer’, ‘my_amp_analytics’ );

    U need to replace the UA Code and it works…

    Best wishes

    Santhosh veer

    (@worldfacts)

    Try this code

    //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 } ?>

    Also, check this tutorial – https://www.allwebtuts.com/install-google-analytics-in-wordpress-amp-pages/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to Add Google Analytics code to the AMP pages?’ is closed to new replies.