This is only a POC! Goal is to have a plugin which adds direct access to the Plausible Analytics API within a WordPress site.
NOTE: only the /stats/aggregate
API endpoint is implemented so far.
- Place the
/plausible-api
-directory inside your WP (Child-)Theme folder.- (Optional) Place the
/assets/plugins/plausible-analytics-post-insights
-directory inside your WP Plugins folder
- (Optional) Place the
- Insert your Plausible API Token to the const
PLAUSIBLE_API_APITOKEN
withinplausible-api.php
- Require the main Plugin file to get access to the Plausible Analytics API endpoints within your Theme (see examples)
- If the Post Insights-Plugin is installed: activate it via the Plugins Settings
<?php
#...
if ( false !== stream_resolve_include_path( get_stylesheet_directory() . '/assets/plausible-api/plausible-api.php' ) )
{
// Include the plausible-api Plugin
require_once( get_stylesheet_directory() . '/assets/plausible-api/plausible-api.php' );
// Get current Page's URL-Path (aka "slug") and add Slashes
$pagepath = sprintf( '/%s/', basename( get_permalink() ) );
// Query the Plausible Stats API
$pageviews = $PlausibleStatsAPI->aggregate(
['pageviews'] // metric(s)
,'custom' // date period
,get_the_date('Y-m-d', $post_id) // date period - start date
,date('Y-m-d') // date period - end date
,false // compare to previous period
,['page' => $pagepath] // filter criteria
);
// Show the API request results to the given $pagepath
echo $pageviews;
}
#...
(Make sure the Plugin is activated properly)
- Open any published Blog Post or Page
- There's a new Sidebar / Custom Panel with Plausible Analytics-Data:
Example:
2023-05-02 — 2023-07-17
Pageviews: 159
Visitors: 118
Visit Duration: ø 150 Seconds
Bounce Rate: 73.0%