Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Leverage new Scripts API strategy attribute to improve performance #506

@adamsilverstein

Description

@adamsilverstein

Hello MonsterInsights Team!

I have been working on the WordPress core performance team helping research performance issues in by investigating blocking script usage that could leverage the new core Scripts API strategy feature.

While working on a query that uses httparchive data to see which scripts are most commonly “blocking” on WordPress sites (by "blocking" I mean in the head section and not marked as async or defer) the MonsterInsights frontend script (assets/js/[frontend-gtag.min.js) popped up as one of the top ten blocking scripts.

I am opening this ticket to ask that you will consider making this script non-blocking by adding either the defer or async attributes. (defer is usually preferable; use async if you want your script to execute as early as possible while still not blocking). Either one would provide performance benefits to your users - especially for slower device and bandwidth environments.

Switching on defer with WordPress 6.3+ is simple, add this one line of code after your enqueue:

wp_script_add_data( 'monsterinsights-frontend-script', 'strategy', 'defer' );

(or alternately async)

Even better would be an approach that also supported WordPress < 6.3 since I'm sure some of your users will be slow to upgrade. This gist provides a complete drop in code solution you can use; I'm also happy to provide a PR if that is helpful.

Starting by opening an issue to get your take on the suggestion - perhaps the plugin has some reason to have this be a blocking header script?

Please let me know what you think.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions