woocommerce / woocommerce-google-analytics-integration

WordPress plugin: Provides the integration between WooCommerce and Google Analytics.
http://wordpress.org/plugins/woocommerce-google-analytics-integration/
170 stars 69 forks source link

Add integration with WP Consent API #425

Closed martynmjones closed 3 months ago

martynmjones commented 4 months ago

Changes proposed in this Pull Request:

Closes #424

Adds integration with the WP Consent API plugin to better support different consent banner configurations.

If the plugin is installed on a website then the existing consent state will be sent as an update when tracking is initialised. For on-page updates, we're adding an event listener for wp_listen_for_consent_change which is dispatched by WP Consent API when any changes are made.

Detailed test instructions:

General test instruction

  1. Build extension from add/424-integration-with-wp-consent-api
  2. Install Complianz and setup a basic consent banner
  3. Debug the site using Tag Assistant while logged out
  4. Grant consent via consent banner
  5. Confirm a Consent event is sent with the updated state
  6. Trigger events on page i.e visit the shop archive, visit a single product, add a product to cart
  7. Confirm the events are all recorded in Tag Assistant and that the consent On-page Update is Granted (Under the Consent tab) Screenshot 2024-05-20 at 17 50 37
  8. Revoke consent via the consent banner
  9. Confirm that the consent state is no longer Granted in Tag Assistant

Event order testing

By default, the WP Consent API plugin JavaScript loads in the footer meaning we are unable to send the consent update event with the current state until after the page loads.

We need to support calls to gtag from on the page for Google Listings & Ads and other third-party extensions. To do that we're including the wait_for_update parameter as recommended by Google so that events sent before the consent update will still be recorded. 57ec41e

  1. Trigger an event on the page
    add_action(
    'wp_head',
    function() {
        echo '<script>gtag( "event", "example_event" );</script>';
    }
    );
  2. Visit the site while debugging via Tag Assistant with consent Granted
    1. Confirm the event is sent before the consent update
    2. View the Consent tab and confirm that the On-page Update is listed as Granted

Without wait_for_update:

Screenshot 2024-05-20 at 15 23 56

With wait_for_update:

Screenshot 2024-05-20 at 15 26 37

Additional details:

Changelog entry

Add - Integration with the WP Consent API plugin

martynmjones commented 3 months ago

Hey @tomalec, thanks for the review 🙌

I've made some adjustments and responded to your comments so this is ready for a second round.

tomalec commented 3 months ago

After adding the fixes I suggested, I tested the branch with the following extensions:

  1. Complianz, w/ & w/o Autooptimize
  2. Cookie bot, w/ & w/o Autooptimize
  3. GDPR Cookie Compliance, w/ & w/o Autooptimize

I added the WP Consent API steps to general testing wiki https://github.com/woocommerce/woocommerce-google-analytics-integration/wiki/General-Testing#12-wp-consent-api-integration

I also created Setup & Smoke testing instructions for those three extensions in a separate Wiki: https://github.com/woocommerce/woocommerce-google-analytics-integration/wiki/Smoke-Testing-WP-Consent-API-integration-with-other-extensions. I know we don't have the capacity to maintain support for all of them. Still, IMHO, it would be good to at least one integration works.

I made a PR with a small README note about WP Consent API and UI for banners. https://github.com/woocommerce/woocommerce-google-analytics-integration/pull/426


Tests will be added in a follow-up PR

Do you have anything started, or can I pick that up?

martynmjones commented 3 months ago

Hey @tomalec, many thanks for the additional testing and for updating the documentation. I've made some changes based on our discussions so far if you could please take another look.