Skip to content

Commit

Permalink
Add WooCommerce integration with Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Sep 27, 2024
1 parent 84fc204 commit 796996e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions plugins/web-worker-offloading/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@

require_once __DIR__ . '/helper.php';
require_once __DIR__ . '/hooks.php';
require_once __DIR__ . '/third-party.php';
12 changes: 10 additions & 2 deletions plugins/web-worker-offloading/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ Offload JavaScript execution to a Web Worker.

== Description ==

This plugin offloads JavaScript execution to a Web Worker, improving performance by freeing up the main thread.
This plugin offloads JavaScript execution to a Web Worker, improving performance by freeing up the main thread. This should translate into improved [Interaction to Next Paint](https://web.dev/articles/inp) (INP) scores. _This functionality is considered experimental._

In order to opt-in a script to be loaded in a worker, simply add `worker` script data to a registered script. For example,
In order to opt in a script to be loaded in a worker, simply add `worker` script data to a registered script. For example,
if you have a script registered with the handle of `foo`, opt-in to offload it to a web worker by doing:

`
wp_script_add_data( 'foo', 'worker', true );
`

Otherwise, the plugin currently ships with built-in integrations to offload Google Analytics to a web worker for the following plugins:

* [Rank Math SEO](https://wordpress.org/plugins/seo-by-rank-math/)
* [Site Kit by Google](https://wordpress.org/plugins/google-site-kit/)
* [WooCommerce](https://wordpress.org/plugins/woocommerce/)

Please monitor your analytics once activating to ensure all the expected events are being logged. At the same time, monitor your INP scores to check for improvement.

== Frequently Asked Questions ==

= Why are my offloaded scripts not working and I see a 404 error in the console for `partytown-sandbox-sw.html`? =
Expand Down
76 changes: 76 additions & 0 deletions plugins/web-worker-offloading/third-party.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Third-party integration loader for Web Worker Offloading.
*
* @since 0.1.0
* @package web-worker-offloading
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Adds forwarded events for Google Analytics.
*
* @since 0.1.0
* @link https://partytown.builder.io/google-tag-manager#forward-events
*
* @param array<string, mixed>|mixed $configuration Configuration.
* @return array<string, mixed> Configuration.
*/
function wwo_add_google_analytics_forwarded_events( $configuration ): array {
$configuration = (array) $configuration;

$configuration['forward'][] = 'dataLayer.push';
return $configuration;
}

/**
* Adds a script to be offloaded to a worker.
*
* @param string $script_handle Script handle.
*/
function wwo_mark_script_for_offloading( string $script_handle ): void {
add_filter(
'print_scripts_array',
static function ( $script_handles ) use ( $script_handle ) {
if ( in_array( $script_handle, (array) $script_handles, true ) ) {
wp_script_add_data( $script_handle, 'worker', true );
}
return $script_handles;
}
);
}

/**
* Loads third party plugin integrations for active plugins.
*
* @since 0.1.0
*/
function wwo_load_third_party_integrations(): void {
$plugins_with_integrations = array(
// TODO: google-site-kit.
// TODO: seo-by-rank-math.
'woocommerce',
);

// Load corresponding file for each string in $plugins if the WordPress plugin is installed and active.
$active_plugin_slugs = array_filter(
array_map(
static function ( $plugin_file ) {
if ( is_string( $plugin_file ) && str_contains( $plugin_file, '/' ) ) {
return strtok( $plugin_file, '/' );
} else {
return false;
}
},
(array) get_option( 'active_plugins' )
)
);

foreach ( array_intersect( $active_plugin_slugs, $plugins_with_integrations ) as $plugin_slug ) {
require_once __DIR__ . '/third-party/' . $plugin_slug . '.php';
}
}
add_action( 'plugins_loaded', 'wwo_load_third_party_integrations' );
15 changes: 15 additions & 0 deletions plugins/web-worker-offloading/third-party/woocommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Web Worker Offloading integration with WooCommerce.
*
* @since 0.1.0
* @package web-worker-offloading
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

add_filter( 'wwo_configuration', 'wwo_add_google_analytics_forwarded_events' );
wwo_mark_script_for_offloading( 'google-tag-manager' );
wwo_mark_script_for_offloading( 'woocommerce-google-analytics-integration-gtag' );

0 comments on commit 796996e

Please sign in to comment.