Skip to content

Commit 929231f

Browse files
authored
Merge pull request #182 from pluginkollektiv/fix/181-amp-compatibility
fix AMP compatibility for Standard and Transitional mode (#181)
2 parents 23f9e36 + 7195935 commit 929231f

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
33

4+
## 1.8.1
5+
* Fix AMP compatiblity for Standard and Transitional mode (#181) (#182)
6+
* JavaScript is no longer embedded if request is served by AMP (#181) (#182)
7+
48
## 1.8.0
59
* Fix date offset in dashboard widget in WP 5.3+ environments with mixed timezones (#167)
610
* Allow to deactivate the nonce check during JavaScript tracking (#168)

inc/class-statify-frontend.php

+61-26
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,12 @@ public static function query_vars( $vars ) {
374374
* @version 1.4.1
375375
*/
376376
public static function wp_footer() {
377-
// Skip by option.
378-
if ( ! self::is_javascript_tracking_enabled() ) {
377+
// JS tracking disabled or AMP is used for the current request.
378+
if (
379+
! self::is_javascript_tracking_enabled() ||
380+
( function_exists( 'amp_is_request' ) && amp_is_request() ) ||
381+
( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() )
382+
) {
379383
return;
380384
}
381385

@@ -404,7 +408,30 @@ public static function wp_footer() {
404408
}
405409

406410
/**
407-
* Add amp-analytics.
411+
* Add amp-analytics for Standard and Transitional mode.
412+
*
413+
* @see https://amp-wp.org/documentation/playbooks/analytics/
414+
*
415+
* @param array $analytics_entries Analytics entries.
416+
*/
417+
public static function amp_analytics_entries( $analytics_entries ) {
418+
if ( ! is_array( $analytics_entries ) ) {
419+
$analytics_entries = array();
420+
}
421+
422+
// Analytics script is only relevant, if "JS" tracking is enabled, to prevent double tracking.
423+
if ( self::is_javascript_tracking_enabled() ) {
424+
$analytics_entries['statify'] = array(
425+
'type' => '',
426+
'config' => wp_json_encode( self::make_amp_config() ),
427+
);
428+
}
429+
430+
return $analytics_entries;
431+
}
432+
433+
/**
434+
* Add AMP-analytics for Reader mode.
408435
*
409436
* @see https://amp-wp.org/documentation/playbooks/analytics/
410437
*
@@ -420,32 +447,40 @@ public static function amp_post_template_analytics( $analytics ) {
420447
$analytics['statify'] = array(
421448
'type' => '',
422449
'attributes' => array(),
423-
'config_data' => array(
424-
'extraUrlParams' => array(
425-
'action' => 'statify_track',
426-
'_ajax_nonce' => wp_create_nonce( 'statify_track' ),
427-
'statify_referrer' => '${documentReferrer}',
428-
'statify_target' => '${canonicalPath}amp/',
429-
),
430-
'requests' => array(
431-
'event' => admin_url( 'admin-ajax.php' ),
432-
),
433-
'triggers' => array(
434-
'trackPageview' => array(
435-
'on' => 'visible',
436-
'request' => 'event',
437-
'vars' => array(
438-
'eventId' => 'pageview',
439-
),
440-
),
441-
),
442-
'transport' => array(
443-
'xhrpost' => true,
444-
),
445-
),
450+
'config_data' => self::make_amp_config(),
446451
);
447452
}
448453

449454
return $analytics;
450455
}
456+
457+
/**
458+
* Generate AMP-analytics configuration.
459+
*
460+
* @return array Configuration array.
461+
*/
462+
private static function make_amp_config() {
463+
return array(
464+
'requests' => array(
465+
'pageview' => admin_url( 'admin-ajax.php' ),
466+
),
467+
'extraUrlParams' => array(
468+
'action' => 'statify_track',
469+
'_ajax_nonce' => wp_create_nonce( 'statify_track' ),
470+
'statify_referrer' => '${documentReferrer}',
471+
'statify_target' => '${canonicalPath}amp/',
472+
),
473+
'triggers' => array(
474+
'trackPageview' => array(
475+
'on' => 'visible',
476+
'request' => 'pageview',
477+
),
478+
),
479+
'transport' => array(
480+
'beacon' => true,
481+
'xhrpost' => true,
482+
'image' => false,
483+
),
484+
);
485+
}
451486
}

inc/class-statify.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public static function init() {
8080
add_action( 'template_redirect', array( 'Statify_Frontend', 'track_visit' ) );
8181
add_filter( 'query_vars', array( 'Statify_Frontend', 'query_vars' ) );
8282
add_action( 'wp_footer', array( 'Statify_Frontend', 'wp_footer' ) );
83-
if ( function_exists( 'is_amp_endpoint' ) ) { // Automattic AMP plugin present.
83+
if ( function_exists( 'amp_is_request' ) || function_exists( 'is_amp_endpoint' ) ) {
84+
// Automattic AMP plugin present.
85+
add_filter( 'amp_analytics_entries', array( 'Statify_Frontend', 'amp_analytics_entries' ) );
8486
add_filter( 'amp_post_template_analytics', array( 'Statify_Frontend', 'amp_post_template_analytics' ) );
8587
}
8688
}

0 commit comments

Comments
 (0)