kollapsminoriteten/wp-content/plugins/jetpack/modules/google-analytics/classes/wp-google-analytics-legacy.php

338 lines
11 KiB
PHP
Raw Normal View History

2022-06-16 14:01:47 +02:00
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2019-11-15 23:26:29 +01:00
/**
2020-12-10 14:04:11 +01:00
* Jetpack_Google_Analytics_Legacy hooks and enqueues support for ga.js
* https://developers.google.com/analytics/devguides/collection/gajs/
*
* @author Aaron D. Campbell (original)
* @author allendav
*/
2019-11-15 23:26:29 +01:00
/**
2020-12-10 14:04:11 +01:00
* Bail if accessed directly
*/
2019-11-15 23:26:29 +01:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
2022-06-16 14:01:47 +02:00
/**
* Jetpack_Google_Analytics_Legacy hooks and enqueues support for ga.js
*/
2019-11-15 23:26:29 +01:00
class Jetpack_Google_Analytics_Legacy {
2022-06-16 14:01:47 +02:00
/**
* Jetpack_Google_Analytics_Legacy constructor.
*/
2019-11-15 23:26:29 +01:00
public function __construct() {
add_filter( 'jetpack_wga_classic_custom_vars', array( $this, 'jetpack_wga_classic_anonymize_ip' ) );
add_filter( 'jetpack_wga_classic_custom_vars', array( $this, 'jetpack_wga_classic_track_purchases' ) );
2020-10-20 18:05:12 +02:00
add_action( 'wp_head', array( $this, 'insert_code' ), 999 );
2019-11-15 23:26:29 +01:00
add_action( 'wp_footer', array( $this, 'jetpack_wga_classic_track_add_to_cart' ) );
}
/**
* Used to generate a tracking URL
* Called exclusively by insert_code
*
* @param array $track - Must have ['data'] and ['code'].
* @return string - Tracking URL
*/
2022-06-16 14:01:47 +02:00
private function get_url( $track ) {
$site_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( wp_unslash( isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '' ) );
2019-11-15 23:26:29 +01:00
foreach ( $track as $k => $value ) {
if ( strpos( strtolower( $value ), strtolower( $site_url ) ) === 0 ) {
$track[ $k ] = substr( $track[ $k ], strlen( $site_url ) );
}
if ( 'data' === $k ) {
$track[ $k ] = preg_replace( '/^https?:\/\/|^\/+/i', '', $track[ $k ] );
}
// This way we don't lose search data.
if ( 'data' === $k && 'search' === $track['code'] ) {
$track[ $k ] = rawurlencode( $track[ $k ] );
} else {
$track[ $k ] = preg_replace( '/[^a-z0-9\.\/\+\?=-]+/i', '_', $track[ $k ] );
}
$track[ $k ] = trim( $track[ $k ], '_' );
}
$char = ( strpos( $track['data'], '?' ) === false ) ? '?' : '&amp;';
2022-06-16 14:01:47 +02:00
return str_replace( "'", "\'", "/{$track['code']}/{$track['data']}{$char}referer=" . rawurlencode( isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '' ) );
2019-11-15 23:26:29 +01:00
}
/**
* This injects the Google Analytics code into the footer of the page.
2020-10-20 18:05:12 +02:00
* Called exclusively by wp_head action
2019-11-15 23:26:29 +01:00
*/
public function insert_code() {
$tracking_id = Jetpack_Google_Analytics_Options::get_tracking_code();
if ( empty( $tracking_id ) ) {
echo "<!-- Your Google Analytics Plugin is missing the tracking ID -->\r\n";
return;
}
2022-12-15 17:41:53 +01:00
// If we're in the admin_area or DNT is honored and enabled, return without inserting code.
if (
is_admin()
|| Jetpack_Google_Analytics_Utils::is_dnt_enabled()
) {
2019-11-15 23:26:29 +01:00
return;
}
2023-05-23 23:18:12 +02:00
if ( class_exists( Jetpack_AMP_Support::class ) && Jetpack_AMP_Support::is_amp_request() ) {
2020-06-23 13:49:54 +02:00
// For Reader mode — legacy.
add_filter( 'amp_post_template_analytics', 'Jetpack_Google_Analytics::amp_analytics_entries', 1000 );
// For Standard and Transitional modes.
add_filter( 'amp_analytics_entries', 'Jetpack_Google_Analytics::amp_analytics_entries', 1000 );
return;
}
2023-12-07 09:44:11 +01:00
if ( str_starts_with( $tracking_id, 'G-' ) ) {
2020-12-10 14:04:11 +01:00
$this->render_gtag_code( $tracking_id );
} else {
$this->render_ga_code( $tracking_id );
}
}
/**
* Renders legacy ga.js code.
*
* @param string $tracking_id Google Analytics measurement ID.
*/
private function render_ga_code( $tracking_id ) {
2019-11-15 23:26:29 +01:00
$custom_vars = array(
"_gaq.push(['_setAccount', '{$tracking_id}']);",
);
$track = array();
if ( is_404() ) {
// This is a 404 and we are supposed to track them.
$custom_vars[] = "_gaq.push(['_trackEvent', '404', document.location.href, document.referrer]);";
} elseif (
is_search()
2022-06-16 14:01:47 +02:00
&& isset( $_REQUEST['s'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Function renders client-side JS, no site actions.
2019-11-15 23:26:29 +01:00
) {
// Set track for searches, if it's a search, and we are supposed to.
2022-06-16 14:01:47 +02:00
$track['data'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Function renders client-side JS, no site actions.
2019-11-15 23:26:29 +01:00
$track['code'] = 'search';
}
if ( ! empty( $track ) ) {
2022-06-16 14:01:47 +02:00
$track['url'] = $this->get_url( $track );
2019-11-15 23:26:29 +01:00
// adjust the code that we output, account for both types of tracking.
2020-12-10 14:04:11 +01:00
$track['url'] = esc_js( str_replace( '&', '&amp;', $track['url'] ) );
2019-11-15 23:26:29 +01:00
$custom_vars[] = "_gaq.push(['_trackPageview','{$track['url']}']);";
} else {
$custom_vars[] = "_gaq.push(['_trackPageview']);";
}
/**
* Allow for additional elements to be added to the classic Google Analytics queue (_gaq) array
*
* @since 5.4.0
*
* @param array $custom_vars Array of classic Google Analytics queue elements
*/
$custom_vars = apply_filters( 'jetpack_wga_classic_custom_vars', $custom_vars );
// Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example
printf(
"<!-- Jetpack Google Analytics -->
<script type='text/javascript'>
var _gaq = _gaq || [];
%s
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
2020-12-10 14:04:11 +01:00
</script>
<!-- End Jetpack Google Analytics -->\r\n",
2022-06-16 14:01:47 +02:00
implode( "\r\n", $custom_vars ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Additional elements added to the classic Google Analytics script.
2019-11-15 23:26:29 +01:00
);
}
2020-12-10 14:04:11 +01:00
/**
* Renders new gtag code.
*
* @param string $tracking_id Google Analytics measurement ID.
*/
private function render_gtag_code( $tracking_id ) {
/**
* Allow for additional elements to be added to the Global Site Tags array.
*
* @since 9.2.0
*
* @param array $universal_commands Array of gtag function calls.
*/
$universal_commands = apply_filters( 'jetpack_gtag_universal_commands', array() );
$custom_vars = array();
if ( is_404() ) {
$custom_vars[] = array(
'event',
'exception',
array(
'description' => '404',
'fatal' => false,
),
);
}
// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
?>
<!-- Jetpack Google Analytics -->
<script async src='https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( $tracking_id ); ?>'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push( arguments ); }
gtag( 'js', new Date() );
gtag( 'config', <?php echo wp_json_encode( $tracking_id ); ?> );
<?php
foreach ( $universal_commands as $command ) {
echo 'gtag( ' . implode( ', ', array_map( 'wp_json_encode', $command ) ) . " );\n";
}
foreach ( $custom_vars as $var ) {
echo 'gtag( ' . implode( ', ', array_map( 'wp_json_encode', $var ) ) . " );\n";
}
?>
</script>
<!-- End Jetpack Google Analytics -->
<?php
// phpcs:enable
}
2019-11-15 23:26:29 +01:00
/**
* Used to filter in the anonymize IP snippet to the custom vars array for classic analytics
* Ref https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat#_gat._anonymizelp
2020-12-10 14:04:11 +01:00
*
2022-06-16 14:01:47 +02:00
* @param array $custom_vars Custom vars to be filtered.
* @return array Possibly updated custom vars.
2019-11-15 23:26:29 +01:00
*/
public function jetpack_wga_classic_anonymize_ip( $custom_vars ) {
if ( Jetpack_Google_Analytics_Options::anonymize_ip_is_enabled() ) {
array_push( $custom_vars, "_gaq.push(['_gat._anonymizeIp']);" );
}
return $custom_vars;
}
/**
* Used to filter in the order details to the custom vars array for classic analytics
2020-12-10 14:04:11 +01:00
*
2022-06-16 14:01:47 +02:00
* @param array $custom_vars Custom vars to be filtered.
* @return array Possibly updated custom vars.
2019-11-15 23:26:29 +01:00
*/
public function jetpack_wga_classic_track_purchases( $custom_vars ) {
global $wp;
if ( ! class_exists( 'WooCommerce' ) ) {
return $custom_vars;
}
if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
return;
}
// Ref: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce#Example
if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) {
return $custom_vars;
}
$minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' );
if ( $minimum_woocommerce_active && is_order_received_page() ) {
$order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0;
2022-06-16 14:01:47 +02:00
if ( 0 < $order_id && 1 !== (int) get_post_meta( $order_id, '_ga_tracked', true ) ) {
2019-11-15 23:26:29 +01:00
$order = new WC_Order( $order_id );
2022-06-16 14:01:47 +02:00
/**
* [ '_add_Trans', '123', 'Site Title', '21.00', '1.00', '5.00', 'Snohomish', 'WA', 'USA' ]
*/
2019-11-15 23:26:29 +01:00
array_push(
$custom_vars,
sprintf(
2020-12-10 14:04:11 +01:00
'_gaq.push( %s );',
2022-06-16 14:01:47 +02:00
wp_json_encode(
2019-11-15 23:26:29 +01:00
array(
'_addTrans',
(string) $order->get_order_number(),
get_bloginfo( 'name' ),
(string) $order->get_total(),
(string) $order->get_total_tax(),
(string) $order->get_total_shipping(),
(string) $order->get_billing_city(),
(string) $order->get_billing_state(),
2020-12-10 14:04:11 +01:00
(string) $order->get_billing_country(),
2019-11-15 23:26:29 +01:00
)
)
)
);
// Order items
if ( $order->get_items() ) {
foreach ( $order->get_items() as $item ) {
2020-12-10 14:04:11 +01:00
$product = $order->get_product_from_item( $item );
2019-11-15 23:26:29 +01:00
$product_sku_or_id = $product->get_sku() ? $product->get_sku() : $product->get_id();
array_push(
$custom_vars,
sprintf(
2020-12-10 14:04:11 +01:00
'_gaq.push( %s );',
2022-06-16 14:01:47 +02:00
wp_json_encode(
2019-11-15 23:26:29 +01:00
array(
'_addItem',
(string) $order->get_order_number(),
(string) $product_sku_or_id,
$item['name'],
Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ),
(string) $order->get_item_total( $item ),
2020-12-10 14:04:11 +01:00
(string) $item['qty'],
2019-11-15 23:26:29 +01:00
)
)
)
);
}
} // get_items
// Mark the order as tracked
update_post_meta( $order_id, '_ga_tracked', 1 );
array_push( $custom_vars, "_gaq.push(['_trackTrans']);" );
} // order not yet tracked
} // is order received page
return $custom_vars;
}
/**
* Used to add footer javascript to track user clicking on add-to-cart buttons
* on single views (.single_add_to_cart_button) and list views (.add_to_cart_button)
*/
public function jetpack_wga_classic_track_add_to_cart() {
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
if ( ! Jetpack_Google_Analytics_Options::has_tracking_code() ) {
return;
}
if ( ! Jetpack_Google_Analytics_Options::track_add_to_cart_is_enabled() ) {
return;
}
if ( is_product() ) { // product page
global $product;
2022-06-16 14:01:47 +02:00
$product_sku_or_id = $product->get_sku() ? $product->get_sku() : '#' . $product->get_id();
2019-11-15 23:26:29 +01:00
wc_enqueue_js(
"$( '.single_add_to_cart_button' ).click( function() {
_gaq.push(['_trackEvent', 'Products', 'Add to Cart', '#" . esc_js( $product_sku_or_id ) . "']);
} );"
);
2020-12-10 14:04:11 +01:00
} elseif ( is_woocommerce() ) { // any other page that uses templates (like product lists, archives, etc)
2019-11-15 23:26:29 +01:00
wc_enqueue_js(
"$( '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' ).click( function() {
var label = $( this ).data( 'product_sku' ) ? $( this ).data( 'product_sku' ) : '#' + $( this ).data( 'product_id' );
_gaq.push(['_trackEvent', 'Products', 'Add to Cart', label]);
} );"
);
}
}
}