How to delay non critical (external) JavaScript in WordPress. Load and execute non-critical scripts only on user interaction.
I often see external JavaScript that is non critical and with long loading times in the waterfall, slowing down the website. Mostly it is code for Google analytics, Tag Manager, Facebook pixel or Google AdSense.
We can use two plugins to delay non critical JavaScript in WordPress, Paid and free:
Perfmatters
Perfmatters is my favourite performance plugin, because it has a lot of features to speed optimize a WordPress website that a caching plugin does not do. In Perfmatters we have the option to delay JavaScripts.
You can find the options under: Options / Assets / JavaScript
Read this article how to delay JavaScript in Perfmatters
Flying Scripts
You can also use a free plugin called “Flying Scripts” to achieve this. It is also supported in “Flying Press” a caching plugin.
List of common files to delay
Below is a list of common JavaScript files you can delay.
Google Analytics
ga( '
ga('
google-analytics.com/analytics.js
Local Perfmatters gtag.js v4
gtagv4.js
Google Tag Manager
/gtm.js
/gtag/js
gtag(
/gtm-
Google AdSense
adsbygoogle.js
Facebook Pixel
fbevents.js
fbq(
/busting/facebook-tracking/
Facebook customer chat
xfbml.customerchat.js
Google reCAPTCHA
grecaptcha.execute
Google Optimize
optimize.js
Hotjar
static.hotjar.com/c/hotjar-
TikTok Analytics
analytics.tiktok.com/i18n/pixel/events.js
Specific Plugins (Only Used When They Load Below The Fold)
/wp-content/plugins/plugin-name/
WooCommerce Cart Fragments
cart-fragments.min.js
Related articles: