Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Google Analytics for WooCommerce version 2.0.0 #2287

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
74 changes: 55 additions & 19 deletions src/Google/GlobalSiteTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,24 @@
*/
public function activate_global_site_tag( string $ads_conversion_id ) {
if ( $this->gtag_js->is_adding_framework() ) {
add_filter(
'woocommerce_gtag_snippet',
function ( $gtag_snippet ) use ( $ads_conversion_id ) {
return preg_replace(
'~(\s)</script>~',
"\tgtag('config', '" . $ads_conversion_id . "', { 'groups': 'GLA', 'send_page_view': false });\n$1</script>",
$gtag_snippet
);
}
);
if ( version_compare( \WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION, '2.0.0', '>=' ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚧
I don't get into this line when I run it with the new GA, as the one above returns false.
GLA still expect the old GA settings to be there https://github.com/woocommerce/google-listings-and-ads/blob/3a423a2d5cfca64486c74cf41905d01db1cc4c52/src/Proxies/GoogleGtagJs.php#L24:L33

The latest GA does not set ga_gtag_enabled

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wp_add_inline_script(
'woocommerce-google-analytics-integration',
$this->get_gtag_config( $ads_conversion_id )
);

Check warning on line 224 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L220-L224

Added lines #L220 - L224 were not covered by tests
} else {
// Legacy code to support Google Analytics for WooCommerce version < 2.0.0.
add_filter(
'woocommerce_gtag_snippet',
function ( $gtag_snippet ) use ( $ads_conversion_id ) {
return preg_replace(
'~(\s)</script>~',
"\tgtag('config', '" . $ads_conversion_id . "', { 'groups': 'GLA', 'send_page_view': false });\n$1</script>",
$gtag_snippet
);
}
);

Check warning on line 236 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L227-L236

Added lines #L227 - L236 were not covered by tests
}
} else {
$this->display_global_site_tag( $ads_conversion_id );
}
Expand All @@ -249,16 +257,44 @@

gtag('js', new Date());
gtag('set', 'developer_id.<?php echo esc_js( self::DEVELOPER_ID ); ?>', true);
gtag('config', '<?php echo esc_js( $ads_conversion_id ); ?>', {
'groups': 'GLA',
'send_page_view': false
});
<?php echo esc_js( $this->get_gtag_config( $ads_conversion_id ) ); ?>

Check warning on line 260 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L260

Added line #L260 was not covered by tests
martynmjones marked this conversation as resolved.
Show resolved Hide resolved
</script>

<?php
// phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedScript
}

/**
* Get the ads conversion configuration for the Global Site Tag
*
* @param string $ads_conversion_id Google Ads account conversion ID.
*/
protected function get_gtag_config( string $ads_conversion_id ) {
return sprintf(
'gtag("config", "%1$s", { "groups": "GLA", "send_page_view": false });',
esc_js( $ads_conversion_id )
);

Check warning on line 276 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L272-L276

Added lines #L272 - L276 were not covered by tests
}

/**
* Add inline JavaScript to the page either as a standalone script or
* attach it to Google Analytics for WooCommerce if it's installed
*
* @param string $inline_script The JavaScript code to display
*
* @return void
*/
public function add_inline_event_script( string $inline_script ) {
if ( class_exists( '\WC_Google_Gtag_JS' ) ) {
wp_add_inline_script(
'woocommerce-google-analytics-integration',
esc_js( $inline_script )
);

Check warning on line 292 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L287-L292

Added lines #L287 - L292 were not covered by tests
} else {
wp_print_inline_script_tag( $inline_script );

Check warning on line 294 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L294

Added line #L294 was not covered by tests
}
}

/**
* Display the JavaScript code to track conversions on the order confirmation page.
*
Expand Down Expand Up @@ -294,7 +330,7 @@
esc_js( $order->get_currency() ),
esc_js( $order->get_id() ),
);
wp_print_inline_script_tag( $conversion_gtag_info );
$this->add_inline_event_script( $conversion_gtag_info );

Check warning on line 333 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L333

Added line #L333 was not covered by tests

// Get the item info in the order
$item_info = [];
Expand Down Expand Up @@ -355,7 +391,7 @@
esc_js( $language ),
join( ',', $item_info ),
);
wp_print_inline_script_tag( $purchase_page_gtag );
$this->add_inline_event_script( $purchase_page_gtag );

Check warning on line 394 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L394

Added line #L394 was not covered by tests
}

/**
Expand Down Expand Up @@ -387,15 +423,15 @@
esc_js( $product->get_name() ),
esc_js( join( ' & ', $this->product_helper->get_categories( $product ) ) ),
);
wp_print_inline_script_tag( $view_item_gtag );
$this->add_inline_event_script( $view_item_gtag );

Check warning on line 426 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L426

Added line #L426 was not covered by tests
}

/**
* Display the JavaScript code to track all pages.
*/
private function display_page_view_event_snippet(): void {
if ( ! is_cart() ) {
wp_print_inline_script_tag(
$this->add_inline_event_script(

Check warning on line 434 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L434

Added line #L434 was not covered by tests
'gtag("event", "page_view", {send_to: "GLA"});'
);
return;
Expand Down Expand Up @@ -438,7 +474,7 @@
$value,
join( ',', $item_info ),
);
wp_print_inline_script_tag( $page_view_gtag );
$this->add_inline_event_script( $page_view_gtag );

Check warning on line 477 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L477

Added line #L477 was not covered by tests
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Proxies/GoogleGtagJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
$this->wcga_settings = get_option( 'woocommerce_google_analytics_settings', [] );

// Prime some values.
if ( empty( $this->wcga_settings['ga_gtag_enabled'] ) ) {
if ( defined( '\WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION' ) && version_compare( \WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION, '2.0.0', '>=' ) ) {
$this->wcga_settings['ga_gtag_enabled'] = 'yes';
} elseif ( empty( $this->wcga_settings['ga_gtag_enabled'] ) ) {

Check warning on line 27 in src/Proxies/GoogleGtagJs.php

View check run for this annotation

Codecov / codecov/patch

src/Proxies/GoogleGtagJs.php#L25-L27

Added lines #L25 - L27 were not covered by tests
$this->wcga_settings['ga_gtag_enabled'] = 'no';
}
if ( empty( $this->wcga_settings['ga_standard_tracking_enabled'] ) ) {
Expand Down
Loading