-
Notifications
You must be signed in to change notification settings - Fork 21
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 #2734 from woocommerce/fix/2733-prevent-duplicate-…
…purchase-event-tracking Prevent duplicate conversion and purchase event tracking
- Loading branch information
Showing
4 changed files
with
140 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Google; | ||
|
||
use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AssetsHandlerInterface; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Google\GlobalSiteTag; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\GoogleGtagJs; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\WC; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\WP; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\UnitTest; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use WC_Helper_Order; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* Class GlobalSiteTagTest | ||
* | ||
* @package Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Google | ||
*/ | ||
class GlobalSiteTagTest extends UnitTest { | ||
|
||
/** @var MockObject|AssetsHandlerInterface $assets_handler */ | ||
protected $assets_handler; | ||
|
||
/** @var MockObject|GoogleGtagJs $gtag_js */ | ||
protected $gtag_js; | ||
|
||
/** @var MockObject|ProductHelper $product_helper */ | ||
protected $product_helper; | ||
|
||
/** @var MockObject|WC $wc */ | ||
protected $wc; | ||
|
||
/** @var MockObject|WP $wp */ | ||
protected $wp; | ||
|
||
/** @var GlobalSiteTag $tag */ | ||
protected $tag; | ||
|
||
protected const TEST_CONVERSION_ID = 'test_id'; | ||
protected const TEST_CONVERSION_LABEL = 'test_conversion_label'; | ||
|
||
/** | ||
* Runs before each test is executed. | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->assets_handler = $this->createMock( AssetsHandlerInterface::class ); | ||
$this->gtag_js = $this->createMock( GoogleGtagJs::class ); | ||
$this->product_helper = $this->createMock( ProductHelper::class ); | ||
$this->wc = $this->createMock( WC::class ); | ||
$this->wp = $this->createMock( WP::class ); | ||
|
||
$this->tag = new GlobalSiteTag( $this->assets_handler, $this->gtag_js, $this->product_helper, $this->wc, $this->wp ); | ||
} | ||
|
||
public function test_conversion_and_purchase_event_not_order_received_page() { | ||
add_filter( 'woocommerce_is_order_received_page', '__return_false' ); | ||
$this->wp->expects( $this->never() )->method( 'wp_print_inline_script_tag' ); | ||
|
||
$this->tag->maybe_display_conversion_and_purchase_event_snippets( self::TEST_CONVERSION_ID, self::TEST_CONVERSION_LABEL, 0 ); | ||
} | ||
|
||
public function test_conversion_and_purchase_event_no_order() { | ||
add_filter( 'woocommerce_is_order_received_page', '__return_true' ); | ||
$this->wp->expects( $this->never() )->method( 'wp_print_inline_script_tag' ); | ||
|
||
$this->tag->maybe_display_conversion_and_purchase_event_snippets( self::TEST_CONVERSION_ID, self::TEST_CONVERSION_LABEL, 0 ); | ||
} | ||
|
||
public function test_conversion_and_purchase_event_already_tracked() { | ||
add_filter( 'woocommerce_is_order_received_page', '__return_true' ); | ||
|
||
$order = WC_Helper_Order::create_order(); | ||
$order->update_meta_data( '_gla_tracked', 1 ); | ||
$order->save_meta_data(); | ||
|
||
$this->wp->expects( $this->never() )->method( 'wp_print_inline_script_tag' ); | ||
|
||
$this->tag->maybe_display_conversion_and_purchase_event_snippets( self::TEST_CONVERSION_ID, self::TEST_CONVERSION_LABEL, $order->get_id() ); | ||
} | ||
|
||
public function test_conversion_and_purchase_event() { | ||
add_filter( 'woocommerce_is_order_received_page', '__return_true' ); | ||
|
||
$order = WC_Helper_Order::create_order(); | ||
|
||
$invoked_count = $this->exactly( 2 ); | ||
$this->wp->expects( $invoked_count ) | ||
->method( 'wp_print_inline_script_tag' ) | ||
->willReturnCallback( | ||
function ( string $script ) use ( $invoked_count ) { | ||
if ( 1 === $invoked_count->getInvocationCount() ) { | ||
$this->assertStringStartsWith( 'gtag("event", "conversion"', $script ); | ||
} | ||
|
||
if ( 2 === $invoked_count->getInvocationCount() ) { | ||
$this->assertStringStartsWith( 'gtag("event", "purchase"', $script ); | ||
} | ||
} | ||
); | ||
|
||
$this->tag->maybe_display_conversion_and_purchase_event_snippets( self::TEST_CONVERSION_ID, self::TEST_CONVERSION_LABEL, $order->get_id() ); | ||
|
||
// Reload order and confirm tracked meta is set. | ||
$order = wc_get_order( $order->get_id() ); | ||
$this->assertSame( 1, (int) $order->get_meta( '_gla_tracked', true ) ); | ||
} | ||
} |
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