Skip to content

Commit

Permalink
expand dashboard widget to include realtime pageviews and most viewed…
Browse files Browse the repository at this point in the history
… posts over last 2 weeks
  • Loading branch information
dannyvankooten committed Oct 26, 2023
1 parent 1f8f607 commit 9a1f956
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
14 changes: 6 additions & 8 deletions src/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ public function maybe_load_standalone(): void
}

$this->register_scripts();

// Maybe we can get rid of wp-polyfill?
// global $wp_scripts;
// $wp_scripts->registered['wp-i18n']->deps = ['wp-hooks'];
// $wp_scripts->registered['wp-hooks']->deps = [];

require KOKO_ANALYTICS_PLUGIN_DIR . '/views/standalone.php';
exit;
}
Expand Down Expand Up @@ -238,8 +232,12 @@ public function register_dashboard_widget(): void

public function dashboard_widget(): void
{
echo '<div id="koko-analytics-dashboard-widget-mount"></div>';
echo sprintf('<p class="help" style="text-align: center;">%s &mdash; <a href="%s">%s</a></p>', esc_html__('Showing site visits over last 14 days', 'koko-analytics'), esc_attr(admin_url('index.php?page=koko-analytics')), esc_html__('View all statistics', 'koko-analytics'));
$stats = new Stats();
$dateStart = new \DateTime('-14 days');
$dateEnd = new \DateTimeImmutable('now');
$realtime = get_realtime_pageview_count('-1 hour');
$posts = $stats->get_posts($dateStart->format('Y-m-d'), $dateEnd->format('Y-m-d'), 0, 5);
require KOKO_ANALYTICS_PLUGIN_DIR . '/views/dashboard-widget.php';
}

/**
Expand Down
22 changes: 1 addition & 21 deletions src/class-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,33 +197,13 @@ public function get_totals(\WP_REST_Request $request): \WP_REST_Response
*/
public function get_posts(\WP_REST_Request $request): \WP_REST_Response
{
global $wpdb;
$send_cache_headers = WP_DEBUG === false && $this->is_request_for_completed_date_range($request);

$params = $request->get_query_params();
$start_date = $params['start_date'] ?? gmdate('Y-m-d', strtotime('1st of this month') + get_option('gmt_offset', 0) * HOUR_IN_SECONDS);
$end_date = $params['end_date'] ?? gmdate('Y-m-d', time() + get_option('gmt_offset', 0) * HOUR_IN_SECONDS);
$offset = isset($params['offset']) ? absint($params['offset']) : 0;
$limit = isset($params['limit']) ? absint($params['limit']) : 10;
$sql = $wpdb->prepare("SELECT s.id, SUM(visitors) AS visitors, SUM(pageviews) AS pageviews, COALESCE(NULLIF(p.post_title, ''), p.post_name) AS post_title FROM {$wpdb->prefix}koko_analytics_post_stats s LEFT JOIN {$wpdb->posts} p ON p.ID = s.id WHERE s.date >= %s AND s.date <= %s GROUP BY s.id ORDER BY pageviews DESC, s.id ASC LIMIT %d, %d", array( $start_date, $end_date, $offset, $limit ));
$results = $wpdb->get_results($sql);

// add permalink to each result
$results = array_map(function ($row) {
// special handling of records with ID 0 (indicates a view of the front page when front page is not singular)
if ($row->id == 0) {
$row->post_permalink = home_url();
$row->post_title = get_bloginfo('name');
} else {
/* TODO: Optimize this */
$row->post_permalink = get_permalink($row->id);
}

$row->pageviews = (int) $row->pageviews;
$row->visitors = (int) $row->visitors;
return $row;
}, $results);

$results = (new Stats())->get_posts($start_date, $end_date, $offset, $limit);
return $this->respond($results, $send_cache_headers);
}

Expand Down
21 changes: 21 additions & 0 deletions src/class-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,25 @@ public function get_totals(string $start_date, string $end_date): ?object
", array( $start_date, $end_date, $previous_start_date, $start_date ));
return $wpdb->get_row($sql);
}

public function get_posts(string $start_date, string $end_date, int $offset = 0, int $limit = 10): array
{
global $wpdb;
$sql = $wpdb->prepare("SELECT s.id, SUM(visitors) AS visitors, SUM(pageviews) AS pageviews, COALESCE(NULLIF(p.post_title, ''), p.post_name) AS post_title FROM {$wpdb->prefix}koko_analytics_post_stats s LEFT JOIN {$wpdb->posts} p ON p.ID = s.id WHERE s.date >= %s AND s.date <= %s GROUP BY s.id ORDER BY pageviews DESC, s.id ASC LIMIT %d, %d", array( $start_date, $end_date, $offset, $limit ));
$results = $wpdb->get_results($sql);
return array_map(function ($row) {
// special handling of records with ID 0 (indicates a view of the front page when front page is not singular)
if ($row->id == 0) {
$row->post_permalink = home_url();
$row->post_title = get_bloginfo('name');
} else {
/* TODO: Optimize this */
$row->post_permalink = get_permalink($row->id);
}

$row->pageviews = (int) $row->pageviews;
$row->visitors = (int) $row->visitors;
return $row;
}, $results);
}
}
38 changes: 38 additions & 0 deletions views/dashboard-widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php defined('ABSPATH') or exit;

/**
* @var int $realtime
* @var array $posts
*/

?>
<style>
.ka-ul { margin: 0 -12px; }
.ka-ul li { padding: 4px 12px; }
.ka-ul li:nth-child(2n+1) { background-color: #f6f7f7; }
</style>
<h3><?php echo esc_html__('Realtime', 'koko-analytics'); ?></h3>
<p>
<?php echo sprintf(esc_html__('Your site received %s pageviews in the last hour', 'koko-analytics'), '<strong>' . $realtime . '</strong>'); ?>
</p>
<h3 style="margin-top: 2em;">
<?php echo esc_html__('Showing site visits over last 14 days', 'koko-analytics'); ?>
</h3>
<div id="koko-analytics-dashboard-widget-mount"></div>
<h3 style="margin-top: 2em;">
<?php echo esc_html__('Most viewed pages over last 14 days', 'koko-analytics'); ?>
<span style="float: right;"><?php echo esc_html__('Pageviews', 'koko-analytics'); ?></span>
</h3>
<ul class="ka-ul">
<?php foreach ($posts as $post) { ?>
<li>
<a href="<?php echo esc_attr($post->post_permalink); ?>"><?php echo esc_html($post->post_title); ?></a>
<span style="float: right;"><?php echo esc_html($post->pageviews); ?></span>
</li>
<?php } ?>
</ul>
<p style="margin-top: 2em;">
<a href="<?php echo esc_attr(admin_url('index.php?page=koko-analytics')); ?>">
<?php echo esc_html__('View all statistics', 'koko-analytics'); ?>
</a>
</p>

0 comments on commit 9a1f956

Please sign in to comment.