The AMP plugin provides a a simple mechanism to insert analytics tags. Please review the documentation in the AMP project for more details on AMP analytics. And for information on the JSON configuration supplied for Google Analytics, see Adding Analytics to your AMP pages. Notice also that it is recommended that you take advantage of specialized plugins that enable AMP analytics (e.g. Site Kit). Nevertheless, the plugin defines an analytics option to enable the use of amp-analytics in your site, in case you don’t have any other plugin helping you.

The plugin provides a configuration option at the end of the Settings screen:

The plugin provides a brief explanation of you can achieve with this option, and allows you add analytics tags to your pages by entering a JSON configuration definition for each one. If you click on the "+” sign, you can see a text box to specify the target vendor for the analytics configuration, and an empty box to enter the corresponding JSON configuration:

The plugin analytics configuration option provides very simple validation based solely on the validity of the JSON string provided. Do not include any HTML tags like <amp-analytics> or <script>. It is your 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.

Notice that the input to the Type field must correspond to a valid vendor Id; you can supply one of the available analytics vendors or leave it blank for in-house analytics. For Google Analytics specifically, the type should be googleanalytics.

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" } } }
Code language: JSON / JSON with Comments (json)

Reader Mode #

If you are using the plugin in Reader mode, you can use the amp_post_template_analytics filter:

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; }
Code language: PHP (php)

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.

Standard, Transitional mode #

When in Standard or Transitional mode, you can use the filter amp_analytics_entries. 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 accommodate 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:

‘type’StringAnalytics vendor; e.g. ‘googleanalytics’ or ‘nielsen’
‘config’String (valid JSON)JSON-encoded data to be output inside the <amp-analytics>element’s <script type="application/json"> (example)

The following is an example:

/** * 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. ); } );
Code language: PHP (php)

Learn More #

Please see AMP project’s documentation for amp-analytics.