Skip to content

Add ability to anonymise IP address for Google Analytics  #18

@gx10

Description

@gx10

The default GA snippet implementation doesn't add the option to anonymise IP addresses, which is required in some countries, for example the EU.

For all cases where Site Kit places the GA snippet on behalf of the user, we should enable this by default. (Site Kit can't update the snippet for sites where it was already placed via another plugin or other method.)

Steps to QA

Ensure the functionality in the AC/IB works, but it's worth additional QA on an existing site that upgrades to this functionality, because it introduces a new setting with a default.

  1. Create/use an existing site with Analytics enabled before updating to a version of the plugin with this commit.
  2. Ensure the HTML/analytics tag output does not include "anonymize_ip":true
  3. Update the version of the plugin to one with this change.
  4. Check the HTML source; it should now include "anonymize_ip":true

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

For cases where there is no pre-existing GA snippet detected on the site and the user decides to let Site Kit place the GA snippet on their behalf, check if the site is primary AMP.

  • If yes, no need for further action.
  • If not:
    • When placing the snippet, add the anonymize_ip parameter to anonymise IP addresses (details here)
    • In the Analytics settings section, add a toggle (on by default) with the text "Anonymise IP address".

Screenshot 2019-12-05 at 17 33 45

* If the user chooses to switch this off, remove the parameter from their GA snippet and update the toggle text to "IP address anonymization is disabled."

Implementation Brief

  • Add a toggle (on by default) to enable "Anonymize IP" functionality in assets/js/modules/analytics/setup.js with the text:
<Switch label={ __( 'Anonymize IP Address', 'google-site-kit' ) } />
<p>{ __( 'IP addresses will be anonymized.', 'google-site-kit' ) }</p>
  • The above switch should only be available on the change/edit screens—not during setup.
  • Add a setting to the Modules/Analytics.php code for anonymizing IP addresses. This should be enabled by default on updates to respect GDPR—existing users will have the feature on-by-default like new users would.
  • If the anonymous IP setting is enabled, add to enqueue_gtag_js in Modules/Analytics.php.
$anonymize_ip_address = $this->get_data( 'anonymize-ip-address' );
if ( ! is_wp_error( $anonymize_ip_address ) && $anonymize_ip_address ) {
    // See https://developers.google.com/analytics/devguides/collection/gtagjs/ip-anonymization.
    $gtag_opt['anonymize_ip'] = true;
}

Changelog entry

  • Anonymize IP addresses by default in Google Analytics snippet, and grant the user control to modify with a new setting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0High priorityType: EnhancementImprovement of an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions