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

Google Analytics: Add support for universal analytics for eCommerce #8182

Merged
merged 7 commits into from
Nov 27, 2017

Conversation

allendav
Copy link
Contributor

@allendav allendav commented Nov 16, 2017

Ready to test.

Fixes #8181

Background:

  • The Jetpack Google Analytics module currently only supports "legacy" (ga.js) analytics

  • This PR refactors the module in Jetpack to add support for "universal" (analytics.js) analytics and the ecommerce plugin while maintaining support for legacy analytics.

  • Which is used is driven by a new field in the jetpack_wga option: enh_ec_tracking

  • Both "modes" support IP anonymization, tracking new orders and tracking when products are added to the cart (on single or loop pages)

  • A follow-on PR will expand on this PR further by adding support for these additional enhanced analytics behaviors:

enh_ec_track_remove_from_cart
enh_ec_track_prod_impression
enh_ec_track_prod_click
enh_ec_track_prod_detail_view
enh_ec_track_checkout_started

Changes proposed in this Pull Request:

  • Note: The original wp-google-analytics.php file was copied to modules/google-analytics/classes/wp-google-analytics-legacy.php with minor editing.
  • Note: The original wp-google-analytics.php file was then modified to either load that legacy implementation or the new universal analytics implementation.
  • Note: A classes/wp-google-analytics-options.php file was also added to provide a consistent interface to options for both implementations, including the following new enh_ec_tracking (on/off) flag

Testing instructions:

  • Sign up for a Google Analytics tracking code for your test site
  • Install, activate and connect Jetpack and get a Jetpack Professional Plan
  • Install and activate WooCommerce
  • Create a product with no SKU and at least two categories
  • Create another product with a SKU and at least two categories
  • Replace modules/google-analytics with the files from this branch
  • Install and activate the Jetpack Google Analytics Helper plugin ( https://github.com/Automattic/jetpack-google-analytics-helper )
  • Use the helper plugin (wp-admin > Tools > Jetpack Google Analytics Helper) to set the following:
Google Analytics Tracking / Property ID: (empty)
Anonymize IP Addresses: unchecked
Track Purchase Transactions: unchecked
Track Add to Cart Events: unchecked
Enable Enhanced eCommerce: unchecked
  • Visit either product page.

  • View the source and 1) ensure the Google Analytics script (ga.js) is never referenced and 2) no jQuery is present to hook .single_add_to_cart_button or .add_to_cart_button
    Elements.

  • Note: You should see this in the page source (for legacy/classic analytics only):

  • Use the helper plugin (wp-admin > Tools > Jetpack Google Analytics Helper) to set the following:

Google Analytics Tracking / Property ID: UA-12345678-1
Anonymize IP Addresses: unchecked
Track Purchase Transactions: unchecked
Track Add to Cart Events: unchecked
Enable Enhanced eCommerce: unchecked
  • Visit either product page.

  • View the source and 1) ensure the Google Analytics script (ga.js) is present, 2) that the Tracking ID is present, but 3) that no jQuery is present to hook .single_add_to_cart_button or .add_to_cart_button elements.

  • Use the helper plugin (wp-admin > Tools > Jetpack Google Analytics Helper) to set the following:

Google Analytics Tracking / Property ID: UA-12345678-1
Anonymize IP Addresses: CHECKED
Track Purchase Transactions: unchecked
Track Add to Cart Events: unchecked
Enable Enhanced eCommerce: unchecked
  • Visit either product page.
  • View the source and 1) ensure the Google Analytics script (ga.js) is present and that the _anonymizeIp element is present, e.g. for legacy/classic:
_gaq.push(['_gat._anonymizeIp']);

For universal:

ga( 'set', 'anonymizeIp', true );
  • Use the helper plugin (wp-admin > Tools > Jetpack Google Analytics Helper) to set the following:
