Skip to content

Commit b2b034d

Browse files
Add EDD Enhanced Conversions integration.
1 parent e2f17d7 commit b2b034d

File tree

3 files changed

+168
-2
lines changed

3 files changed

+168
-2
lines changed

assets/js/event-providers/easy-digital-downloads.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
],
3838
} );
3939
} );
40+
41+
if (
42+
global._googlesitekit?.gtagUserData &&
43+
global._googlesitekit?.edddata?.purchase?.user_data
44+
) {
45+
global._googlesitekit?.gtagEvent?.( 'purchase', {
46+
user_data: global._googlesitekit.edddata.purchase.user_data,
47+
} );
48+
}
4049
} )( global.jQuery );
4150

4251
/**

includes/Core/Conversion_Tracking/Conversion_Event_Providers/Easy_Digital_Downloads.php

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
use Google\Site_Kit\Core\Assets\Script;
1414
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Events_Provider;
15+
use Google\Site_Kit\Core\Util\Feature_Flags;
16+
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
17+
use Google\Site_Kit\Modules\Ads\Enhanced_Conversions;
1518

1619
/**
1720
* Class for handling Easy Digital Downloads conversion events.
@@ -22,6 +25,8 @@
2225
*/
2326
class Easy_Digital_Downloads extends Conversion_Events_Provider {
2427

28+
use Method_Proxy_Trait;
29+
2530
const CONVERSION_EVENT_PROVIDER_SLUG = 'easy-digital-downloads';
2631

2732
/**
@@ -43,7 +48,13 @@ public function is_active() {
4348
* @return array List of event names.
4449
*/
4550
public function get_event_names() {
46-
return array( 'add_to_cart' );
51+
$event_names = array( 'add_to_cart' );
52+
53+
if ( Feature_Flags::enabled( 'gtagUserData' ) ) {
54+
$event_names[] = 'purchase';
55+
}
56+
57+
return $event_names;
4758
}
4859

4960
/**
@@ -67,4 +78,143 @@ public function register_script() {
6778

6879
return $script;
6980
}
81+
82+
/**
83+
* Registers hooks for the Easy Digital Downloads provider.
84+
*
85+
* @since n.e.x.t
86+
*/
87+
public function register_hooks() {
88+
if ( Feature_Flags::enabled( 'gtagUserData' ) ) {
89+
add_action(
90+
'wp_footer',
91+
$this->get_method_proxy( 'maybe_add_purchase_data_from_session' )
92+
);
93+
}
94+
}
95+
96+
/**
97+
* Prints the purchase data.
98+
*
99+
* @since n.e.x.t
100+
*/
101+
protected function maybe_add_purchase_data_from_session() {
102+
if ( ! edd_is_success_page() ) {
103+
return;
104+
}
105+
106+
$purchase_session = edd_get_purchase_session();
107+
$purchase_data = $this->get_enhanced_conversions_data_from_session( $purchase_session );
108+
109+
wp_add_inline_script(
110+
'googlesitekit-events-provider-' . self::CONVERSION_EVENT_PROVIDER_SLUG,
111+
join(
112+
"\n",
113+
array(
114+
'window._googlesitekit.edddata = window._googlesitekit.edddata || {};',
115+
sprintf( 'window._googlesitekit.edddata.purchase = %s;', wp_json_encode( $purchase_data ) ),
116+
)
117+
),
118+
'before'
119+
);
120+
}
121+
122+
123+
/**
124+
* Extracts Enhanced Conversions data from an EDD session.
125+
*
126+
* @since n.e.x.t
127+
*
128+
* @param mixed|array|null $session An array containing EDD purchase session data.
129+
*
130+
* @return array
131+
*/
132+
protected function get_enhanced_conversions_data_from_session( $session ) {
133+
if ( ! is_array( $session ) ) {
134+
return array();
135+
}
136+
137+
return array(
138+
'user_data' => $this->extract_user_data_from_session( $session ),
139+
);
140+
}
141+
142+
143+
/**
144+
* Extracts user data from an EDD session.
145+
*
146+
* @since n.e.x.t
147+
*
148+
* @param array $session An array containing EDD purchase session data.
149+
*
150+
* @return array
151+
*/
152+
protected function extract_user_data_from_session( $session ) {
153+
154+
if ( isset( $session['user_info'] ) ) {
155+
$session_info = $session['user_info'];
156+
157+
$email = $session_info['email'] ?? $session['user_email'];
158+
$first_name = $session_info['first_name'];
159+
$last_name = $session_info['last_name'];
160+
161+
if ( isset( $session['user_info']['address'] ) ) {
162+
$session_address = $session_info['address'];
163+
164+
$phone_number = $session_address['phone'];
165+
$street = $session_address['line1'];
166+
$city = $session_address['city'];
167+
$region = $session_address['state'];
168+
$postal_code = $session_address['zip'];
169+
$country = $session_address['country'];
170+
}
171+
}
172+
173+
$user_data = array();
174+
$address_data = array();
175+
176+
if ( ! empty( $email ) ) {
177+
$user_data['email'] = Enhanced_Conversions::get_normalized_email( $email );
178+
}
179+
180+
if ( ! empty( $phone_number ) ) {
181+
$user_data['phone_number'] = Enhanced_Conversions::get_normalized_value( $phone_number );
182+
}
183+
184+
if ( ! empty( $first_name ) ) {
185+
$address_data['first_name'] = Enhanced_Conversions::get_normalized_value( $first_name );
186+
}
187+
188+
if ( ! empty( $last_name ) ) {
189+
$address_data['last_name'] = Enhanced_Conversions::get_normalized_value( $last_name );
190+
}
191+
192+
if ( ! empty( $street ) ) {
193+
$address_data['street'] = Enhanced_Conversions::get_normalized_value( $street );
194+
}
195+
196+
if ( ! empty( $city ) ) {
197+
$address_data['city'] = Enhanced_Conversions::get_normalized_value( $city );
198+
}
199+
200+
if ( ! empty( $region ) ) {
201+
// Attempt to get full region name.
202+
$region = edd_get_state_name( $user_data['address']['country'], $region );
203+
$address_data['region'] = Enhanced_Conversions::get_normalized_value( $region );
204+
}
205+
206+
if ( ! empty( $postal_code ) ) {
207+
$address_data['postal_code'] = Enhanced_Conversions::get_normalized_value( $postal_code );
208+
}
209+
210+
if ( ! empty( $country ) ) {
211+
$address_data['country'] = $country;
212+
}
213+
214+
if ( ! empty( $address_data ) ) {
215+
$user_data['address'] = $address_data;
216+
}
217+
218+
return $user_data;
219+
}
70220
}

tests/phpunit/integration/Core/Conversion_Tracking/Conversion_Event_Providers/Easy_Digital_DownloadsTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,19 @@ public function test_is_active() {
3838
$this->assertTrue( $this->contactform->is_active() );
3939
}
4040

41-
public function test_get_event_names() {
41+
public function test_get_event_names_with_gtag_disabled() {
4242
$events = $this->contactform->get_event_names();
4343
$this->assertCount( 1, $events );
4444
$this->assertEquals( 'add_to_cart', $events[0] );
4545
}
4646

47+
public function test_get_event_names_with_gtag_enabled() {
48+
$this->enable_feature( 'gtagUserData' );
49+
$events = $this->contactform->get_event_names();
50+
$this->assertCount( 2, $events );
51+
$this->assertEquals( array( 'add_to_cart', 'purchase' ), $events );
52+
}
53+
4754
public function test_register_script() {
4855
$handle = 'googlesitekit-events-provider-' . Easy_Digital_Downloads::CONVERSION_EVENT_PROVIDER_SLUG;
4956
$this->assertFalse( wp_script_is( $handle, 'registered' ) );

0 commit comments

Comments
 (0)