Microsoft Clarity WordPress plugin starting from version 0.10.5 started to cause delays in page loading times – from 0.5 up to 30 seconds long. While the problem has been reported in the official plugin support forum multiple times, this still has not been fixed in the most recent version of the plugin (0.10.7).
Simple Workaround to fix problem with Microsoft Clarity plugin
Place this code in your WordPress theme functions.php file or add it in a must-use plugin. This code snippet will intercept and cancel the remote requests made by Microsoft Clarity plugin, therefore returning the page loading speeds back to normal. You can use this safely while Microsoft Clarity plugin developers fixes the problem on their side:
<?php
add_filter('pre_http_request', function($preempt, $args, $url) {
if(strpos($url, 'https://ai.clarity.ms/collect-request') !== false) {
return new WP_Error( 'http_request_block', 'This request is blocked by site administrator' );
}
}, 10, 3);
?>
Why MS Clarity affects the page loading speeds?
The problem started with a plugin update in version 0.10.5 which was described as “Analytics improvements”. On the main plugin file (clarity.php) the plugin developer has added a PHP function which does a HTTP POST request (wp_remote_post function) to MS Clarity servers every time when a page on a website gets opened except the WordPress Admin side.
This probably is meant to send the data to MS Clarity servers about visitors which cannot be tracked via Javascript, however this solution is completely wrong because of the following reasons:
- A function clarity_send_request_info() is attached to the WordPress init hook which fires every time when a website gets opened. It uses wp_remote_post – an integrated WordPress function for making the HTTP POST requests, to deliver the data to MS Clarity servers, however wp_remote_post is not an asynchronous WordPress function, therefore it is causing a time delay in the PHP code execution. As these POST requests are being made to a remote server, the amount of this delay is being affected by multiple factors such as physical location of the website server and internet connection quality between the website and MS Clarity servers.
- A lot of websites do not re-compile the pages each time when they get requested / opened by a site visitor. Instead of this – these pages are being returned from so called “Page Cache” (often served from content delivery networks or a website server itself). For Microsoft Clarity plugin this means that the data about site visitors will still be inaccurate.
