Skip to content
Snippets Groups Projects
Commit e0d85b58 authored by Erick Hitter's avatar Erick Hitter
Browse files

Improve how component script is included

Props @mjangda for the pointer.
parent 1387f972
Branches
Tags
1 merge request!1WIP: Initial release
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
...@@ -97,7 +97,7 @@ class ETH_AMP_Analytics_WP { ...@@ -97,7 +97,7 @@ class ETH_AMP_Analytics_WP {
*/ */
public function action_wp_loaded() { public function action_wp_loaded() {
if ( ! empty( $this->get_option( 'property_id' ) ) ) { 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' ) ); add_action( 'amp_post_template_footer', array( $this, 'action_amp_post_template_footer' ) );
} }
} }
...@@ -107,12 +107,16 @@ class ETH_AMP_Analytics_WP { ...@@ -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/ * Must appear before AMP JS library, per https://developers.google.com/analytics/devguides/collection/amp-analytics/
*/ */
public function action_amp_post_template_head( $scripts ) { public function filter_amp_post_template_data( $data ) {
?><script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script><?php 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;
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment