There are two options you can follow to include analytics tags in your posts.

Plugin Analytics options

For information on the JSON configuration supplied for Google Analytics, see Adding Analytics to your AMP pages.

The plugin defines an analytics option to enable the addition of amp-analytics in your posts. When the plugin is active, an AMP top-level menu appears in the Dashboard with one inner sub-menu called ‘Analytics’:

Selecting the Analytics sub-menu in the AMP options menu takes us to an Analytics Options entry page, where we can define the analytics tags we want to have, by specifying the vendor type (e.g. Parsely), and the associated JSON configuration.

Notice that the goal of this option of the plugin is to provide a simple mechanism to insert analytics tags; it provides very simple validation based solely on the validity of the JSON string provided. It is the users responsibility to make sure that the values in the configuration string and the vendor type used are coherent with the analytics requirements of their site . Please review the documentation in the AMP project and in AMPByExample. Here an example JSON configuration for Google Analytics (where you must replace UA-XXXXX-Y with your own account number):

{ "vars": { "account": "UA-XXXXX-Y" }, "triggers": { "trackPageview": { "on": "visible", "request": "pageview" } } }

The AMP Analytics options entry form provides a very simple validation feedback mechanism: if the JSON configuration string entered is invalid (i.e. not valid JSON), an error message (in red) is displayed below the title of the options window and the entry form is reloaded:

And, if the configuration provided is actually a valid JSON string, a success message (in green) is displayed at the top of the window below the title, and again the entry form is reloaded.

For more on the JSON configuration supplied for Google Analytics, see Adding Analytics to your AMP pages.


In ‘Reader’ (formerly ‘Classic’) template mode, you can use the amp_post_template_analytics filter. This template mode is set either from selecting ‘Reader’ in ‘AMP Settings’ > ‘Template Mode,’ or from not calling add_theme_support( 'amp' ).

See the section below if you’re using ‘Standard’ (formerly ‘Native’) or ‘Transitional’ mode (formerly ‘Paired’).

add_filter( 'amp_post_template_analytics', 'xyz_amp_add_custom_analytics' ); function xyz_amp_add_custom_analytics( $analytics ) { if ( ! is_array( $analytics ) ) { $analytics = array(); } // $analytics['xyz-googleanalytics'] = array( 'type' => 'googleanalytics', 'attributes' => array( // 'data-credentials' => 'include', ), 'config_data' => array( 'vars' => array( 'account' => "UA-XXXXX-Y" ), 'triggers' => array( 'trackPageview' => array( 'on' => 'visible', 'request' => 'pageview', ), ), ), ); // $analytics['xyz-parsely'] = array( 'type' => 'parsely', 'attributes' => array(), 'config_data' => array( 'vars' => array( 'apikey' => 'YOUR APIKEY GOES HERE', ) ), ); $analytics['alexa'] = array( 'type' => 'alexametrics', 'attributes' => array(), 'config_data' => array( 'vars' => array( 'atrk_acct' => CUSTOMER_ACCOUNT_CODE, 'domain' => '', ), ), ); return $analytics; }

Each analytics entry must include a unique array key and the following attributes:

  • type(string) one of the valid vendors for amp-analytics.
  • attributes(array) any additional valid attributes to add to the ampanalytics element.
  • config_data(array) the config data to include in the amp-analytics script tag. This is json_encode-d on output.

Filtering Analytics Data In ‘Standard’ (formerly ‘Native’) and ‘Transitional’ (formerly ‘Paired’) Mode

There’s a different filter for analytics data when there’s theme support. This could be from calling add_theme_support( ‘amp’ ), or from selecting ‘Standard’ or ‘Transitional’ from ‘AMP Settings’ > ‘Template Mode.’

When in ‘Standard’ (formerly ‘Native’) or ‘Transitional’ (formerly ‘Paired’) mode, you can use the filter amp_analytics_entries, though this will also apply if there’s no theme support. If you are implementing analytics within a plugin, you may want to use this filter as well as the legacy amp_post_template_analytics in order to accomodate ‘Reader’ mode sites as well as ‘Standard’ and ‘Transitional’ sites.

The amp_analytics_entries filter’s only parameter, $analytics_entries, is an array of associative array(s). Each associative array should have the following keys and values:

Value Description
'type' string Analytics vendor, like ‘googleanalytics’ or ‘nielsen’
'config'string (valid JSON)JSON-encoded data to be output inside the <amp-analytics>element’s <script type="application/json"> (example)

Here’s an example use of this filter:

/** * Filters the analytics data for <amp-analytics> elements. * * @link * * @param array $analytics_entries[][] { * An array of associative array(s) of the analytics entries to output. * * @type string $type The analytics vendor, like 'googleanalytics' or 'nielsen'. * @type string $config JSON-encoded data to be output inside the <amp-analytics> element's <script type="application/json">. * } */ add_filter( 'amp_analytics_entries', function( $analytics_entries ) { unset( $analytics_entries ); return array( array( 'type' => 'googleanalytics', 'config' => wp_json_encode( array( 'requests' => array( 'pageview' => '', ), 'triggers' => array( 'trackPageview' => array( 'on' => 'visible', 'request' => 'pageview', ) ) ) ) ), // There could be another array() here, for another <amp-analytics> element. ); } );