Google Analytics Tracking / Property ID: UA-12345678-1
Anonymize IP Addresses: unchecked
Track Purchase Transactions: CHECKED
Track Add to Cart Events: unchecked
Enable Enhanced eCommerce: unchecked
  • In your Google Analytics Dashboard ( https://analytics.google.com/ ), go to Admin for your site (it looks like a gear in the lower left corner)
  • Under View select Ecommerce Settings
  • Enable Ecommerce if you haven't already. DO NOT ENABLE ENHANCED ECOMMERCE AT THIS TIME.

screen shot 2017-11-20 at 12 33 54 pm

  • Back on your test site, Complete the purchase of one of each product using a gateway like Check Payment.

  • Return to your Google Analytics Dashboard and go to Conversions and then Ecommerce

  • Set the date range to today

  • Make sure you see the sale and that the details are correct (note: it can take a few minutes for the transaction to appear in GA)

  • Use the helper plugin (wp-admin > Tools > Jetpack Google Analytics Helper) to set the following:

Google Analytics Tracking / Property ID: UA-12345678-1
Anonymize IP Addresses: unchecked
Track Purchase Transactions: CHECKED
Track Add to Cart Events: CHECKED
Enable Enhanced eCommerce: unchecked
  • On the product listing page, choose Add to Cart for one of your products
  • Then, visit the other product’s product page and add that product to your cart there.
  • In your Google Analytics Dashboard, go to Real-time and then Events
  • Make sure you see the addition of the SKU less and the SKU product and that the details are correct (Note: for legacy/classic ecommerce the event appears as Product: Add to Cart. For enhanced ecommerce the event is stored as UX/click/Add to Cart)

screen shot 2017-11-20 at 12 40 44 pm

  • In your Google Analytics Dashboard, go to Admin for your site
  • Under View select Ecommerce Settings
  • Enable ENHANCED Ecommerce
  • Now, repeat all the tests above but with Enable Enhanced eCommerce CHECKED

Proposed changelog entry for your changes:

  • Google Analytics: Add support for Universal Analytics / Enhanced eCommerce

See also:

cc @justinshreve

@allendav allendav self-assigned this Nov 16, 2017
@allendav allendav requested a review from a team as a code owner November 16, 2017 00:43
@jeherve jeherve added [Pri] High [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it labels Nov 16, 2017
@allendav allendav changed the title Google Analytics: Add support for universal analytics Google Analytics: Add support for universal analytics for eCommerce Nov 17, 2017
@jeherve jeherve added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] In Progress labels Nov 20, 2017
@justinshreve
Copy link
Contributor

justinshreve commented Nov 20, 2017

@allendav While testing the purchase event, I noticed that my two categories came through as one in GA (for legacy/ga.js and universal):

screen shot 2017-11-20 at 3 46 15 pm

Another awesome category and Awesome category are two separate categories.

@justinshreve
Copy link
Contributor

Everything else seems to be testing nicely 👍 .

@allendav
Copy link
Contributor Author

@justinshreve wrote:

While testing the purchase event, I noticed that my two categories came through as one in GA (for legacy/ga.js and universal)

That's by design. We concatenate the categories on purpose.

Copy link
Contributor

@justinshreve justinshreve left a comment

Choose a reason for hiding this comment

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

Approving from my end of things. Everything tests correctly and changes are looking good to me.

@jeherve jeherve added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Nov 21, 2017
$track['code'] = 'search';
private function __construct() {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( print_r( Jetpack_Google_Analytics_Options::debug_dump(), true ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this error_log needed?, I'm seeing it's only dumping empty arrays even when nothing remarkable happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its just for when WP_DEBUG is active. It isn't as useful as I'd thought - I think I'll just delete the call and the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed in dca8278

@oskosk
Copy link
Contributor

oskosk commented Nov 23, 2017

I've tested following the test instructions and when adding products to the cart when Enhanced eCommerce is enabled, I see this in Real Time Events as opposed to a detail on the products

image

With these settings:

Also I didn't get to see anything about my checkout when looking at Realt Time -> Conversions

I may have tested something wrong. I do see analytics.js being loaded instead of ga.js after checking Enhanced eCommerce. I've tested with two products, each of them under two categories, one of them with an SKU.

The testing instructions when tried with Enhanced eCommerce being unchecked worked for me as expected and described here.

@oskosk oskosk added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Ready to Merge Go ahead, you can push that green button! labels Nov 23, 2017
oskosk
oskosk previously requested changes Nov 23, 2017
Copy link
Contributor

@oskosk oskosk left a comment

Choose a reason for hiding this comment

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

Reviewed and couldn't reproduce the expected view in Google Analytics dashboard. Also, left an observation on the code about an error_log in a class constructor

@allendav
Copy link
Contributor Author

@oskosk wrote:

I've tested following the test instructions and when adding products to the cart when Enhanced eCommerce is enabled, I see this in Real Time Events as opposed to a detail on the products

Also I didn't get to see anything about my checkout when looking at Realt Time -> Conversions

I'll double check - I found it challenging to find the correct click sequence in the Google Analytics dashboard to surface the data myself - will carefully repeat the sequence today and post it

@oskosk
Copy link
Contributor

oskosk commented Nov 26, 2017

Thank you @allendav! All in all, everything here looks great. Old analytics work as expected. New ones are still logged apparently. (It's just that I couldn't see the product details in Analytics' dashboard as the Testing Instructions specifically mention as something to check).

This PR can be probably tested better when we launch the beta, so let's try to merge it on Monday.

I do maintain my opinion on the error_log that doesn't seem to be so necessary. But apart from that, if there are any bugs here, I guess they will certainly be more discoverable when running the beta than in this PR.

@allendav
Copy link
Contributor Author

allendav commented Nov 26, 2017

@oskosk : OK, here's the secret sauce to see add-to-cart details:

add-to-cart

Edit: Again, although events are real-time, I find it can take a few minutes for data to appear under conversions

@allendav
Copy link
Contributor Author

@oskosk - secret sauce for seeing details about your checkout

checkout

@allendav
Copy link
Contributor Author

@oskosk wrote:

Thank you @allendav! All in all, everything here looks great. Old analytics work as expected. New ones are still logged apparently. (It's just that I couldn't see the product details in Analytics' dashboard as the Testing Instructions specifically mention as something to check).

You're welcome!

This PR can be probably tested better when we launch the beta, so let's try to merge it on Monday.

That sounds awesome - hope we can merge #8198 and #8218 as well

I do maintain my opinion on the error_log that doesn't seem to be so necessary. But apart from that, if there are any bugs here, I guess they will certainly be more discoverable when running the beta than in this PR.

Sounds good. I removed the debugging - it was useful early in refactoring the module, but not very helpful now.

And yep - we'll exercise this in the beta and see if anything precipitates.

cc @justinshreve

@allendav
Copy link
Contributor Author

@justinshreve - re #8182 (comment) what would you think about filtering in another data- attribute with the name so we could avoid the "(not set)" rows and keep all the events for a product in one row? it is different than how the existing GA extension works, but i think it would be an easy improvement

@oskosk oskosk added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Nov 27, 2017
@oskosk oskosk dismissed their stale review November 27, 2017 14:56

Requested changes have been provided. Testing again now

Copy link
Contributor

@oskosk oskosk left a comment

Choose a reason for hiding this comment

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

Thanks for the detailed diagrams on how to see the events. I got to see everything as expected. LGTM!

@oskosk oskosk added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Nov 27, 2017
@oskosk
Copy link
Contributor

oskosk commented Nov 27, 2017

@allendav while having this branch checked out, I deactivated WooCommerce and got to see this in debug.log.

map_meta_cap was called <strong>incorrectly</strong>. The post type shop_order is not registered, so it may not be reliable to check the capability "edit_post" against a post of that type. Please see <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information. (This message was added in version 4.4.0.)
 Type: PHP Notice Line: 4145
 File: /srv/users/userad06496f/apps/userad06496f/public/wp-includes/functions.php

Do you think it's something related to the PR or just the fact that I deactivated Woo after having created products ?

@allendav
Copy link
Contributor Author

@oskosk wrote:

while having this branch checked out, I deactivated WooCommerce and got to see this in debug.log

...

Do you think it's something related to the PR or just the fact that I deactivated Woo after having created products ?

It's an old core Wordpress bug - woocommerce/woocommerce#10777 https://core.trac.wordpress.org/ticket/36208

@justinshreve
Copy link
Contributor

@allendav

@justinshreve - re #8182 (comment) what would you think about filtering in another data- attribute with the name so we could avoid the "(not set)" rows and keep all the events for a product in one row? it is different than how the existing GA extension works, but i think it would be an easy improvement

That sounds good to me. That's definitely an improvement.

@dereksmart dereksmart merged commit 2bcd0a0 into master Nov 27, 2017
@dereksmart dereksmart removed the [Status] Ready to Merge Go ahead, you can push that green button! label Nov 27, 2017
@oskosk oskosk added this to the 5.6 milestone Nov 27, 2017
@jeherve jeherve deleted the add/8181-enhanced-google-analytics branch November 27, 2017 21:41
@jeherve jeherve added [Status] Needs Changelog [Status] Needs Testing We need to add this change to the testing call for this month's release and removed [Status] Needs Changelog [Status] Needs Testing We need to add this change to the testing call for this month's release labels Nov 27, 2017
jeherve added a commit that referenced this pull request Nov 28, 2017
oskosk pushed a commit that referenced this pull request Nov 28, 2017
* Changelog 5.6: create base for changelog.

* Update changelog with 5.5.1 info.

* Changelog: add #7930 and #8238

* Changelog: add #8076

* Changelog: add #8100

* Changelog: add #8117

* Changelog: add #8141

* Changelog: add #8143

* Changelog: add #8147

* Changelog: add #8149

* Changelog: add #8153

* Changelog: add #8173

* Changelog: add #8184

* Changelog: add #8196

* Changelog: add #8199

* Changelog: add #8093

* Changelog: add #8171

* Changelog: add #8182

* Changelog: add #8202, #8222

* Changelog: add #8228

* Changelog: add #8240

* Changelog: add #8251

* remove AL card change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Google Analytics [Pri] High [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants