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

Simplify AdSense account status detection function `getAdSenseAccountStatus`

Open adamsilverstein opened this issue 7 years ago • 12 comments

Feature Description

In the AdSense module, the function getAdSenseAccountStatus is responsible for determining the user's AdSense account status after AdSense is activated. Once the status is determined, the function currently returns all of the data needed to display the final state.

We can simplify this function by returning only the status data, moving the strings to the components where they are displayed.

The AdSenseModuleStatus component calls getAdSenseAccountStatus passing the resulting status data and displaying one of two sub-components AdSenseSetupInstructions and AdSenseInProcessStatus which then take responsibility for displaying the correct strings based on the status/reason.

Action items for this Issue:

  • Adjust the getAdSenseAccountStatus function to only return clientId, accountStatus & issue data points (client id is required to construct deep links).
  • Ensure each logic scenario returns a unique accountStatus & issue combination (some may need adjusting to make them unique).
  • Remove the data point returns for statusMessage, profile, ctaLink, ctaLinkText, ctaTarget, helpLink, helpLinkText, setupComplete, statusHeadline, notice, icon, buttonLink, footerText, footerAppendedText, footerCTA, footerCTALink, continueAction, accountTagMatch, switchLabel, tracking, switchOffMessage & switchOnMessage
  • Each string/data value should instead be moved to AdSenseSetupInstructions or AdSenseInProcessStatus.
  • Use a switch statement at component mount to construct strings/data based on status/reason
  • Also move calls to sendAnalyticsTrackingEvent out of the utility function up into AdSenseModuleStatus where a single call can handle sending the tracking status

Verification

  • For each test user the final state display should exactly match the current display
  • Verify all AdSense status strings are in the components where they are displayed (AdSenseSetupInstructions and AdSenseInProcessStatus)
  • For each flow/resulting status, correct events should be sent to analytics tracking

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

Acceptance criteria

  • The getAdSenseAccountStatus() function in JS should only return clientId (AdSense client ID), accountStatus (slug such as no-account or ads-display-pending for example), and issue (a user-facing string with information about the problem if there is one).
  • The issue variable must always be a user-facing string with a reason, or alternatively false. It must never be a camelCase string or another string that is not user-facing (since it is displayed to the user in the AdSenseSetupInstructions component).
  • Other variables currently returned by the function should be set conditionally on the above variables (probably in a switch clause in the AdSenseInProcessStatus component).
  • The current functionality should be fully maintained. The goal of this issue is solely to simplify the getAdSenseAccountStatus() function.

