site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Respect Analytics tracking exclusions when implemented via Tag Manager

Open aaemnnosttv opened this issue 6 years ago • 12 comments

Feature Description

Currently the settings for excluding subsets of users from Analytics tracking is limited to the implementation by the Analytics module itself. However, many users implement Analytics via Tag Manager and lose this granularity in their collected metrics as a result.

We can do this by opting out of tracking on a per-UA code basis, or for ALL tracking altogether.

Opt out of tracking for UA-XXXXX-Y

window['ga-disable-UA-XXXXX-Y'] = true;

The challenge with this approach is we may not know what UA code the user is using via Tag Manager.

Opt out of ALL tracking

window._gaUserPrefs = { ioo: () => true };

See https://developers.google.com/analytics/devguides/collection/analyticsjs/user-opt-out


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

Acceptance criteria

  • Based on whether tracking the current user is disabled:
    • Instead of not rendering our GA snippet, include window._gaUserPrefs = { ioo: () => true }; in the page to disable GA tracking for this request altogether.

Implementation Brief

  • Add a new private function: Analytics::print_tracking_opt_out to output the following
    <script type="text/javascript">window["_gaUserPrefs"] = { ioo : function() { return true; } }</script>
    
    This is the exact code added by the official Chrome extension
  • Add an action on wp_head in Analytics::register to conditionally call print_tracking_opt_out if $this->is_tracking_disabled()
  • Remove existing guards to output snippet using is_tracking_disabled
    • enqueue_gtag_js
    • print_amp_gtag
    • print_amp_client_id_optin (Removed in #1160)

QA Brief

We need to test that when the Analytics module is configured and set to exclude logged-in users that the tracking opt-out is added (on the front-end only).

  1. Install Tag Assistant Chrome extension
    Note that when opt-out is added as a result of tracking being disabled, Tag Assistant will turn yellow/orange and show it in "Where to optimize"
    image
  2. Set up Site Kit and the Analytics module with default settings (logged in users should be excluded from tracking)
  3. Ensure Site Kit screens do not show the orange opt-out enabled in Tag Assistant
  4. Go to the frontend while logged-in
  5. Ensure the orange opt-out is shown in Tag Assistant
  6. Go to the frontend while NOT logged-in
  7. Ensure the orange opt-out is NOT shown in Tag Assistant

Now we need to make sure that the opt-out code is not added when tracking is not disabled

  1. Edit Settings for Analytics and toggle off "Logged-in users" under "Exclude from Analytics"
  2. Ensure Site Kit screens do not show the orange opt-out enabled in Tag Assistant
  3. Go to the frontend while logged-in
  4. Ensure the orange opt-out is NOT shown in Tag Assistant
  5. Go to the frontend while NOT logged-in
  6. Ensure the orange opt-out is NOT shown in Tag Assistant

To test with Tag Manager

  1. Edit settings for Analytics and choose "Remove snippet" to prevent the module from outputting the Analytics tracking code (since we're going to implement it from Tag Manager instead)
  2. Go to https://tagmanager.google.com with your Google Account
  3. Create a new web container in your Tag Manager account
  4. Create a new tag in your new container
    Type: Analytics, provide a tracking ID to use (can be same as in Analytics module), and set Trigger to All Pages
    image
  5. Create another tag: Custom HTML to simply log something in the console to know that it's loaded
    image
    Set trigger to also run on all pages
  6. Configure Tag Manager module to use the new container
  7. With Analytics module configured to exclude logged-in users, go to the frontend while logged-in
  8. Ensure the console log is shown in the console to check that the container is loaded
  9. Ensure the orange opt-out is shown in Tag Assistant and no page views are recorded for Google Analytics
  10. Go to the frontend while NOT logged-in
  11. Ensure the console log is shown in the console to check that the container is loaded
  12. Ensure the orange opt-out is NOT shown in Tag Assistant and page view is recorded in Google Analytics for the configured property (shown in Tag Assistant)

Changelog entry

  • Respect Analytics tracking exclusion of logged-in WordPress users also if Tag Manager is used.

aaemnnosttv avatar Dec 05 '19 15:12 aaemnnosttv

@aaemnnosttv Since this is for GTM, we need to think about how we would determine whether to disable tracking. We shouldn't have duplicate settings. At the moment, we could rely on GA settings in GTM because it is a dependency, but in the future when it's decoupled, GA might not be active.

As you mentioned already, #859 would come in handy. I think we should look at that one sooner than later.

felixarntz avatar Dec 05 '19 15:12 felixarntz

@felixarntz where would you say we stand on this one, now that #859 is finished?

marrrmarrr avatar Feb 18 '20 14:02 marrrmarrr

@felixarntz this is rather trivial to implement if Analytics is active (see current IB). If we want to think farther ahead to something which isn't coupled to Analytics being active, that means we would need to add the same settings UI for Tag Manager as we have for Analytics for tracking exclusions.

We should also allow this to be filtered the way it is in Analytics now https://github.com/google/site-kit-wp/blob/8b75b993eb01666fe1ce90cf87935fabd6145cd3/includes/Modules/Analytics.php#L124-L136

I feel like this warrants a new standalone setting which is controlled from both modules (regardless of which is active). Essentially a global version of trackingDisabled (above), but one that stores exclusions. The return value should also pass through the same filter as above.

Let me know if this sounds like the right direction. The alternative of keeping the value in the analytics setting and manipulating it or referencing it when Analytics itself may not be active seems wrong.

aaemnnosttv avatar Mar 02 '20 17:03 aaemnnosttv

@aaemnnosttv I've discussed this with @marrrmarrr before and we decided to keep this within Analytics for now. The main reason is that tracking may mean different things for different services. For this case, it is both related to Analytics, and furthermore GTM depends on Analytics for being active anyway, so it should be kept in the Analytics module.

Let's keep this issue only focused on the implementation fix to support Analytics via GTM by placing the opt-out snippet (rather than only placing our GA snippet only when needed).

felixarntz avatar Mar 02 '20 18:03 felixarntz

Okay, that makes things easier for sure. I thought the plan was to decouple these modules in the future so we would need a shared setting for this kind of thing. I'll update the IB now 👍

aaemnnosttv avatar Mar 04 '20 10:03 aaemnnosttv

There is that plan, but it's still very vague and actually quite complex to scope out. We can deal with that when we get there.

felixarntz avatar Mar 05 '20 00:03 felixarntz

IB ✅

felixarntz avatar Mar 05 '20 00:03 felixarntz

Bringing this back to execution due to a bug I found causing the opt-out script to be output in wp-admin.

aaemnnosttv avatar Mar 10 '20 15:03 aaemnnosttv

Tested

Enabled Tag Manager after installing latest develop and Analytics Module

Analytics

Navigating around Admin on the BE doesn't show any tags: image

image

Navigated to FE while logged in checked Tag Assistant: image

Navigated to FE while not logged in and checked Tag Assistant: image

Toggled off "Logged-in users" under "Exclude from Analytics" image

image

Navigated to FE while logged in checked Tag Assistant: image

Navigated to FE while not logged in and checked Tag Assistant: image

Tag Manager

Removed Analytics Snippet image image

image

Created a tag in Tag Manager: image

Created second tag in Tag Manager image

Configured Tag Manager in SK selecting my container image

Flipped logged in users exclusion toggle and saved image

image

Console is showing a 404 error per my script tag image

image

Navigated to FE while logged in checked Tag Assistant: image

Console is showing a 404 error per my script tag

Navigated to FE while not logged in and checked Tag Assistant: image

@aaemnnosttv - I followed the QA steps properly except when creating Tag variables, I tried a few things but it wasn't clear what to place for the tracking ID setting I tried Page URL, Page Path and Page Hostname still seeing an error in tag assistant

image

cole10up avatar Mar 12 '20 06:03 cole10up

Update:

Seems my container wasn't published.

Retesting Navigated to FE while logged in checked Tag Assistant: image

Checked console image

Navigated to FE while not logged in and checked Tag Assistant: image

Not seeing orange message but showing an error for property ID.

cole10up avatar Mar 12 '20 06:03 cole10up

Checking page views while logged in: image

Checking page views while not logged in still showing 0... image

cole10up avatar Mar 12 '20 06:03 cole10up

Adjusting my Tag to target my property ID for the variable fixed things. Seeing a positive test now.

Signed out and navigating to FE

image

Also seeing tracking in GA image

Passed QA ✅

cole10up avatar Mar 12 '20 06:03 cole10up