Skip to content

Commit

Permalink
Add optimization of Monster Insights.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Dec 3, 2024
1 parent 182d52d commit 1a70841
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@
"dealerdirect/phpcodesniffer-composer-installer": true
},
"platform": {
"php": "7.0"
"php": "7.2"
}
},
"require": {
"ext-json": "*",
"tedivm/jshrink": "^v1.6.8"
"tedivm/jshrink": "^v1.7.0"
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"squizlabs/php_codesniffer": "^3.7.2",
"squizlabs/php_codesniffer": "^3.11.1",
"phpcompatibility/php-compatibility": "^9.3.5",
"phpcompatibility/phpcompatibility-wp": "^2.1.3",
"wp-coding-standards/wpcs": "^3.0.0"
"phpcompatibility/phpcompatibility-wp": "^2.1.5",
"wp-coding-standards/wpcs": "^3.1.0"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion src/php/DelayedScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public static function launch_stored_scripts(): void {
* @noinspection JSUnusedLocalSymbols
*/
private static function get_js( array $args, bool $async = true ): string {
unset( $args['async'] );
$async_string = $async ? 'true' : 'false';

$js = <<<JS
Expand All @@ -196,7 +197,7 @@ private static function get_js( array $args, bool $async = true ): string {
foreach ( $args as $key => $arg ) {
if ( 'data' === $key ) {
foreach ( $arg as $data_key => $data_arg ) {
$js .= "\t\t\ts.dataset.$data_key = '$data_arg';\n";
$js .= "\t\t\ts.setAttribute( 'data-' + '$data_key', '$data_arg' );\n";
}
continue;
}
Expand Down
1 change: 1 addition & 0 deletions src/php/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function init(): void {
new FBShareLikeButton();
new StatCounter();
new Clutch();
new MonsterInsights();
}

/**
Expand Down
81 changes: 81 additions & 0 deletions src/php/MonsterInsights.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* MonsterInsights class file.
*
* @package kagg_pagespeed_optimization
*/

namespace KAGG\PageSpeed\Optimization;

/**
* Class MonsterInsights.
*/
class MonsterInsights {

/**
* Constructor.
*/
public function __construct() {
add_action( 'plugins_loaded', [ $this, 'init_hooks' ] );
}

/**
* Init hooks.
*/
public function init_hooks(): void {
if ( ! defined( 'MONSTERINSIGHTS_VERSION' ) ) {
return;
}

add_action( 'monsterinsights_tracking_before_gtag', [ $this, 'before_gtag' ] );
add_action( 'monsterinsights_tracking_after_gtag', [ $this, 'after_gtag' ] );
}

/**
* Before gtag.
*
* @return void
*/
public function before_gtag(): void {
ob_start();
}

/**
* After gtag.
*
* @return void
*/
public function after_gtag(): void {
$g_tag = ob_get_clean();

if ( preg_match( '#<script.+googletagmanager.+?</script>#', $g_tag, $matches ) ) {
$script_tag = $matches[0];

preg_match( '#<script (.+)></script>#', $script_tag, $matches );

$script_args = $matches[1] ?? '';

preg_match_all( '#([\w|-]+)=["\']([^"\']+)["\']|([\w|-]+)#', $script_args, $matches );

$keys = array_filter( $matches[1] ) + array_filter( $matches[3] );
$values = $matches[2];
$args = array_combine( $keys, $values );
$data = [];

foreach ( $args as $key => $value ) {
if ( 0 === strpos( $key, 'data-' ) ) {
$data[ str_replace( 'data-', '', $key ) ] = $value;
unset( $args[ $key ] );
}
}

$args['data'] = $data;

$g_tag = str_replace( $script_tag, '', $g_tag );

DelayedScript::launch( $args );
}

DelayedScript::launch_html( $g_tag );
}
}

0 comments on commit 1a70841

Please sign in to comment.