Implementation Brief

  • { ...this.state } spread (https://github.com/google/site-kit-wp/blob/b42304c5e577eb13fa5934bd9c02f2a0cba1e44b/assets/js/modules/adsense/dashboard/adsense-module-status.js#L130) should be removed—it should not be needed but can also lead to unexpected behaviours if the state of that component changes. The spread should be replaced with only the props AdSenseSetupInstructions requires.
  • getAdSenseAccountStatus should return an object with only the following keys: accountStatus, clientId, issue.
  • Do not set WP translation strings (eg: __()) in getAdSenseAccountStatus(). Only issue should be set, but this is user-facing content returned from the AdSense API. Other user-facing messages/strings generated in that function should be moved closer to the components that output the strings.
  • Move sendAnalyticsTrackingEvent() calls to the component that actually renders the scenario they are tracking instead of in one large function.
  • A nice-to-have: if Adsense E2E tests are not written yet but can be written with test data, it would be wise to test their existing output first and then verify the newly-architected solution still outputs the same thing.

Changelog entry

  • Simplify AdSense account status detection as a base for easier future enhancements and fixes.

adamsilverstein avatar Aug 21 '19 13:08 adamsilverstein

The action item for this issue that specifies "messages should be moved to AdSenseSetupInstructions" concerns me a bit; that's already a reasonably large component, and moving a lot of the extra props it expects to receive into this component is going to make it pretty intense and hard to manage.

This seems another good candidate for moving things into a data store rather than just to a different component.


There's also the specification to "Use a switch statement at component mount to construct strings/data based on status/reason", but I don't know why we would only set this data on component mount. What's the reason for this specificity? I think we can construct the strings on render, not only on mount.


I'm also unsure about "Also move calls to sendAnalyticsTrackingEvent out of the utility function up into AdSenseModuleStatus where a single call can handle sending the tracking status"—looking at the code base, it seems there are a lot of different branches with different statuses to send, but moving that up to the component means replicating all that if/else branching logic just a level up. Maybe those events should be attached to the components rendering that step?


The main issue with that function—to my eyes—is how detached it is from the component rendering its content. I started an IB for this one, but that function seems to be doing a lot and isn't too documented so I might be missing things. 😓

tofumatt avatar Sep 02 '19 22:09 tofumatt

@tofumatt

Do not set strings in getAdSenseAccountStatus(); any messages/strings generated in that function should be moved closer to the components that output the strings.

Just a minor clarification, the issue variable should contain user-facing strings, however these would come directly from AdSense APIs. So you might wanna clarify this to "not set WP translation strings".

The action item for this issue that specifies "messages should be moved to AdSenseSetupInstructions" concerns me a bit; that's already a reasonably large component, and moving a lot of the extra props it expects to receive into this component is going to make it pretty intense and hard to manage.

That's a fair point, however all these messages depend on the accountStatus / issue props passed. I agree this is not optimal, but the AdSense process is in itself fairly complex in the requirements of all the different scenarios we need to support. I'm not sure it makes sense to rely on a data store to get user-facing translation strings and links etc. that depend on the account status. I think, while a big switch clause based on accountStatus increases the size of this component quite a bit, it's still easy enough to follow for now and a more decoupled solution than having all this logic intertwined with the detection of the actual accountStatus.

There's also the specification to "Use a switch statement at component mount to construct strings/data based on status/reason"

I can't comment on this since I didn't write this (note that the "Acceptance criteria" are the "official" list of things to look out for), but I agree with you, this should happen in render().

looking at the code base, it seems there are a lot of different branches with different statuses to send, but moving that up to the component means replicating all that if/else branching logic just a level up. Maybe those events should be attached to the components rendering that step?

Agreed, that makes more sense to me.

The main issue with that function—to my eyes—is how detached it is from the component rendering its content. I started an IB for this one, but that function seems to be doing a lot and isn't too documented so I might be missing things.

Indeed, and this is only the start to making it easier to follow and understand. Because of the complexity, I want to keep this issue fairly constrained to only moved all the variables dependent on the actual "source of truth" variables (accountStatus and issue, plus clientId although it's weird that also comes from there, but let's not worry about that now) out of it. It's a start and will already make the function a bit easier to follow. Once that is done we can start actually simplifying the logic in it.

felixarntz avatar Sep 03 '19 10:09 felixarntz

Btw the rest of the IB that you already wrote looks good (except for the detail about the strings being set, which I mentioned at the top of the above comment). Would be great if you could update it based on the discussion.

felixarntz avatar Sep 03 '19 10:09 felixarntz

IB ✅

felixarntz avatar Sep 03 '19 17:09 felixarntz

A nice-to-have: if Adsense E2E tests are not written yet but can be written with test data, it would be wise to test their existing output first and then verify the newly-architected solution still outputs the same thing.

Not yet written unfortunately, but I agree it would be safer to refactor with test coverage in place. Most of these are currently parked in Revisit Later.

aaemnnosttv avatar Sep 04 '19 07:09 aaemnnosttv

As mentioned: it would be safer to refactor this after E2E tests are implemented, but even without writing them as part of this PR (before changes are made), this looks to be a considerable/meticulous refactor.

Right now the approach getAdSenseAccountStatus() takes is to return the kitchen sink, so pairing it down will likely require refactoring and a lot of testing.

Because this involves AdSense as well (which we currently have a minimal local development/testing story for), I see this as a medium-sized issue because either development or QA will involve a user without a reliable AdSense account.

tofumatt avatar Sep 11 '19 16:09 tofumatt

I'm updating this ticket's size to "L" because the code paths in the current utils.js and their callback/conditionals is especially tangled. I've started to really get a handle on what needs to be refactored here after a lot of looking through and tracing of the code paths, but it is built on a lot of callbacks and not especially React-like 😓

Things are going better now but it's a lot of manual effort retracing/refactoring the existing approach.

tofumatt avatar Sep 30 '19 11:09 tofumatt

Just to note here: digging into this refactor it's such that a child component of AdSenseModuleStatus is supplied a function that updates the state of AdSenseModuleStatus (its parent), causing the parent to re-render as it is heavily dependant on state to render.

Untangling this setup is proving pretty baffling, so I'm still working on it. I don't think untangling the child-updating-parent-state fix is doable within this refactor, but not doing it I think still leaves us with a difficult-to-refactor component. This wasn't immediately apparent as this sort of component/data structure is atypical but not immediately apparent in the design.

tofumatt avatar Oct 01 '19 17:10 tofumatt

An unrelated note, but I've noticed this component is frequently re-rendered. I noticed this on my branch but also in the develop branch. I'm not sure if this is because it's loaded in with filters, but occasionally I notice that, in the background with no user interaction and after all requests have completed: the component re-mounted. Because the data requests are made in componentDidMount, this means the calls are started over again and the component goes from being loaded with a user message to "Loading…"

tofumatt avatar Oct 01 '19 17:10 tofumatt

An unrelated note, but I've noticed this component is frequently re-rendered

This may be related to code that causes a status re-check when the user has focused away from the window and then returns to it. The goal of that refresh is to re-check the status after the user leaves Site Kit to set up their AdSense account. See https://github.com/google/site-kit-wp/blob/develop/assets/js/components/setup/setup-wrapper.js#L54-L72 & https://github.com/google/site-kit-wp/blob/develop/assets/js/modules/adsense/index.js#L115-L135 for the code that drives this.

Just to note here: digging into this refactor it's such that a child component of AdSenseModuleStatus is supplied a function that updates the state of AdSenseModuleStatus (its parent), causing the parent to re-render as it is heavily dependant on state to render.

This callback is used to update the "progress string" on the container component and provide feedback to the user during the process which takes a while as it progresses thru each step in the status logic.

There's also the specification to "Use a switch statement at component mount to construct strings/data based on status/reason"

Good point - this should likely be in render, not in component mount as I wrote in the action items. I will correct that in the description.

I'm updating this ticket's size to "L" because the code paths in the current utils.js and their callback/conditionals is especially tangled

The logic for determining an AdSense user's state is quite complex and reflected in the code logic. Open to suggestions for simplification, so far the idea here is to move most of the result setting out of the logic loop, so the code length will be dramatically reduced, making it a bit easier to follow.

adamsilverstein avatar Oct 07 '19 13:10 adamsilverstein

@felixarntz CR ✅

aaemnnosttv avatar Oct 15 '19 07:10 aaemnnosttv

Tested

SK01: Final state: image

Current state: image image

Confirmed multiple Adsense states status haven't changed from final to current.

Refactoring the Adsense test user spreadsheet utilizing a test management tool going forward.

Passed QA ✅

cole10up avatar Nov 15 '19 02:11 cole10up