Skip to content

Added: defer plausible.js using native wp-data-strategy attribute. #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ public function maybe_register_assets() {
* Bail if tracked_user_roles is empty (which means no roles should be tracked) or,
* if current role should not be tracked.
*/
if ( ( ! empty( $user_role ) && ! isset( $settings[ 'tracked_user_roles' ] ) ) ||
( ! empty( $user_role ) && ! in_array( $user_role, $settings[ 'tracked_user_roles' ], true ) ) ) {
if ( ( ! empty( $user_role ) && ! isset( $settings[ 'tracked_user_roles' ] ) ) || ( ! empty( $user_role ) && ! in_array( $user_role, $settings[ 'tracked_user_roles' ], true ) ) ) {
return; // @codeCoverageIgnore
}

$version =
Helpers::proxy_enabled() && file_exists( Helpers::get_js_path() ) ? filemtime( Helpers::get_js_path() ) :
PLAUSIBLE_ANALYTICS_VERSION;
$version = Helpers::proxy_enabled() && file_exists( Helpers::get_js_path() ) ? filemtime( Helpers::get_js_path() ) : PLAUSIBLE_ANALYTICS_VERSION;

wp_enqueue_script(
'plausible-analytics',
Helpers::get_js_url( true ),
'',
$version,
apply_filters( 'plausible_load_js_in_footer', false )
[
'in_footer' => apply_filters( 'plausible_load_js_in_footer', false ),
'strategy' => 'defer',
]
);

// Goal tracking inline script (Don't disable this as it is required by 404).
Expand Down Expand Up @@ -131,13 +131,13 @@ public function admin_bar_node( $admin_bar ) {
return; // @codeCoverageIgnore
}

$settings = Helpers::get_settings();
$settings = Helpers::get_settings();
$current_user = wp_get_current_user();

$has_access = false;
$user_roles_have_access = array_merge(
[ 'administrator' ],
$settings['expand_dashboard_access'] ?? []
$settings[ 'expand_dashboard_access' ] ?? []
);

foreach ( $current_user->roles as $role ) {
Expand All @@ -157,9 +157,7 @@ public function admin_bar_node( $admin_bar ) {
'title' => 'Plausible Analytics',
];


if ( ! empty( $settings[ 'enable_analytics_dashboard' ] ) ||
( ! empty( $settings[ 'self_hosted_domain' ] ) && ! empty( $settings[ 'self_hosted_shared_link' ] ) ) ) {
if ( ! empty( $settings[ 'enable_analytics_dashboard' ] ) || ( ! empty( $settings[ 'self_hosted_domain' ] ) && ! empty( $settings[ 'self_hosted_shared_link' ] ) ) ) {
$args[] = [
'id' => 'view-analytics',
'title' => esc_html__( 'View Analytics', 'plausible-analytics' ),
Expand Down
2 changes: 1 addition & 1 deletion src/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function add_plausible_attributes( $tag, $handle ) {
/**
* the data-cfasync ensures this script isn't processed by CF Rocket Loader @see https://developers.cloudflare.com/speed/optimization/content/rocket-loader/ignore-javascripts/
*/
$params = "defer data-domain='{$domain_name}' data-api='{$api_url}' data-cfasync='false'";
$params = "data-domain='{$domain_name}' data-api='{$api_url}' data-cfasync='false'";

// Triggered when exclude pages is enabled.
if ( ! empty( $settings[ 'excluded_pages' ] ) && $settings[ 'excluded_pages' ] ) {
Expand Down
Loading