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 integration with WP Consent API #425

Merged
merged 14 commits into from
May 29, 2024

Conversation

martynmjones
Copy link
Contributor

@martynmjones martynmjones commented May 16, 2024

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>';
	}
);
  1. 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:

  • Another option was to add the existing consent state to the ga4w data object so that immediately after we set the consent defaults the update can be sent. While this would be preferable, caching and minification plugins would cause problems with it which is why we're relying on a purely JS implementation instead
  • Tests will be added in a follow-up PR

Changelog entry

Add - Integration with the WP Consent API plugin

@martynmjones martynmjones self-assigned this May 16, 2024
@github-actions github-actions bot added changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement. labels May 16, 2024
@martynmjones martynmjones marked this pull request as ready for review May 20, 2024 17:08
@martynmjones martynmjones requested a review from a team May 20, 2024 17:08
@tomalec tomalec self-requested a review May 21, 2024 14:32
Copy link
Member

@tomalec tomalec left a comment

Choose a reason for hiding this comment

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

I run tests locally - works well. However, initially, I get some quirk that WP Consent API's cookie was not created after allowing in Complianz modal. Restarting new incognito mode helped. I'll take some time to try to reproduce it, not to miss something.

I left some comments regarding the code. The most important are marked with 🚧

assets/js/src/index.js Outdated Show resolved Hide resolved
assets/js/src/index.js Outdated Show resolved Hide resolved
assets/js/src/index.js Outdated Show resolved Hide resolved
assets/js/src/index.js Outdated Show resolved Hide resolved
assets/js/src/index.js Outdated Show resolved Hide resolved
assets/js/src/index.js Outdated Show resolved Hide resolved
includes/class-wc-google-gtag-js.php Outdated Show resolved Hide resolved
@martynmjones
Copy link
Contributor Author

Hey @tomalec, thanks for the review 🙌

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

@martynmjones martynmjones requested a review from tomalec May 22, 2024 19:17
@tomalec
Copy link
Member

tomalec commented May 26, 2024

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.
#426


Tests will be added in a follow-up PR

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

@martynmjones
Copy link
Contributor Author

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.

@martynmjones martynmjones requested a review from tomalec May 28, 2024 13:56
Copy link
Member

@tomalec tomalec left a comment

Choose a reason for hiding this comment

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

Reviewed the code, tested locally. Using Polish IP, and US one, tested with two cookie banner plugins and fiddling manually with API.

LGTM

@martynmjones martynmjones merged commit d2f657b into trunk May 29, 2024
7 checks passed
@martynmjones martynmjones deleted the add/424-integration-with-wp-consent-api branch May 29, 2024 11:11
@jorgemd24 jorgemd24 mentioned this pull request Jun 5, 2024
57 tasks
@martynmjones martynmjones mentioned this pull request Jun 5, 2024
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrate Google Analytics for WooCommerce and WP Consent API
2 participants