Skip to content

Commit 81a11b8

Browse files
write function that gets canonical path based on available public query vars on site
1 parent bf6901f commit 81a11b8

7 files changed

+52
-6
lines changed

src/class-burst-importer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function show_page(): void
2222

2323
// grab start date
2424
global $wpdb;
25-
$date_start = $wpdb->get_var("SELECT MIN(date) FROM {$wpdb->prefix}burst_summary;");
25+
$date_start = $wpdb->get_var("SELECT MIN(date) FROM {$wpdb->prefix}burst_summary");
2626
$date_end = $wpdb->get_var("SELECT MAX(date) FROM {$wpdb->prefix}burst_summary");
2727

2828
?>

src/class-pageview-aggregator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public function clean_url(string $url): string
284284
parse_str($query_str, $params);
285285

286286
// strip all but the following query parameters from the URL
287-
$allowed_params = [ 'page_id', 'p', 'cat', 'product' ];
287+
$allowed_params = [ 'page_id', 'p', 'cat', 'tag', 'product' ];
288288
$new_params = array_intersect_key($params, array_flip($allowed_params));
289289
$new_query_str = http_build_query($new_params);
290290
$new_url = substr($url, 0, $pos + 1) . $new_query_str;

src/class-script-loader.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ private static function get_post_id(): string
6363
return "-1";
6464
}
6565

66+
public static function get_canonical_path(): string
67+
{
68+
global $wp;
69+
70+
$base_path = parse_url(home_url('/'), PHP_URL_PATH);
71+
$request_path = $_SERVER['REQUEST_URI'];
72+
73+
if (str_starts_with($request_path, $base_path)) {
74+
$request_path = substr($request_path, strlen($base_path));
75+
}
76+
77+
$question_mark_pos = strpos($request_path, '?');
78+
if ($question_mark_pos !== false) {
79+
$query_string = parse_url($request_path, PHP_URL_QUERY);
80+
$params = [];
81+
parse_str($query_string, $params);
82+
$new_params = array_intersect_key($params, array_flip($wp->public_query_vars));
83+
$new_query_str = http_build_query($new_params);
84+
$request_path = rtrim(substr($request_path, 0, $question_mark_pos + 1) . $new_query_str, '?');
85+
}
86+
87+
return '/' . ltrim($request_path, '/');
88+
}
89+
6690
private static function get_tracker_url(): string
6791
{
6892
if (\defined('KOKO_ANALYTICS_CUSTOM_ENDPOINT') && KOKO_ANALYTICS_CUSTOM_ENDPOINT) {
@@ -89,7 +113,7 @@ public static function print_js_object()
89113
'url' => self::get_tracker_url(),
90114
'site_url' => get_home_url(),
91115

92-
'path' => '/' . ltrim(add_query_arg($wp->query_vars, $wp->request), '/'),
116+
'path' => self::get_canonical_path(),
93117

94118
// ID of the current post (if singular post type), path name otherwise
95119
'post_id' => (string) self::get_post_id(),

tests/FunctionsTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ public function testExtractPageviewData(): void
2525

2626
// complete but invalid
2727
$this->assertEquals(extract_pageview_data(['p' => '', 'nv' => '', 'up' => '']), []);
28-
$this->assertEquals(extract_pageview_data(['p' => 'x', 'nv' => '2', 'up' => '3']), []);
2928
$this->assertEquals(extract_pageview_data(['p' => '1', 'nv' => 'x', 'up' => '3']), []);
3029
$this->assertEquals(extract_pageview_data(['p' => '1', 'nv' => '2', 'up' => 'x']), []);
3130
$this->assertEquals(extract_pageview_data(['p' => '1', 'nv' => '2', 'up' => '3', 'r' => 'not an url']), []);
3231

3332
// complete and valid
34-
foreach (
35-
[
33+
foreach ([
3634
[['p' => '1', 'nv' => '2', 'up' => '3'], ['p', null, 1, 2, 3, '']],
35+
[['p' => '/', 'nv' => '2', 'up' => '3'], ['p', null, '/', 2, 3, '']],
3736
[['p' => '1', 'nv' => '2', 'up' => '3', 'r' => ''], ['p', null, 1, 2, 3, '']],
3837
[['p' => '1', 'nv' => '2', 'up' => '3', 'r' => 'https://www.kokoanalytics.com'], ['p', null, 1, 2, 3, 'https://www.kokoanalytics.com']],
3938

tests/ScriptLoaderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use KokoAnalytics\Script_Loader;
88
use PHPUnit\Framework\TestCase;
9+
use stdClass;
910

1011
final class ScriptLoaderTest extends TestCase
1112
{
@@ -14,4 +15,19 @@ public function testCanInstantiate(): void
1415
$i = new Script_Loader();
1516
self::assertTrue($i instanceof Script_Loader);
1617
}
18+
19+
public function test_get_canonical_path(): void
20+
{
21+
$GLOBALS['wp'] = new stdClass;
22+
$GLOBALS['wp']->public_query_vars = ['p', 'page_id', 'tag', 'cat'];
23+
24+
$_SERVER['REQUEST_URI'] = '/';
25+
self::assertEquals(Script_Loader::get_canonical_path(), '/');
26+
27+
$_SERVER['REQUEST_URI'] = '/about/?utm_medium=Email';
28+
self::assertEquals(Script_Loader::get_canonical_path(), '/about/');
29+
30+
$_SERVER['REQUEST_URI'] = '/about/?utm_medium=Email&foo=bar';
31+
self::assertEquals(Script_Loader::get_canonical_path(), '/about/');
32+
}
1733
}

tests/benchmarks/functions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ function bench(Closure $fn, $iterations = 1000)
1010
$fn();
1111
}
1212
$time_end = microtime(true);
13+
14+
// in microseconds
1315
return round(($time_end - $time_start) * 1000 * 1000, 2);
1416
}

tests/mocks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ function _deprecated_function($function, $version, $replacement)
151151
{
152152
}
153153

154+
function home_url(string $path = '/')
155+
{
156+
return 'http://localhost:8080' . $path;
157+
}
158+
154159
class wpdb_mock
155160
{
156161
public $prefix = '';

0 commit comments

Comments
 (0)