Skip to content

Commit

Permalink
add "?amp" instead of "/amp/" for AMP visit tracking (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
stklcode committed Jun 24, 2021
1 parent 7889a57 commit d5ca4f2
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions inc/class-statify-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit d5ca4f2

Please sign in to comment.