er->get_cart_tax() ),
$order->get_total_shipping(),
esc_js( $order->get_billing_postcode() ),
esc_js( $this->wc->get_base_country() ),
esc_js( $language ),
join( ',', $item_info ),
);
$this->add_inline_event_script( $purchase_page_gtag );
}
/**
* Display the JavaScript code to track the product view page.
*/
private function display_view_item_event_snippet(): void {
$product = wc_get_product( get_the_ID() );
if ( ! $product instanceof WC_Product ) {
return;
}
$this->add_product_data( $product );
$view_item_gtag = sprintf(
'gtag("event", "view_item", {
send_to: "GLA",
ecomm_pagetype: "product",
value: %f,
items:[{
id: "gla_%s",
price: %f,
google_business_vertical: "retail",
name: "%s",
category: "%s",
}]});',
wc_get_price_to_display( $product ),
esc_js( $product->get_id() ),
wc_get_price_to_display( $product ),
esc_js( $product->get_name() ),
esc_js( join( ' & ', $this->product_helper->get_categories( $product ) ) ),
);
$this->add_inline_event_script( $view_item_gtag );
}
/**
* Display the JavaScript code to track all pages.
*/
private function display_page_view_event_snippet(): void {
if ( ! is_cart() ) {
$this->add_inline_event_script(
'gtag("event", "page_view", {send_to: "GLA"});'
);
return;
}
// display the JavaScript code to track the cart page
$item_info = [];
foreach ( WC()->cart->get_cart() as $cart_item ) {
// gets the product id
$id = ! empty( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : $cart_item['product_id'];
// gets the product object
$product = $cart_item['data'];
$name = $product->get_name();
$price = WC()->cart->display_prices_including_tax() ? wc_get_price_including_tax( $product ) : wc_get_price_excluding_tax( $product );
// gets the cart item quantity
$quantity = $cart_item['quantity'];
$item_info[] = sprintf(
'{
id: "gla_%s",
price: %f,
google_business_vertical: "retail",
name:"%s",
quantity: %d,
}',
esc_js( $id ),
$price,
esc_js( $name ),
$quantity,
);
}
$value = WC()->cart->total;
$page_view_gtag = sprintf(
'gtag("event", "page_view", {
send_to: "GLA",
ecomm_pagetype: "cart",
value: %f,
items: [%s]});',
$value,
join( ',', $item_info ),
);
$this->add_inline_event_script( $page_view_gtag );
}
/**
* Add product data to include in JS data.
*
* @since 2.0.3
*
* @param WC_Product $product
*/
protected function add_product_data( $product ) {
$this->products[ $product->get_id() ] = [
'name' => $product->get_name(),
'price' => wc_get_price_to_display( $product ),
];
}
/**
* Controls whether the Google Ads tag and event snippets should be included on the page.
*
* @return bool True if the Global Site Tag framework should be included.
*/
public static function is_needed(): bool {
if ( apply_filters( 'woocommerce_gla_disable_gtag_tracking', false ) ) {
return false;
}
return true;
}
/**
* Check if the customer has previous orders.
* Called after order creation (check for older orders including the order which was just created).
*
* @param string $customer_email Customer email address.
* @return bool True if this customer has previous orders.
*/
private static function is_first_time_customer( $customer_email ): bool {
$query = new \WC_Order_Query(
[
'limit' => 2,
'return' => 'ids',
]
);
$query->set( 'customer', $customer_email );
$orders = $query->get_orders();
return count( $orders ) === 1 ? true : false;
}
/**
* This method ONLY works during development in the Fast Refresh mode.
*
* The runtime.js and react-refresh-runtime.js files are created when the front-end development is
* running `npm run start:hot`, and they need to be loaded to make the gtag-events scrips work.
*/
private function register_js_for_fast_refresh_dev() {
// This file exists only when running `npm run start:hot`
$runtime_path = "{$this->get_root_dir()}/js/build/runtime.js";
if ( ! file_exists( $runtime_path ) ) {
return;
}
$plugin_url = $this->get_plugin_url();
wp_enqueue_script(
'gla-webpack-runtime',
"{$plugin_url}/js/build/runtime.js",
[],
(string) filemtime( $runtime_path ),
false
);
// This script is one of the gtag-events dependencies, and its handle is wp-react-refresh-runtime.
// Ref: js/build/gtag-events.asset.php
wp_register_script(
'wp-react-refresh-runtime',
"{$plugin_url}/js/build-dev/react-refresh-runtime.js",
[ 'gla-webpack-runtime' ],
$this->get_version(),
false
);
}
/**
* Set user data config when Enhanced Conversions is enabled.
*
* @return string|null
*/
public function get_enhanced_conversion_tag() {
$enhanced_conversions = $this->options->get( OptionsInterface::ADS_ENHANCED_CONVERSIONS_ENABLED );
if ( ! $enhanced_conversions ) {
return;
}
// Retrieve user data from the current session, returns an empty array if not set.
$customer = $this->wc->get_customer_details();
$ec_data = [];
// Add email address to enhanced conversion data.
if ( ! empty( $customer['email'] ) ) {
$ec_data['sha256_email_address'] = $this->normalize_and_hash( $customer['email'] );
}
// Add address details if available.
if ( ! empty( $customer['first_name'] ) && ! empty( $customer['last_name'] ) && ! empty( $customer['postcode'] ) && ! empty( $customer['country'] ) ) {
$ec_data['address'] = [
'sha256_first_name' => $this->normalize_and_hash( $customer['first_name'] ),
'sha256_last_name' => $this->normalize_and_hash( $customer['last_name'] ),
'postal_code' => $customer['postcode'],
'country' => $customer['country'],
];
if ( ! empty( $customer['address'] ) ) {
$ec_data['address']['street'] = $customer['address'];
}
if ( ! empty( $customer['city'] ) ) {
$ec_data['address']['city'] = $customer['city'];
}
if ( ! empty( $customer['state'] ) ) {
$ec_data['address']['region'] = $customer['state'];
}
}
// Phone number can only be added when email and/or address is present.
if ( empty( $ec_data ) ) {
return;
}
// Add phone number if available, requires country code for correct format.
if ( ! empty( $customer['phone'] ) && ! empty( $customer['country'] ) ) {
$phone = $this->format_phone_to_international( $customer['phone'], $customer['country'] );
if ( ! empty( $phone ) ) {
$ec_data['sha256_phone_number'] = $this->normalize_and_hash( $phone );
}
}
// Return the tag.
return sprintf(
'gtag("set", "user_data", %s);',
wp_json_encode( $ec_data )
);
}
/**
* Converts a customers phone number to E.164 format.
*
* @param string $phone The customer entered phone number.
* @param string $country The customer country code.
* @return string
*/
private function format_phone_to_international( $phone, $country ) {
// Get the calling code for the customers country.
$countries = new WC_Countries();
$calling_code = $countries->get_country_calling_code( $country );
// Cannot create a international number if there is no valid call code.
if ( empty( $calling_code ) ) {
return '';
}
// Remove any non-digit characters and the leading 0 from the phone number.
$phone = ltrim( preg_replace( '/[^0-9]/', '', $phone ), '0' );
// Prepend the calling code.
$phone = $calling_code . $phone;
// Validate the number is the correct length.
if ( strlen( $phone ) < 11 || strlen( $phone ) > 15 ) {
return '';
}
return $phone;
}
/**
* Normalize and hash enhanced conversion data.
*
* @param string $value The value to hash.
* @param string $algo The hashing algorithm to use.
*
* @return string
*/
private function normalize_and_hash( $value, $algo = 'sha256' ): string {
return hash( $algo, strtolower( trim( $value ) ) );
}
}
Warning: class_implements(): Class Automattic\WooCommerce\GoogleListingsAndAds\Google\GlobalSiteTag does not exist and could not be loaded in /home/shagunto/public_html/wp-content/plugins/google-listings-and-ads/src/Internal/DependencyManagement/AbstractServiceProvider.php on line 119