diff --git a/CHANGELOG.md b/CHANGELOG.md index e2adaa2..c27421d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## 1.8.1 +* Fix AMP compatiblity for Standard and Transitional mode (#181) (#182) +* JavaScript is no longer embedded if request is served by AMP (#181) (#182) + ## 1.8.0 * Fix date offset in dashboard widget in WP 5.3+ environments with mixed timezones (#167) * Allow to deactivate the nonce check during JavaScript tracking (#168) diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index 6394886..711187a 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -374,8 +374,12 @@ public static function query_vars( $vars ) { * @version 1.4.1 */ public static function wp_footer() { - // Skip by option. - if ( ! self::is_javascript_tracking_enabled() ) { + // JS tracking disabled or AMP is used for the current request. + if ( + ! self::is_javascript_tracking_enabled() || + ( function_exists( 'amp_is_request' ) && amp_is_request() ) || + ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) + ) { return; } @@ -404,7 +408,30 @@ public static function wp_footer() { } /** - * Add amp-analytics. + * Add amp-analytics for Standard and Transitional mode. + * + * @see https://amp-wp.org/documentation/playbooks/analytics/ + * + * @param array $analytics_entries Analytics entries. + */ + public static function amp_analytics_entries( $analytics_entries ) { + if ( ! is_array( $analytics_entries ) ) { + $analytics_entries = array(); + } + + // Analytics script is only relevant, if "JS" tracking is enabled, to prevent double tracking. + if ( self::is_javascript_tracking_enabled() ) { + $analytics_entries['statify'] = array( + 'type' => '', + 'config' => wp_json_encode( self::make_amp_config() ), + ); + } + + return $analytics_entries; + } + + /** + * Add AMP-analytics for Reader mode. * * @see https://amp-wp.org/documentation/playbooks/analytics/ * @@ -420,32 +447,40 @@ public static function amp_post_template_analytics( $analytics ) { $analytics['statify'] = array( 'type' => '', 'attributes' => array(), - 'config_data' => array( - 'extraUrlParams' => array( - 'action' => 'statify_track', - '_ajax_nonce' => wp_create_nonce( 'statify_track' ), - 'statify_referrer' => '${documentReferrer}', - 'statify_target' => '${canonicalPath}amp/', - ), - 'requests' => array( - 'event' => admin_url( 'admin-ajax.php' ), - ), - 'triggers' => array( - 'trackPageview' => array( - 'on' => 'visible', - 'request' => 'event', - 'vars' => array( - 'eventId' => 'pageview', - ), - ), - ), - 'transport' => array( - 'xhrpost' => true, - ), - ), + 'config_data' => self::make_amp_config(), ); } return $analytics; } + + /** + * Generate AMP-analytics configuration. + * + * @return array Configuration array. + */ + private static function make_amp_config() { + return array( + 'requests' => array( + 'pageview' => admin_url( 'admin-ajax.php' ), + ), + 'extraUrlParams' => array( + 'action' => 'statify_track', + '_ajax_nonce' => wp_create_nonce( 'statify_track' ), + 'statify_referrer' => '${documentReferrer}', + 'statify_target' => '${canonicalPath}amp/', + ), + 'triggers' => array( + 'trackPageview' => array( + 'on' => 'visible', + 'request' => 'pageview', + ), + ), + 'transport' => array( + 'beacon' => true, + 'xhrpost' => true, + 'image' => false, + ), + ); + } } diff --git a/inc/class-statify.php b/inc/class-statify.php index 1fb38f5..da6a056 100644 --- a/inc/class-statify.php +++ b/inc/class-statify.php @@ -80,7 +80,9 @@ public static function init() { add_action( 'template_redirect', array( 'Statify_Frontend', 'track_visit' ) ); add_filter( 'query_vars', array( 'Statify_Frontend', 'query_vars' ) ); add_action( 'wp_footer', array( 'Statify_Frontend', 'wp_footer' ) ); - if ( function_exists( 'is_amp_endpoint' ) ) { // Automattic AMP plugin present. + if ( function_exists( 'amp_is_request' ) || function_exists( 'is_amp_endpoint' ) ) { + // Automattic AMP plugin present. + add_filter( 'amp_analytics_entries', array( 'Statify_Frontend', 'amp_analytics_entries' ) ); add_filter( 'amp_post_template_analytics', array( 'Statify_Frontend', 'amp_post_template_analytics' ) ); } }