Skip to content

Instantly share code, notes, and snippets.

@soderlind
Last active July 17, 2025 19:20
Show Gist options
  • Save soderlind/7f1fe41ff6ac283fd5ff04c5e1caa304 to your computer and use it in GitHub Desktop.
Save soderlind/7f1fe41ff6ac283fd5ff04c5e1caa304 to your computer and use it in GitHub Desktop.

Plausible roll-up for WordPress

Also available, Plausible Stats Aggregator

If you have multiple sites and need a roll-up view, you can use the plugin below.

Prerequiste

Installation

  1. Change rollup.domain.tld to your roll-up domain
  2. Save the plugin to the wp-content/mu-plugins folder.

Copyright and License

Rollup for Plausible is copyright 2025 Per Soderlind

Rollup for Plausible is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

Rollup for Plausible is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with the Extension. If not, see http://www.gnu.org/licenses/.

<?php
/**
* Plugin name: Rollup for Plausible
* Description: Add rollup domain for Plausible Analytics plugin.
* Version: 1.0.0
* Author: Per Soderlind
* Author URI: https://soderlind.no
* License: GPL-2.0-or-later
* Plugin URI: https://gist.github.com/soderlind/7f1fe41ff6ac283fd5ff04c5e1caa304
* Requires Plugins: plausible-analytics
*
* @package Plausible
*/
if ( ! defined( 'ABSPATH' ) ) {
wp_die();
}
/**
* Class to handle Plausible Analytics rollup functionality
*/
class PlausibleRollupHandler {
/**
* Default rollup domain
*/
private const DEFAULT_ROLLUP_DOMAIN = 'rollup.domain.tld';
/**
* Initialize the rollup handler
*/
public static function init() {
if ( ! defined( 'PLAUSIBLE_ROLLUP_DOMAIN' ) ) {
define( 'PLAUSIBLE_ROLLUP_DOMAIN', self::DEFAULT_ROLLUP_DOMAIN );
}
add_filter( 'plausible_analytics_script_params', [ __CLASS__, 'modify_script_params' ] );
}
/**
* Modify the script parameters for the Plausible Analytics script
*
* @param string $params The original script parameters.
* @return string Modified script parameters with rollup domain and API URL.
*/
public static function modify_script_params( $params ) {
if ( empty( $params ) || ! is_string( $params ) ) {
return $params;
}
$domain_name = self::extract_and_modify_domain( $params );
$api_url = self::extract_api_url( $params );
if ( empty( $domain_name ) || empty( $api_url ) ) {
return $params;
}
return self::build_script_params( $domain_name, $api_url );
}
/**
* Extract and modify the domain from script parameters
*
* @param string $params The script parameters.
* @return string Modified domain with rollup domain appended.
*/
private static function extract_and_modify_domain( $params ) {
if ( ! preg_match( '/data-domain=\'([^\']+)\'/', $params, $matches ) ) {
return '';
}
$original_domain = trim( $matches[ 1 ] );
$rollup_domain = PLAUSIBLE_ROLLUP_DOMAIN;
// Avoid duplicate rollup domains
if ( strpos( $original_domain, $rollup_domain ) !== false ) {
return $original_domain;
}
return $original_domain . ',' . $rollup_domain;
}
/**
* Extract API URL from script parameters
*
* @param string $params The script parameters.
* @return string The API URL or empty string if not found.
*/
private static function extract_api_url( $params ) {
if ( ! preg_match( '/data-api=\'([^\']+)\'/', $params, $matches ) ) {
return '';
}
return trim( $matches[ 1 ] );
}
/**
* Build the complete script parameters string
*
* @param string $domain_name The domain name(s).
* @param string $api_url The API URL.
* @return string Complete script parameters string.
*/
private static function build_script_params( $domain_name, $api_url ) {
return sprintf(
"defer data-domain='%s' data-api='%s' data-cfasync='false'",
esc_attr( $domain_name ),
esc_attr( $api_url )
);
}
}
// Initialize the rollup handler
PlausibleRollupHandler::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment