diff --git a/eth-amp-analytics-wp.php b/eth-amp-analytics-wp.php index c55559a8338f7eb6d6bc48a7f84203a75f14641c..fcaf92cabf4524287b6c8f86e40ab535bea31fc8 100644 --- a/eth-amp-analytics-wp.php +++ b/eth-amp-analytics-wp.php @@ -97,7 +97,7 @@ class ETH_AMP_Analytics_WP { */ public function action_wp_loaded() { if ( ! empty( $this->get_option( 'property_id' ) ) ) { - add_action( 'amp_post_template_head', array( $this, 'action_amp_post_template_head' ), 5 ); // Must appear before the AMP JS library + add_filter( 'amp_post_template_data', array( $this, 'filter_amp_post_template_data' ) ); add_action( 'amp_post_template_footer', array( $this, 'action_amp_post_template_footer' ) ); } } @@ -107,12 +107,16 @@ class ETH_AMP_Analytics_WP { */ /** - * Add component script to header + * Include component script * * Must appear before AMP JS library, per https://developers.google.com/analytics/devguides/collection/amp-analytics/ */ - public function action_amp_post_template_head( $scripts ) { - ?><script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script><?php + public function filter_amp_post_template_data( $data ) { + if ( ! isset( $data['amp_component_scripts']['amp-analytics'] ) ) { + $data['amp_component_scripts']['amp-analytics'] = 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js'; + } + + return $data; } /**