Skip to content

Commit 79255c6

Browse files
committed
add "?amp" instead of "/amp/" for AMP visit tracking (#183)
1 parent 9a6d97c commit 79255c6

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

inc/class-statify-frontend.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,24 @@ public static function track_visit( $is_snippet = false ) {
9191
$data['referrer'] = esc_url_raw( $referrer, array( 'http', 'https' ) );
9292
}
9393

94-
/* Relative target url */
94+
// Relative target URL.
9595
$data['target'] = user_trailingslashit( str_replace( home_url( '/', 'relative' ), '/', $target ) );
9696

97-
// Trim target url.
97+
// Trim target URL, i.e. remove query parameters.
9898
if ( $wp_rewrite->permalink_structure ) {
99-
$data['target'] = wp_parse_url( $data['target'], PHP_URL_PATH );
99+
$parsed_target = wp_parse_url( $data['target'] );
100+
$data['target'] = $parsed_target['path'];
101+
102+
/*
103+
* Re-add AMP parameter to keep that information (only applicable for JS tracking).
104+
* We can assume "amp" to be the only parameter, as ${canonicalPath} already eliminates other queries.
105+
*/
106+
if ( $is_snippet && isset( $parsed_target['query'] ) && 'amp/' === $parsed_target['query'] ) {
107+
$data['target'] .= '?amp';
108+
}
100109
}
101110

102-
// Sanitize target url.
111+
// Sanitize target URL.
103112
$data['target'] = esc_url_raw( $data['target'] );
104113

105114
// Insert.
@@ -469,7 +478,7 @@ private static function make_amp_config() {
469478
'action' => 'statify_track',
470479
'_ajax_nonce' => wp_create_nonce( 'statify_track' ),
471480
'statify_referrer' => '${documentReferrer}',
472-
'statify_target' => '${canonicalPath}amp/',
481+
'statify_target' => '${canonicalPath}?amp',
473482
),
474483
'triggers' => array(
475484
'trackPageview' => array(

0 commit comments

Comments
 (0)