-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8629 from google/enhancement/8553-icei-woocommerce
Enhancement/8553 icei woocommerce
- Loading branch information
Showing
7 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
71 changes: 71 additions & 0 deletions
71
includes/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerce.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...punit/integration/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters