Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix AMP compatibility for Standard and Transitional mode (#181) #182

Merged
merged 3 commits into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
87 changes: 61 additions & 26 deletions inc/class-statify-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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/
*
Expand All @@ -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,
),
);
}
}
4 changes: 3 additions & 1 deletion inc/class-statify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
}
Expand Down