From d5ca4f292fbef16b84af07cf8b5d97422589700a 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 | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index 6199dad..f45798d 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -91,15 +91,24 @@ public static function track_visit( $is_snippet = false ) { $data['referrer'] = esc_url_raw( $referrer, array( 'http', 'https' ) ); } - /* Relative target url */ + // Relative target URL. $data['target'] = user_trailingslashit( str_replace( home_url( '/', 'relative' ), '/', $target ) ); - // Trim target url. + // Trim target URL, i.e. remove query parameters. if ( $wp_rewrite->permalink_structure ) { - $data['target'] = wp_parse_url( $data['target'], PHP_URL_PATH ); + $parsed_target = wp_parse_url( $data['target'] ); + $data['target'] = $parsed_target['path']; + + /* + * Re-add AMP parameter to keep that information (only applicable for JS tracking). + * We can assume "amp" to be the only parameter, as ${canonicalPath} already eliminates other queries. + */ + if ( $is_snippet && isset( $parsed_target['query'] ) && 'amp/' === $parsed_target['query'] ) { + $data['target'] .= '?amp'; + } } - // Sanitize target url. + // Sanitize target URL. $data['target'] = esc_url_raw( $data['target'] ); // Insert. @@ -469,7 +478,7 @@ private static function make_amp_config() { 'action' => 'statify_track', '_ajax_nonce' => wp_create_nonce( 'statify_track' ), 'statify_referrer' => '${documentReferrer}', - 'statify_target' => '${canonicalPath}amp/', + 'statify_target' => '${canonicalPath}?amp', ), 'triggers' => array( 'trackPageview' => array(