Skip to content

Commit

Permalink
Merge pull request #8629 from google/enhancement/8553-icei-woocommerce
Browse files Browse the repository at this point in the history
Enhancement/8553 icei woocommerce
  • Loading branch information
tofumatt authored May 6, 2024
2 parents 15be138 + 644505f commit e023adf
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 5 deletions.
31 changes: 31 additions & 0 deletions assets/js/event-providers/woocommerce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Site Kit by Google, Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

( ( jQuery ) => {
if ( ! jQuery ) {
return;
}

const body = jQuery( 'body' );

body.on( 'added_to_cart', () => {
global.gtag( 'event', 'add_to_cart' );
} );

body.on( 'checkout_place_order_success', () => {
global.gtag( 'event', 'purchase' );
} );
} )( global.jQuery );
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Class Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\WooCommerce
*
* @package Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers
* @copyright 2024 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers;

use Google\Site_Kit\Core\Assets\Script;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Events_Provider;

/**
* Class for handling WooCommerce conversion events.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class WooCommerce extends Conversion_Events_Provider {

const CONVERSION_EVENT_PROVIDER_SLUG = 'woocommerce';

/**
* Checks if the WooCommerce plugin is active.
*
* @since n.e.x.t
*
* @return bool True if WooCommerce is active, false otherwise.
*/
public function is_active() {
return did_action( 'woocommerce_loaded' ) > 0;
}

/**
* Gets the conversion event names that are tracked by this provider.
*
* @since n.e.x.t
*
* @return array List of event names.
*/
public function get_event_names() {
return array( 'add_to_cart', 'purchase' );
}

/**
* Registers the script for the provider.
*
* @since n.e.x.t
*
* @return Script Script instance.
*/
public function register_script() {
$script = new Script(
'gsk-cep-' . self::CONVERSION_EVENT_PROVIDER_SLUG,
array(
'src' => $this->context->url( 'dist/assets/js/woocommerce.js' ),
'execution' => 'async',
'dependencies' => array( 'woocommerce' ),
)
);

$script->register( $this->context );

return $script;
}

}
8 changes: 8 additions & 0 deletions includes/Core/Conversion_Tracking/Conversion_Tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\OptinMonster;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\WooCommerce;
use LogicException;

/**
Expand All @@ -38,6 +39,7 @@ class Conversion_Tracking {
*/
public static $providers = array(
OptinMonster::CONVERSION_EVENT_PROVIDER_SLUG => OptinMonster::class,
WooCommerce::CONVERSION_EVENT_PROVIDER_SLUG => WooCommerce::class,
);

/**
Expand All @@ -60,6 +62,11 @@ public function register() {
add_action(
'wp_enqueue_scripts',
function() {
// Do nothing if neither Ads nor Analytics snippet has been inserted.
if ( ! did_action( 'googlesitekit_ads_init_tag' ) && ! did_action( 'googlesitekit_analytics-4_init_tag' ) ) {
return;
}

$active_providers = $this->get_active_providers();

array_walk(
Expand Down Expand Up @@ -125,4 +132,5 @@ public function get_active_providers() {

return $active_providers;
}

}
5 changes: 4 additions & 1 deletion includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ function() use ( $options, $activation_flag ) {
( new Core\Prompts\Prompts( $this->context, $user_options ) )->register();
( new Core\Consent_Mode\Consent_Mode( $this->context, $options ) )->register();
( new Core\Tags\GTag() )->register();
( new Core\Conversion_Tracking\Conversion_Tracking( $this->context ) )->register();

if ( Feature_Flags::enabled( 'conversionInfra' ) ) {
( new Core\Conversion_Tracking\Conversion_Tracking( $this->context ) )->register();
}

// If a login is happening (runs after 'init'), update current user in dependency chain.
add_action(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* @package Google\Site_Kit
* @copyright 2024 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Tests\Core\Conversion_Tracking\Conversion_Event_Providers;

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Assets\Script;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\WooCommerce;
use Google\Site_Kit\Tests\TestCase;

class WooCommerceTest extends TestCase {

/**
* WooCommerce instance.
*
* @var WooCommerce
*/
private $woocommerce;

public function set_up() {
parent::set_up();
$this->woocommerce = new WooCommerce( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) );
}

public function test_is_active() {
$this->assertFalse( $this->woocommerce->is_active() );
do_action( 'woocommerce_loaded' );
$this->assertTrue( $this->woocommerce->is_active() );
}

public function test_get_event_names() {
$events = $this->woocommerce->get_event_names();
$this->assertCount( 2, $events );
$this->assertEquals( 'add_to_cart', $events[0] );
$this->assertEquals( 'purchase', $events[1] );
}

public function test_register_script() {
$script = $this->woocommerce->register_script();
$this->assertInstanceOf( Script::class, $script );
$this->assertTrue( wp_script_is( 'gsk-cep-' . WooCommerce::CONVERSION_EVENT_PROVIDER_SLUG, 'registered' ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,35 @@ public function tear_down() {
Conversion_Tracking::$providers = array();
}

public function test_register() {
public function test_register__not_enqueued_when_no_snippet_inserted() {
$this->conversion_tracking->register();

do_action( 'wp_enqueue_scripts' );

$this->assertTrue( wp_script_is( 'gsk-cep-' . FakeConversionEventProvider_Active::CONVERSION_EVENT_PROVIDER_SLUG ) );
$this->assertFalse( wp_script_is( 'gsk-cep-' . FakeConversionEventProvider_Active::CONVERSION_EVENT_PROVIDER_SLUG ) );
$this->assertFalse( wp_script_is( 'gsk-cep-' . FakeConversionEventProvider::CONVERSION_EVENT_PROVIDER_SLUG ) );
}

/**
* @dataProvider data_modules
*/
public function test_register__enqueued_when_snippet_inserted( $module_slug ) {
$this->conversion_tracking->register();

do_action( "googlesitekit_{$module_slug}_init_tag" );
do_action( 'wp_enqueue_scripts' );

$this->assertTrue( wp_script_is( 'gsk-cep-' . FakeConversionEventProvider_Active::CONVERSION_EVENT_PROVIDER_SLUG ) );
$this->assertFalse( wp_script_is( 'gsk-cep-' . FakeConversionEventProvider::CONVERSION_EVENT_PROVIDER_SLUG ) );
}

public function data_modules() {
return array(
'ads_module' => array( 'ads' ),
'analytics_modules' => array( 'analytics-4' ),
);
}

public function test_get_active_conversion_event_providers() {
$active_providers = $this->conversion_tracking->get_active_providers();

Expand Down
5 changes: 3 additions & 2 deletions webpack/conversionEventProviders.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const {
module.exports = ( mode ) => ( {
entry: {
'optin-monster': './assets/js/event-providers/optin-monster.js',
woocommerce: './assets/js/event-providers/woocommerce.js',
},
externals,
output: {
Expand Down Expand Up @@ -67,8 +68,8 @@ module.exports = ( mode ) => ( {
},
plugins: [
new WebpackBar( {
name: 'Conversion Event Provider Modules',
color: '#fb1105',
name: 'Conversion Providers',
color: '#34dbeb',
} ),
new ManifestPlugin( {
...manifestArgs( mode ),
Expand Down

0 comments on commit e023adf

Please sign in to comment.