From 01458e8d60d57e1c52d274aaa4f4519a7e396c57 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 29 Aug 2020 18:20:36 +0200 Subject: [PATCH] add "?amp" instead of "/amp/" for AMP visit tracking (#183) --- inc/class-statify-frontend.php | 2 +- inc/class-statify.php | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index 7153e8c..0b5c12d 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -163,7 +163,7 @@ private static function make_amp_config() { ), 'extraUrlParams' => array( 'referrer' => '${documentReferrer}', - 'target' => '${canonicalPath}amp/', + 'target' => '${canonicalPath}?amp', ), 'triggers' => array( 'trackPageview' => array( diff --git a/inc/class-statify.php b/inc/class-statify.php index 5aca13f..42db8fa 100755 --- a/inc/class-statify.php +++ b/inc/class-statify.php @@ -135,9 +135,15 @@ protected static function track( $referrer, $target ) { /* Global vars */ global $wp_rewrite; - // Trim target URL. + // Trim target URL, i.e. remove query parameters.. if ( $wp_rewrite->permalink_structure ) { - $target = wp_parse_url( $target, PHP_URL_PATH ); + $parsed_target = wp_parse_url( $target ); + $target = isset( $parsed_target['path'] ) ? $parsed_target['path'] : null; + + // Re-add AMP parameter to preserve that information (only applicable for JS tracking). + if ( isset( $parsed_target['query'] ) && 'amp/' === $parsed_target['query'] ) { + add_query_arg( 'amp', '', $target ); + } } // Init rows.