Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions assets/js/event-providers/easy-digital-downloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
],
} );
} );

if (
global._googlesitekit?.gtagUserData &&
global._googlesitekit?.edddata?.purchase?.user_data
) {
global._googlesitekit?.gtagEvent?.( 'purchase', {
user_data: global._googlesitekit.edddata.purchase.user_data,
} );
}
} )( global.jQuery );

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

use Google\Site_Kit\Core\Assets\Script;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Events_Provider;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
use Google\Site_Kit\Modules\Ads\Enhanced_Conversions;

/**
* Class for handling Easy Digital Downloads conversion events.
Expand All @@ -22,6 +25,8 @@
*/
class Easy_Digital_Downloads extends Conversion_Events_Provider {

use Method_Proxy_Trait;

const CONVERSION_EVENT_PROVIDER_SLUG = 'easy-digital-downloads';

/**
Expand All @@ -43,7 +48,13 @@ public function is_active() {
* @return array List of event names.
*/
public function get_event_names() {
return array( 'add_to_cart' );
$event_names = array( 'add_to_cart' );

if ( Feature_Flags::enabled( 'gtagUserData' ) ) {
$event_names[] = 'purchase';
}

return $event_names;
}

/**
Expand All @@ -67,4 +78,148 @@ public function register_script() {

return $script;
}

/**
* Registers hooks for the Easy Digital Downloads provider.
*
* @since n.e.x.t
*/
public function register_hooks() {
if ( Feature_Flags::enabled( 'gtagUserData' ) ) {
add_action(
'wp_footer',
$this->get_method_proxy( 'maybe_add_purchase_data_from_session' )
);
}
}

/**
* Prints the purchase data.
*
* @since n.e.x.t
*/
protected function maybe_add_purchase_data_from_session() {
if ( ! edd_is_success_page() ) {
return;
}

$purchase_session = edd_get_purchase_session();
$purchase_data = $this->get_enhanced_conversions_data_from_session( $purchase_session );

wp_add_inline_script(
'googlesitekit-events-provider-' . self::CONVERSION_EVENT_PROVIDER_SLUG,
join(
"\n",
array(
'window._googlesitekit.edddata = window._googlesitekit.edddata || {};',
sprintf( 'window._googlesitekit.edddata.purchase = %s;', wp_json_encode( $purchase_data ) ),
)
),
'before'
);
}


/**
* Extracts Enhanced Conversions data from an EDD session.
*
* @since n.e.x.t
*
* @param mixed|array|null $session An array containing EDD purchase session data.
*
* @return array
*/
protected function get_enhanced_conversions_data_from_session( $session ) {
if ( ! is_array( $session ) ) {
return array();
}

$user_data = $this->extract_user_data_from_session( $session );

if ( empty( $user_data ) ) {
return array();
}

return array(
'user_data' => $user_data,
);
}


/**
* Extracts user data from an EDD session.
*
* @since n.e.x.t
*
* @param array $session An array containing EDD purchase session data.
*
* @return array
*/
protected function extract_user_data_from_session( $session ) {
if ( isset( $session['user_info'] ) ) {
$session_info = $session['user_info'];

$email = $session_info['email'] ?? $session['user_email'];
$first_name = $session_info['first_name'];
$last_name = $session_info['last_name'];

if ( isset( $session['user_info']['address'] ) ) {
$session_address = $session_info['address'];

$phone_number = $session_address['phone'];
$street = $session_address['line1'];
$city = $session_address['city'];
$region = $session_address['state'];
$postal_code = $session_address['zip'];
$country = $session_address['country'];
}
}

$user_data = array();
$address_data = array();

if ( ! empty( $email ) ) {
$user_data['email'] = Enhanced_Conversions::get_normalized_email( $email );
}

if ( ! empty( $phone_number ) ) {
$user_data['phone_number'] = Enhanced_Conversions::get_normalized_value( $phone_number );
}

if ( ! empty( $first_name ) ) {
$address_data['first_name'] = Enhanced_Conversions::get_normalized_value( $first_name );
}

if ( ! empty( $last_name ) ) {
$address_data['last_name'] = Enhanced_Conversions::get_normalized_value( $last_name );
}

if ( ! empty( $street ) ) {
$address_data['street'] = Enhanced_Conversions::get_normalized_value( $street );
}

if ( ! empty( $city ) ) {
$address_data['city'] = Enhanced_Conversions::get_normalized_value( $city );
}

if ( ! empty( $region ) ) {
// Attempt to get full region name.
$region = edd_get_state_name( $user_data['address']['country'], $region );
$address_data['region'] = Enhanced_Conversions::get_normalized_value( $region );
}

if ( ! empty( $postal_code ) ) {
$address_data['postal_code'] = Enhanced_Conversions::get_normalized_value( $postal_code );
}

if ( ! empty( $country ) ) {
$address_data['country'] = $country;
}

if ( ! empty( $address_data ) ) {
$user_data['address'] = $address_data;
}

return $user_data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,41 @@ class Easy_Digital_DownloadsTest extends TestCase {
*
* @var Easy_Digital_Downloads
*/
private $contactform;
private $edd;

public function set_up() {
parent::set_up();
$this->contactform = new Easy_Digital_Downloads( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) );
$this->edd = new Easy_Digital_Downloads( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) );
}

/**
* @runInSeparateProcess
*/
public function test_is_active() {
$this->assertFalse( $this->contactform->is_active() );
$this->assertFalse( $this->edd->is_active() );
define( 'EDD_VERSION', 1 );
$this->assertTrue( $this->contactform->is_active() );
$this->assertTrue( $this->edd->is_active() );
}

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

public function test_get_event_names_with_gtag_enabled() {
$this->enable_feature( 'gtagUserData' );
$events = $this->edd->get_event_names();
$this->assertCount( 2, $events );
$this->assertEquals( array( 'add_to_cart', 'purchase' ), $events );
}


public function test_register_script() {
$handle = 'googlesitekit-events-provider-' . Easy_Digital_Downloads::CONVERSION_EVENT_PROVIDER_SLUG;
$this->assertFalse( wp_script_is( $handle, 'registered' ) );

$script = $this->contactform->register_script();
$script = $this->edd->register_script();
$this->assertInstanceOf( Script::class, $script );
$this->assertTrue( wp_script_is( $handle, 'registered' ) );
}
Expand Down
Loading