Opt In/out should take effect immediately
Bug Description
Follow up for issues discovered during QA of https://github.com/google/site-kit-wp/issues/633.
- Checking or unchecking the opt in to tracking checkbox, then immediately performing an action (without a page refresh) doesn't work correctly during setup flow or on settings screen.
Steps to reproduce
I see the same issue in two locations.
Setup flow

- During the plugin setup I am given the option to opt in to tracking (or opt out if I opted in from the plugin activation banner).
- If I opt in to tracking, then click on "Sign In With Google" the first tracking event is not sent correctly.
gtagis undefined andgooglesitekit.admin.trackingOptinis still false - Similarly if I arrive with tracking enabled having enabled it on the plugin activation screen, then try to opt out, the first tracking event is still sent because
googlesitekit.admin.trackingOptinis still true.
Settings screen

- Arriving at the settings screen with tracking disabled, I go to settings->admin and enable tracking. I then go to "Connect More Services (without refreshing) and activate Analytics. the first activation tracking event is not sent correctly.
gtagis undefined andgooglesitekit.admin.trackingOptinis still false. - Similarly if I arrive with tracking enabled , then try to opt out, the first tracking event is still sent because
googlesitekit.admin.trackingOptinis still true.
Screenshots
Screenshots above.
Additional Context
- PHP Version: 7.3
- OS: [e.g. iOS] OSX
- Browser [e.g. chrome, safari] Chrome Dev
- Plugin Version [e.g. 22] develop branch also tested fix/633-opt-in-window
- Device: [e.g. iPhone6] Macbook Pro
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Opting in to tracking or opting out of it should immediately take effect, with the following event being fired or not fired accordingly.
- Let's consistently rely on one specific global variable
window.googlesitekitTrackingEnabledfor whether tracking is enabled or not, and remove the other one(s).
Implementation Brief
- Change all references to
googlesitekit.admin.trackingOptinto usewindow.googlesitekitTrackingEnabled - Update
sendAnalyticsTrackingEventto only read/usewindow.googlesitekitTrackingEnabledinstead of the currentgooglesitekit.admin.trackingOptin(this will require updating its E2E test as well).
Changelog entry
- Ensure that opting in or out of tracking takes effect immediately.
In the non-React version, this relies on window.googlesitekitTrackingEnabled instead of googlesitekit.admin.trackingOptin. Maybe we should consistently decide on one of the two and use that value throughout (I'd vote for the first since that doesn't rely on an extra admin object to exist).
Agreed, let's use the same value everywhere; it's a bit confusing to have to handle two different values. (Leading to this bug/confusion!)
IB ✅
Tested
Confirmed during setup that opt in takes effect immediately and the setting is retained in the admin section


Confirmed opt out functions the same during setup and into admin


ONE THING TO NOTE Opting out isn't sticky on the same page. Steps:
-
Have user opted in and navigate to the admin page

-
Click opt out by unchecking the box

-
Navigate to one of the other settings tabs in the admin

-
Navigate back to the admin tab
Notice box is still checked

If user refreshes the page the check is gone and the admin shows opted out

Please review if this is acceptable.
It being opted-in after the user opts-out isn't good; I'll take a look at that and try to push a fix now…
Tested
Setup while selecting opt-in and navigated to the admin page to confirm it took effect

Unchecked box to opt out

Navigated to a different tab within the window

Navigated back to Admin tab

Checked general functionality with opting out, refreshing page, navigating around and back to the admin. All looks good.
Passed QA ✅