3
votes

I have a question regarding the tracking of AMP with Analytics and I've been looking everywhere but sadly couldn't find my answer :)

If my understanding is right, this topic deals with that situation as well, but I'm not sure.. AMP Project - Google Analytics - Content Grouping amp-analytics code

We have a high-traffic blog with the official AMP for WordPress plugin. GA is well implemented on our normal pages but I haven't installed GA tracking on the AMP version, which means I'm missing lots of data.

I know it's not difficult to put the GA tag on AMP but I'm looking for the right way to do it.

If I choose to implement tracking of AMP, I guess I have to set-up a separate Google Analytics property for AMP tracking, which means it will be more difficult to evaluate the trafic of my pages.

To illustrate what I'm saying:

To understand the trafic of "my-page" I will have to look at two sets of data on Analytics and add them to each other to have to total:

On Analytics: www.site.com/blog/my-page/ (for Desktop, tablet and mobile trafic)

www.site.com/blog/my-page/amp/ (for AMP only)

Is there a way to combine these two pages so trafic on "my-page" include both AMP and non-AMP trafic? Indeed, to see the performance of a page I will have to go to two different places now...

Am I asking too much or maybe I'm not the only one looking for that?

I hope it makes sense. Maybe my understanding of how AMP may be questioned here, and I will be more than happy to get it right!

Thanks a lot, Mike

1
You don't have to create separate view for AMP part of your website. Just add on AMP page/template this same Google Analytics tracking ID like on the rest of your page (and it's fine).Jacek Szymański

1 Answers

-1
votes

Open this link and read

Add this hook in WordPress function file

add_filter( 'amp_post_template_analytics', 'xyz_amp_add_custom_analytics' );
function xyz_amp_add_custom_analytics( $analytics ) {
    if ( ! is_array( $analytics ) ) {
        $analytics = array();
    }

// https://developers.google.com/analytics/devguides/collection/amp-analytics/
$analytics['xyz-googleanalytics'] = array(
    'type' => 'googleanalytics',
    'attributes' => array(
        // 'data-credentials' => 'include',
    ),
    'config_data' => array(
        'vars' => array(
            'account' => "UA-XXXXXXXX-X"
        ),
        'triggers' => array(
            'trackPageview' => array(
                'on' => 'visible',
                'request' => 'pageview',
            ),
        ),
    ),
);

return $analytics;

}

above code tracking your amp pageviews if you want to track user where user goes Non-AMP to AMP that you achieve by

<meta name="amp-google-client-id-api" content="googleanalytics">

And Also need to change code in Non AMP analytics JS like

ga("create", "UA-XXXXXX-X", "auto", {'useAmpClientId': true})

Pease read above link.