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. (See also GA4 Measurement of AMP and Google tag setup for AMP.) Notice also that it is recommended that you take advantage of specialized plugins that enable AMP analytics (e.g. Site Kit, which supports GA4 as of #7221). 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 gtag
(this is as of GA4, where previously the now-obsolete universal analytics used the googleanalytics
type).
Here an example JSON configuration for Google Analytics (where you must replace <GA_MEASUREMENT_ID>
with your own property’s ID):
{
"vars" : {
"gtag_id": "<GA_MEASUREMENT_ID>",
"config" : {
"<GA_MEASUREMENT_ID>": { "groups": "default" }
}
}
}
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();
}
// https://developers.google.com/analytics/devguides/collection/amp-analytics/
$analytics['xyz-googleanalytics'] = array(
'type' => 'gtag',
'attributes' => array(
'data-credentials' => 'include',
),
'config_data' => array(
'gtag_id' => '<GA_MEASUREMENT_ID>',
'config' => array(
'<GA_MEASUREMENT_ID>' => array(
'groups' => 'default',
),
),
),
);
// https://www.parsely.com/docs/integration/tracking/google-amp.html
$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' => 'example.com',
),
),
);
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 theampanalytics
element.
config_data
:(array)
the config data to include in theamp-analytics
script tag. This isjson_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:
Key | Type | Description |
‘type’ | String | Analytics vendor; e.g. ‘gtag’ 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 https://www.ampproject.org/docs/reference/components/amp-analytics
*
* @param array $analytics_entries[][] {
* An array of associative array(s) of the analytics entries to output.
*
* @type string $type The analytics vendor, like 'gtag' 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' => 'gtag',
'config' => wp_json_encode( array(
'gtag_id' => '<GA_MEASUREMENT_ID>',
'config' => array(
'<GA_MEASUREMENT_ID>' => array(
'groups' => 'default',
),
),
) )
),
// 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
.