How To Delay JavaScript Execution In WordPress (With List Of Common Files)

Delay javascript wordpress

Some cache plugins don’t let you delay JavaScript (SiteGround Optimizer and Cloudways Breeze) while other plugins make you delay JS files manually (Perfmatters).

If your cache plugin doesn’t delay JavaScript, you can either use Flying Scripts (free) or Perfmatters (paid). They both let you add JS files manually while setting a timeout period.

The next step is to learn which JavaScript files you want delayed. Below is a list of common JavaScript files you can delay. This was mostly pulled from the Perfmatters documentation.

Google Analytics

ga( '
ga('
google-analytics.com/analytics.js
analytics.js
gtagv4.js
analytics-minimal.js

Google Tag Manager

/gtm.js
/gtag/js
gtag(
/gtm-

Google AdSense
*Delaying this too long can impact revenue/data. Test carefully. You will also need to delay the plugin you used to add AdSense (if applicable).

adsbygoogle.js

Google reCAPTCHA

grecaptcha.execute

Google Optimize

optimize.js

Facebook Pixel

fbevents.js
fbq(
/busting/facebook-tracking/

Specific Plugins (Only Used When They Load Below The Fold)

wp-content/plugins/plugin-name

To learn the exact files loading on your site, view your third-party code report in PSI:

Delay javascript third party code

Both Flying Script and Perfmatters let you set a timeout period in seconds. 5 seconds is a good starting point but if you’re not seeing good results in GTmetrix or PSI, try increasing the number.

Flying scripts

Gijo also created a helpful video for delaying JavaScript using Flying Scripts.

WP Rocket does this automatically, so there’s usually no reason to do anything unless there are other JS files you notice aren’t being delayed. In this case, you’ll need another plugin since there’s no option to add JS files in WP Rocket.

Wp rocket delay javascript execution

Cheers,
Tom

You Might Also Like: