Skip to content

Commit

Permalink
Add user-data in datalayer formatted for the User-Provided Data variable
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Apr 13, 2024
1 parent 87cac86 commit 69fa092
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
2 changes: 1 addition & 1 deletion assets/admin/settings.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '4bfcb42f7e1b23743b4f');
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '596c1a7b6bd34fb67624');
14 changes: 7 additions & 7 deletions assets/admin/settings.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Release date: 2024-MM-DD
Find out about what's new in our [our release post](https://gtmkit.com/gtm-kit-1-21/).

#### Enhancements:
* Added user-data in the datalayer containing user data formatted for the User-Provided Data variable so you don't have to map it manually.
* Added dynamic template sections that allows for template updates wíthout updating the plugin.
* The tutorial section is now dynamic and based on a feed, so we don't have to update the plugin to include new tutorials.

Expand Down
59 changes: 59 additions & 0 deletions src/Common/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,63 @@ public function get_data( string $endpoint, string $transient ): array {

return $data;
}

/**
* Normalize and hash a string.
*
* @link https://developers.google.com/google-ads/api/docs/conversions/enhanced-conversions/web#php
*
* @param string $hash_algorithm The hash algorithm.
* @param string $value The string to normalize and hash.
* @param bool $trim_intermediate_spaces Removes leading, trailing, and intermediate spaces.
*
* @return string The normalized and hashed string.
*/
public function normalize_and_hash(
string $hash_algorithm,
string $value,
bool $trim_intermediate_spaces
): string {
// Normalizes by first converting all characters to lowercase, then trimming spaces.
$normalized = strtolower( $value );
if ( $trim_intermediate_spaces === true ) {
// Removes leading, trailing, and intermediate spaces.
$normalized = str_replace( ' ', '', $normalized );
} else {
// Removes only leading and trailing spaces.
$normalized = trim( $normalized );
}

if ( $normalized === '' ) {
return '';
}

return hash( $hash_algorithm, strtolower( trim( $normalized ) ) );
}

/**
* Returns the result of normalizing and hashing an email address. For this use case, Google
* Ads requires removal of any '.' characters preceding "gmail.com" or "googlemail.com".
*
* @param string $hash_algorithm The hash algorithm to use.
* @param string $email_address The email address to normalize and hash.
* @return string The normalized and hashed email address.
*/
public function normalize_and_hash_email_address(
string $hash_algorithm,
string $email_address
): string {
$normalized_email = strtolower( $email_address );
$email_parts = explode( '@', $normalized_email );
if (
count( $email_parts ) > 1
&& preg_match( '/^(gmail|googlemail)\.com\s*/', $email_parts[1] )
) {
// Removes any '.' characters from the portion of the email address before the domain
// if the domain is gmail.com or googlemail.com.
$email_parts[0] = str_replace( '.', '', $email_parts[0] );
$normalized_email = sprintf( '%s@%s', $email_parts[0], $email_parts[1] );
}
return $this->normalize_and_hash( $hash_algorithm, $normalized_email, true );
}
}
10 changes: 10 additions & 0 deletions src/Integration/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,16 @@ public function include_customer_data( array $data_layer, $order_value ): array
$data_layer['ecommerce']['customer']['shipping_country'] = $wc_customer->get_shipping_country();
$data_layer['ecommerce']['customer']['shipping_state'] = $wc_customer->get_shipping_state();

$data_layer['user_data']['sha256_email_address'] = $this->util->normalize_and_hash_email_address( 'sha256', $wc_customer->get_billing_email() );
$data_layer['user_data']['sha256_phone_number'] = $this->util->normalize_and_hash( 'sha256', $wc_customer->get_billing_phone(), true );
$data_layer['user_data']['address']['sha256_first_name'] = $this->util->normalize_and_hash( 'sha256', $wc_customer->get_billing_first_name(), false );
$data_layer['user_data']['address']['sha256_last_name'] = $this->util->normalize_and_hash( 'sha256', $wc_customer->get_billing_last_name(), false );
$data_layer['user_data']['address']['street'] = $wc_customer->get_billing_address_1();
$data_layer['user_data']['address']['city'] = $wc_customer->get_billing_city();
$data_layer['user_data']['address']['region'] = $wc_customer->get_billing_state();
$data_layer['user_data']['address']['postal_code'] = $wc_customer->get_billing_postcode();
$data_layer['user_data']['address']['country'] = $wc_customer->get_billing_country();

return $data_layer;
}

Expand Down

0 comments on commit 69fa092

Please sign in to comment.