Skip to content

Analytics

Weston Ruter edited this page Dec 15, 2017 · 12 revisions

To output proper analytics tags, 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' => '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',
			)
		),
	);

	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 amp-analytics element.
  • config_data: (array) the config data to include in the amp-analytics script tag. This is json_encode-d on output.

Clone this wiki locally