Analytics

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.

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.

Manually

In ‚ÄėClassic‚Äô template mode, you can use the¬†section below¬†if you‚Äôre using ‚ÄėNative‚Äô or ‚ÄėPaired‚Äô mode.

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-XXXXX-Y"
			),
			'triggers' => array(
				'trackPageview' => array(
					'on' => 'visible',
					'request' => 'pageview',
				),
			),
		),
	);

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

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 ‚ÄėNative‚Äô and ‚Äė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 ‚ÄėNative‚Äô or ‚ÄėPaired‚Äô from ‚ÄėAMP Settings‚Äô > ‚ÄėTemplate Mode.‚Äô

When in ‚ÄėNative‚Äô or ‚Äė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 ‚ÄėClassic‚Äô mode sites as well as ‚ÄėNative‚Äô and ‚ÄėPaired‚Äô 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:

KeyValue
Type
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 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 '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' => 'https://example.com/view',
				),
				'triggers' => array(
					'trackPageview'   => array(
						'on'      => 'visible',
						'request' => 'pageview',
					)
				)
			) )
		),
		// There could be another array() here, for another <amp-analytics> element.
	);
} );