Below, you’ll find a detailed guide on how to add the Matomo Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Matomo Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Matomo Plugin and the shortcodes it provides:
"Matomo Analytics – Ethical Stats. Powerful Insights. is a WordPress plugin that offers ethical, privacy-friendly site analysis. It provides powerful insights to optimize your website's performance."
- [matomo_opt_out]
- [matomo_privacy_badge]
- [matomo_report]
Matomo [matomo_opt_out] Shortcode
The Matomo Opt-Out shortcode allows users to opt out of tracking. It checks the ‘DoNotTrack’ header and if enabled, displays a message. If not, it enqueues a script for opt-out.
Shortcode: [matomo_opt_out]
Parameters
Here is a list of all possible matomo_opt_out shortcode parameters and attributes:
language
– Sets the language for the opt-out message displayed to users
Examples and Usage
Basic example – A basic usage of the Matomo Opt-Out shortcode without any parameters.
[matomo_opt_out /]
Advanced examples
Using the shortcode with a language parameter. This will display the opt-out form in the specified language, if available.
[matomo_opt_out language="en" /]
Using the shortcode with an unsupported language. The opt-out form will default to English if the specified language is not supported or incorrect.
[matomo_opt_out language="xyz" /]
Note: The ‘language’ attribute accepts ISO 639-1 language codes. For example, ‘en’ for English, ‘de’ for German, ‘fr’ for French, etc. If the language code is incorrect or not supported, the opt-out form will default to English.
PHP Function Code
In case you have difficulties debugging what causing issues with [matomo_opt_out]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'matomo_opt_out', array( $this, 'show_opt_out' ) );
Shortcode PHP function:
function show_opt_out( $atts ) {
$a = shortcode_atts(
[
'language' => null,
],
$atts
);
if ( ! empty( $a['language'] ) && strlen( $a['language'] ) < 6 ) {
$this->language = $a['language'];
}
try {
Bootstrap::do_bootstrap();
} catch ( Throwable $e ) {
$logger = new Logger();
$logger->log_exception( 'optout', $e );
return '<p>An error occurred. Please check Matomo system report in WP-Admin.</p>';
}
$dnt_checker = new DoNotTrackHeaderChecker();
$dnt_enabled = $dnt_checker->isDoNotTrackFound();
if ( ! empty( $dnt_enabled ) ) {
return '<p>' . $this->translate( 'CoreAdminHome_OptOutDntFound' ) . '</p>';
}
wp_enqueue_script( 'matomo_opt_out_js' );
$track_visits = empty( $_COOKIE['mtm_consent_removed'] );
$style_tracking_enabled = '';
$style_tracking_disabled = '';
$checkbox_attr = '';
if ( $track_visits ) {
$style_tracking_enabled = 'style="display:none;"';
$checkbox_attr = 'checked="checked"';
} else {
$style_tracking_disabled = 'style="display:none;"';
}
$content = '<p id="matomo_opted_out_intro" ' . $style_tracking_enabled . '>' . $this->translate( 'CoreAdminHome_OptOutComplete' ) . ' ' . $this->translate( 'CoreAdminHome_OptOutCompleteBis' ) . '</p>';
$content .= '<p id="matomo_opted_in_intro" ' . $style_tracking_disabled . '>' . $this->translate( 'CoreAdminHome_YouMayOptOut2' ) . ' ' . $this->translate( 'CoreAdminHome_YouMayOptOut3' ) . '</p>';
$content .= '<form>
<input type="checkbox" id="matomo_optout_checkbox" ' . $checkbox_attr . '/>
<label for="matomo_optout_checkbox"><strong>
<span id="matomo_opted_in_label" ' . $style_tracking_disabled . '>' . $this->translate( 'CoreAdminHome_YouAreNotOptedOut' ) . ' ' . $this->translate( 'CoreAdminHome_UncheckToOptOut' ) . '</span>
<span id="matomo_opted_out_label" ' . $style_tracking_enabled . '>' . $this->translate( 'CoreAdminHome_YouAreOptedOut' ) . ' ' . $this->translate( 'CoreAdminHome_CheckToOptIn' ) . '</span>
</strong></label></form>';
$content .= '<noscript><p><strong style="color: #ff0000;">This opt out feature requires JavaScript.</strong></p></noscript>';
$content .= '<p id="matomo_outout_err_cookies" style="display: none;"><strong>' . $this->translate( 'CoreAdminHome_OptOutErrorNoCookies' ) . '</strong></p>';
return $content;
}
Code file location:
matomo/matomo/classes/WpMatomo/OptOut.php
Matomo [matomo_privacy_badge] Shortcode
The Matomo Privacy Badge shortcode is used to display a privacy badge on your website. It allows customization of size and alignment. The shortcode generates an image tag with the privacy badge, sourced from the plugin’s assets. The width, height, and alignment of the image can be customized using the shortcode attributes ‘size’ and ‘align’. The image displays a message: “Your privacy protected! This website uses Matomo.”
Shortcode: [matomo_privacy_badge]
Parameters
Here is a list of all possible matomo_privacy_badge shortcode parameters and attributes:
size
– sets the width and height of the privacy badge imagealign
– adjusts the alignment of the privacy badge image
Examples and Usage
Basic example – A simple usage of the Matomo Privacy Badge shortcode to display the privacy badge with default size and alignment.
[matomo_privacy_badge /]
Advanced examples
Modifying the size of the Matomo Privacy Badge by using the ‘size’ attribute. This will display a larger privacy badge with a size of 150 instead of the default size of 120.
[matomo_privacy_badge size=150 /]
Aligning the Matomo Privacy Badge to the right side of the page by using the ‘align’ attribute. This will display the privacy badge aligned to the right.
[matomo_privacy_badge align="right" /]
Combining both ‘size’ and ‘align’ attributes to display a larger privacy badge aligned to the right side of the page.
[matomo_privacy_badge size=150 align="right" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [matomo_privacy_badge]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'matomo_privacy_badge', [ $this, 'show_privacy_page' ] );
Shortcode PHP function:
function show_privacy_page( $atts ) {
$a = shortcode_atts(
[
'size' => '120',
'align' => '',
],
$atts
);
$option = sprintf( ' width="%s" height="%s"', esc_attr( $a['size'] ), esc_attr( $a['size'] ) );
if ( ! empty( $a['align'] ) ) {
$option .= sprintf( ' align="%s"', esc_attr( $a['align'] ) );
}
$url = plugins_url( 'assets/img/privacybadge.png', MATOMO_ANALYTICS_FILE );
$title = __( 'Your privacy protected! This website uses Matomo.', 'matomo' );
return sprintf( '<img alt="%s" src="%s" %s>', $title, esc_attr( $url ), $option );
}
Code file location:
matomo/matomo/classes/WpMatomo/PrivacyBadge.php
Matomo [matomo_report] Shortcode
The Matomo Report shortcode is a powerful tool for displaying custom reports. It fetches data based on the unique_id, report_date, and limit attributes. The shortcode checks user permissions, identifies the report period, and adjusts the limit for ‘visits_over_time’ reports. It then fetches the relevant report data and displays it in a table format. If no data is found, it displays a ‘no data’ message.
Shortcode: [matomo_report]
Parameters
Here is a list of all possible matomo_report shortcode parameters and attributes:
unique_id
– Identifier for the specific report typereport_date
– Date for the report, default is yesterday’s datelimit
– Maximum number of results to display, default is 10
Examples and Usage
Basic example – Display a Matomo report with the default parameters.
[matomo_report unique_id="visits_over_time" /]
Advanced examples
Display a Matomo report for a specific date, instead of the default ‘yesterday’.
[matomo_report unique_id="visits_over_time" report_date="2020-12-25" /]
Display a Matomo report with a specified limit of data points, instead of the default limit of 10.
[matomo_report unique_id="visits_over_time" limit=20 /]
Display a Matomo report for a specific date with a specified limit of data points.
[matomo_report unique_id="visits_over_time" report_date="2020-12-25" limit=20 /]
PHP Function Code
In case you have difficulties debugging what causing issues with [matomo_report]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'matomo_report', [ $this, 'show_report' ] );
Shortcode PHP function:
function show_report( $atts ) {
$a = shortcode_atts(
[
'unique_id' => '',
'report_date' => Dates::YESTERDAY,
'limit' => 10,
],
$atts
);
$cannot_view = $this->check_cannot_view();
if ( $cannot_view ) {
return $cannot_view;
}
$dates = new Dates();
list( $period, $date ) = $dates->detect_period_and_date( $a['report_date'] );
if ( 'visits_over_time' === $a['unique_id'] ) {
$is_default_limit = 10 === $a['limit'];
if ( $is_default_limit ) {
$a['limit'] = 14;
}
return $this->show_visits_over_time( $a['limit'], $period );
}
$metadata = new Metadata();
$report_meta = $metadata->find_report_by_unique_id( $a['unique_id'] );
if ( empty( $report_meta ) ) {
return sprintf( esc_html__( 'Report %s not found', 'matomo' ), esc_html( $a['unique_id'] ) );
}
$metric_keys = array_keys( $report_meta['metrics'] );
$first_metric_name = reset( $metric_keys );
$first_metric_display_name = reset( $report_meta['metrics'] );
$report_data = new Data();
$report = $report_data->fetch_report( $report_meta, $period, $date, $first_metric_name, $a['limit'] );
$has_report_data = ! empty( $report['reportData'] ) && $report['reportData']->getRowsCount();
ob_start();
if ( ! $has_report_data ) {
include 'views/table_no_data.php';
} elseif ( empty( $report_meta['dimension'] ) ) {
include 'views/table_no_dimension.php';
} else {
include 'views/table.php';
}
return ob_get_clean();
}
Code file location:
matomo/matomo/classes/WpMatomo/Report/Renderer.php
Conclusion
Now that you’ve learned how to embed the Matomo Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply