Skip to content

Commit

Permalink
include new data and meta tracking data in PHP and JS
Browse files Browse the repository at this point in the history
  • Loading branch information
2ndkauboy committed Mar 26, 2023
1 parent 9e2c01b commit 210a515
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
15 changes: 8 additions & 7 deletions inc/class-statify-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ public static function track_visit( $request ) {
}
}

$referrer = $request->get_param( 'referrer' );
if ( null !== $referrer ) {
$referrer = filter_var( $referrer, FILTER_SANITIZE_URL );
$tracking_data = $request->get_param( 'tracking_data' );
if ( null !== $tracking_data['referrer'] ) {
$tracking_data['referrer'] = filter_var( $tracking_data['referrer'], FILTER_SANITIZE_URL );
}
$target = $request->get_param( 'target' );
if ( null !== $target ) {
$target = filter_var( $target, FILTER_SANITIZE_URL );
if ( null !== $tracking_data['target'] ) {
$tracking_data['target'] = filter_var( $tracking_data['target'], FILTER_SANITIZE_URL );
}

Statify::track( $referrer, $target );
$tracking_meta = $request->get_param( 'tracking_meta' );

Statify::track( $tracking_data, $tracking_meta );
}

return new WP_REST_Response( null, 204 );
Expand Down
14 changes: 3 additions & 11 deletions inc/class-statify-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,7 @@ public static function track_visit( $is_snippet = false ) {
return false;
}

// Set target & referrer.
$target = null;
$referrer = null;
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$target = filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
}
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
$referrer = filter_var( wp_unslash( $_SERVER['HTTP_REFERER'] ), FILTER_SANITIZE_URL );
}

Statify::track( $referrer, $target );
Statify::track( self::$tracking_data, self::$tracking_meta );
}

/**
Expand Down Expand Up @@ -146,6 +136,8 @@ public static function wp_footer() {
// Add endpoint to script.
$script_data = array(
'url' => esc_url_raw( rest_url( Statify_Api::REST_NAMESPACE . '/' . Statify_Api::REST_ROUTE_TRACK ) ),
'tracking_data' => self::$tracking_data,
'tracking_meta' => wp_list_pluck( self::$tracking_meta, 'meta_value', 'meta_key' ),
);
if ( Statify::TRACKING_METHOD_JAVASCRIPT_WITH_NONCE_CHECK === self::$_options['snippet'] ) {
$script_data['nonce'] = wp_create_nonce( 'statify_track' );
Expand Down
36 changes: 17 additions & 19 deletions inc/class-statify.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,30 @@ public static function init() {
/**
* Track the page view.
*
* @param string|null $referrer Referrer URL.
* @param string|null $target Target URL.
* @param array $tracking_data Tracking data.
* @param array $tracking_meta Tracking meta.
*
* @return void
*
* @since 0.1.0
* @since 1.7.0 $is_snippet parameter added.
* @since 2.0.0 Migration from Statify_Frontend::track_visit to Statify::track with multiple parameters.
*/
protected static function track( $referrer, $target ) {
protected static function track( $tracking_data, $tracking_meta ) {
if ( empty( Statify_Frontend::get_tracking_data() ) ) {
Statify_Frontend::init_tracking_data();
}

// Fallbacks for uninitialized or omitted target and referrer values.
if ( is_null( $target ) ) {
$target = '/';
if ( is_null( $tracking_data['target'] ) ) {
$tracking_data['target'] = '/';
}
if ( is_null( $referrer ) ) {
$referrer = '';
if ( is_null( $tracking_data['referrer'] ) ) {
$tracking_data['referrer'] = '';
}

// Invalid target?
if ( empty( $target ) || ! wp_validate_redirect( $target, false ) ) {
if ( empty( $tracking_data['target'] ) || ! wp_validate_redirect( $tracking_data['target'], false ) ) {
return;
}

Expand All @@ -128,28 +128,28 @@ protected static function track( $referrer, $target ) {
}

// Sanitize referrer url.
if ( ! empty( $referrer ) && false === self::strposa( $referrer, array( home_url(), network_admin_url() ) ) ) {
$referrer = esc_url_raw( $referrer, array( 'http', 'https' ) );
if ( ! empty( $tracking_data['referrer'] ) && false === self::strposa( $tracking_data['referrer'], array( home_url(), network_admin_url() ) ) ) {
$tracking_data['referrer'] = esc_url_raw( $tracking_data['referrer'], array( 'http', 'https' ) );
} else {
$referrer = '';
$tracking_data['referrer'] = '';
}

// Relative target URL.
$target = user_trailingslashit( str_replace( home_url( '/', 'relative' ), '/', $target ) );
$tracking_data['target'] = user_trailingslashit( str_replace( home_url( '/', 'relative' ), '/', $tracking_data['target'] ) );

/* Global vars */
global $wp_rewrite;

// Trim target URL.
if ( $wp_rewrite->permalink_structure ) {
$target = wp_parse_url( $target, PHP_URL_PATH );
$tracking_data['target'] = wp_parse_url( $tracking_data['target'], PHP_URL_PATH );
}

// Init rows.
$data = array(
'created' => current_time( 'Y-m-d' ),
'referrer' => $referrer,
'target' => $target,
'created' => current_time( 'Y-m-d' ),
'referrer' => $tracking_data['referrer'],
'target' => $tracking_data['target'],
);

// Insert.
Expand All @@ -158,9 +158,7 @@ protected static function track( $referrer, $target ) {

$statify_id = $wpdb->insert_id;

$tracking_meta = Statify_Frontend::get_tracking_data();

foreach ( self::$tracking_meta as $meta_field ) {
foreach ( Statify_Frontend::get_tracking_data() as $meta_field ) {
if ( array_key_exists( $meta_field['meta_key'], $tracking_meta ) ) {
$meta_value = $tracking_meta[ $meta_field['meta_key'] ];

Expand Down
4 changes: 2 additions & 2 deletions js/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
statifyReq.open( 'POST', statifyAjax.url, true );
statifyReq.setRequestHeader( 'Content-Type', 'application/json' );
data = {
referrer: document.referrer,
target: location.pathname + location.search,
tracking_data: statifyAjax.tracking_data,
tracking_meta: statifyAjax.tracking_meta,
};
if ( 'nonce' in statifyAjax ) {
data.nonce = statifyAjax.nonce;
Expand Down

0 comments on commit 210a515

Please sign in to comment.