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

Implement consent mode v0 #361

Merged
merged 5 commits into from
Mar 1, 2024
Merged

Conversation

tomalec
Copy link
Member

@tomalec tomalec commented Feb 27, 2024

Changes proposed in this Pull Request:

Add the minimalistic implementation to add support for Google Consent mode

We want to add MVP ASAP before Google starts penalizing merchants without this setup. (pcTzPl-25o-p2)
Later, we could iterate by adding settings to customize the snippet a bit using UI.

This PR adds a basic default and documents how to customize/extend it.

Checks:

  • Does your code follow the WordPress coding standards?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Screenshots:

image

Detailed test instructions:

  1. Buiild, Install and activate the extension
  2. Deactivate GLA (if applicable, as the integration is broken for remove-universal-analytics branch)
  3. Disable "Add to cart" events in settings (/wp-admin/admin.php?page=wc-settings&tab=integration&section=google_analytics) so WooCommerce Core will not set up its own gtag config making us do nothing.
  4. Open Tag Assistant
  5. Open shop page the page
  6. In Tag assistant you should see the "Consent" as the first entry.
  7. If you're connecting from EEA region, you should see "Consent" being Denied by default
    image
  8. If you're connecting from non-EEA region, you should see "Consent" not set
    image
  9. Check that Readme description
  10. Add the snippet to customize the consent mode:
add_action( 'wp_enqueue_scripts', function () {
    $code = "
        gtag( 'consent', 'default', {
            analytics_storage: 'granted',
            ad_storage: 'granted',
            ad_user_data: 'denied',
            ad_personalization: 'granted',
            region: 'PL', // <--------- SET IT TO YOUR REGION
        } );";

    wp_register_script( 'my-custom-consent-mode', '', array('woocommerce-google-analytics-integration'), null, [ 'in_footer' => true ] );
    wp_add_inline_script( 'my-custom-consent-mode', $code );
    wp_enqueue_script( 'my-custom-consent-mode' );
} );

Check that the second Consent entry is added with custom defaults
image

Additional details:

  1. This customization capabilities are not ideal, as the is a change to get gtag hit before the second consent config is saved, as we may trigger page_view or other events. We may improve that in the future.

    Especially since the current logic to force main.js to run after the document is ready is pretty hacky. (I guess that's why we add inline scriptonwp_footer` hook). I think we can make it in a cleaner fashion:

    • wrap all JS code that requires document ready in DOMContentLoaded event listener
    • execute the code that needs to run synchronously

Changelog entry

Add - Basic consent mode support.

Deny all for EEA visitors. Document the customization snippet.
@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 Feb 27, 2024
@tomalec tomalec requested review from martynmjones and a team February 27, 2024 19:28
@tomalec tomalec marked this pull request as ready for review February 27, 2024 19:28
@tomalec tomalec changed the title Implement consnet mode v0 Implement consent mode v0 Feb 27, 2024
to cover the quirks of wcgaiData not being loaded otherwise.
@tomalec
Copy link
Member Author

tomalec commented Feb 28, 2024

After #363 the [ 'in_footer' => true ] part would not be needed in the snippet.

Copy link
Contributor

@martynmjones martynmjones left a comment

Choose a reason for hiding this comment

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

Hey @tomalec, thanks for adding the initial consent implementation.

All appears to be working well and the consent parameters are picked up by Google. One issue with the country code config but everything else looks good ✅

assets/js/src/config.js Outdated Show resolved Hide resolved
@tomalec tomalec merged commit d221ed3 into remove-universal-analytics Mar 1, 2024
1 check passed
@tomalec tomalec deleted the add/consent-mode-v0 branch March 1, 2024 16:49
@jorgemd24 jorgemd24 mentioned this pull request Mar 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.

2 participants