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

Remove wp_head Analytics and AdSense tag verification

Open lucymtc opened this issue 7 years ago • 26 comments

Feature Description

Currently the plugin verifies the existence of Analytics and AdSense tags by first running wp_head hook and parsing the output, if tag was not found then it parses the front end html content, currently being loaded in iframe, to be replaced through fetch request in this PR.

While making the request to the front end site URL wp_head will run either way so the first verification ads complexity to the code and process when is not really needed.

Proposed solution

Remove wp_head tag verifications for Analytics https://github.com/google/site-kit-wp/blob/develop/includes/Modules/Analytics.php#L1033-L1044 and AdSense https://github.com/google/site-kit-wp/blob/develop/includes/Modules/AdSense.php#L542-L556 to make the code and process simpler.

Alternative approaches

Additional Context


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

Acceptance criteria

  • Lookup for potentially existing Analytics and AdSense tags is never done via manually triggering wp_head, wp_body_open, or wp_footer hook in the backend.
  • During Analytics setup, with an existing Analytics tag already added to the frontend via a wp_head action, the plugin detects this existing tag correctly and informs the user about it.
  • During Analytics setup, with an existing Analytics tag already added to the frontend hard-coded in a random location in the theme template for the front page/home page, the plugin detects this existing tag correctly and informs the user about it.
  • During AdSense setup, with an existing AdSense tag already added to the frontend via a wp_head action, the plugin detects this existing tag correctly and informs the user about it.
  • During AdSense setup, with an existing AdSense tag already added to the frontend hard-coded in a random location in the theme template for the front page/home page, the plugin detects this existing tag correctly and informs the user about it.
  • If the user doesn't have access to the Analytics or AdSense property, they are blocked from proceeding.
  • If the user does have access to the Analytics or AdSense property, they can't select the property, instead it will be forced to the property identified by the existing tag. The correct account and property are preselected.

Implementation brief

Existing tag check will be left entirely to the request of frontend which returns all the html content in the response https://github.com/google/site-kit-wp/issues/51 While requesting the frontend any existing tag hardcoded or included through wp_head hook will be detected.

Analytics:

  • Remove existing tag verification from get-accounts datapoint in Analytics https://github.com/google/site-kit-wp/blob/eeec310f16421555f3c9ce8b46daed2e3858b049/includes/Modules/Analytics.php#L1051-L1061
  • Remove tag datapoint from Analytics that will no longer be used.
  • From tag-permission datapoint return account id and property instead of true, this is data already returned by has_access_to_property() so we only need to return the value of $has_access_to_property here https://github.com/google/site-kit-wp/blob/eeec310f16421555f3c9ce8b46daed2e3858b049/includes/Modules/Analytics.php#L591
  • In analytics/setup.js remove call to getExistingTag( ‘analytics’ ) inside getAccounts() and call in componentDidMount(), make request to tag-permission all before calling getAccounts(). This will save existing tag found and tag-permission response in sessionStorage.
  • Remove any reference of responseData.existingTag inside getAccounts() and just set state.existingTag with response from getExistingTag( ‘analytics’ ).
  • data.deleteCache( 'analytics', 'existingTag' ); should run only run if request to tag-permission throws error.
  • Add argument to getAccounts() with the response of tag-permission like getAccounts( existingTagData ), where existingTagData will contain the account ID and property id, then replace the current checks of resonseData.existingTag with existingTagData https://github.com/google/site-kit-wp/blob/eeec310f16421555f3c9ce8b46daed2e3858b049/assets/js/modules/analytics/setup.js#L265
  • Add optional queryArgs to get-accounts request to send the account ID and property ID that we already know from tag-permission response.
  • Set temporary property $_existing_tag_account to the analytics class to store the account id of the existing tag when requesting accounts https://github.com/google/site-kit-wp/blob/eeec310f16421555f3c9ce8b46daed2e3858b049/includes/Modules/Analytics.php#L511 ( will be later removed with https://github.com/google/site-kit-wp/issues/341 )
  • Set $found_account_id here https://github.com/google/site-kit-wp/blob/develop/includes/Modules/Analytics.php#L1047 with value of $_existing_tag_account and remove https://github.com/google/site-kit-wp/blob/eeec310f16421555f3c9ce8b46daed2e3858b049/includes/Modules/Analytics.php#L1051-L1061
  • Ensure this way that account and property are preselected and dropdowns are disabled.

AdSense:

  • Remove tag datapoint from AdSense that will no longer be used.

AdSense already requests currently the existing tag from the frontend fetch response https://github.com/google/site-kit-wp/blob/develop/assets/js/modules/adsense/dashboard/adsense-module-status.js#L52 and there is no existing request to 'tag' datapoint, so nothing else to remove. Existing tag is passed as argument to getAdSenseAccountStatus() https://github.com/google/site-kit-wp/blob/develop/assets/js/modules/adsense/dashboard/adsense-module-status.js#L57 where user permission checks are done in this condition https://github.com/google/site-kit-wp/blob/develop/assets/js/modules/adsense/util.js#L294

https://github.com/google/site-kit-wp/issues/89 depends on this issue as the implementation brief has been updated according to the changes of this work removing existing tag verification from the server side.

Changelog entry

  • Simplify and improve accuracy of existing tag detection mechanism by only relying on a remote request.

lucymtc avatar Jun 28 '19 17:06 lucymtc

@ThierryA @marrrmarrr adding this to the definition board with the note that there is an existing PR for this ticket.

bjackson27 avatar Jul 11 '19 13:07 bjackson27

@bjackson27 Which PR are you referring to?

felixarntz avatar Jul 17 '19 14:07 felixarntz

@felixarntz https://github.com/google/site-kit-wp/pull/311

lucymtc avatar Jul 18 '19 14:07 lucymtc

@lucymtc Can you please prioritize writing an implementation brief and working on this? It will affect (and simplify!) both #51 and #110, so I think this should be done first.

That would likely mean though we need to move #51 back to Implementation Brief, as it would be different I assume.

felixarntz avatar Jul 18 '19 15:07 felixarntz

@lucymtc Implementation brief looks good!

So that means this issue depends on #110 being completed first, right? And then this issue should be completed before #89 I assume (which will need an implementation brief update)?

Would be great if you could clarify your thoughts on the interdependencies. Also make sure to coordinate with the team. I think it's best to reserve all three issues for working on it yourself, since you have the best understanding of how they relate and depend on each other.

felixarntz avatar Jul 18 '19 17:07 felixarntz

Yes, dependant on https://github.com/google/site-kit-wp/issues/110 for the acceptance criteria:

If the user doesn't have access to the Analytics or AdSense property, they are blocked from proceeding. If the user does have access to the Analytics or AdSense property, they can't select the property, instead it will be forced to the property identified by the existing tag.

And https://github.com/google/site-kit-wp/issues/89 as the implementation brief needs an update

lucymtc avatar Jul 18 '19 17:07 lucymtc

@lucymtc - Could we add a label with what type of issue this is (i.e. bug, enhancement etc.)? Thanks!

lilybonney avatar Jul 25 '19 09:07 lilybonney

Discussed with @felixarntz and agreed on moving the ticket back to Implementation Brief for next sprint as some requirements have changed and need to be updated and re estimated

lucymtc avatar Jul 29 '19 13:07 lucymtc

See https://github.com/google/site-kit-wp/pull/311#pullrequestreview-267679938 for additional requirements determined during work on the PR. In order to get the correct properties included in the get-accounts response, that datapoint has to be aware of an eventual existing tag (and the property associated with it). Therefore the existing tag detection must happen first.

felixarntz avatar Jul 29 '19 13:07 felixarntz

@lucymtc Please also have a look at #341 and whether the separation for these two pieces makes sense as it is. They are closely related, and we shouldn't do too much work here that #341 would invalidate.

felixarntz avatar Jul 29 '19 13:07 felixarntz

thanks @felixarntz leaving here as a note, we also need to have a way for the user to refresh in the case they remove the existing tag, right now if you have existing tag and have access to it, even if you remove it, it’s still stored in session storage, so user still gets warned about it

lucymtc avatar Jul 29 '19 13:07 lucymtc

@felixarntz also assigning this ticket to you so it's in your radar for reviewing the doc with suggested updates for IB.

lucymtc avatar Aug 01 '19 07:08 lucymtc

@felixarntz assigning to you for Implementation brief review.

lucymtc avatar Aug 20 '19 05:08 lucymtc

@felixarntz Implementation brief will likely need to be updated after https://github.com/google/site-kit-wp/issues/154 code review is approved ( PR https://github.com/google/site-kit-wp/pull/418 ) moving back to Implementation Brief column

lucymtc avatar Aug 20 '19 10:08 lucymtc

@felixarntz I have updated Implementation Brief according to the latest changes from https://github.com/google/site-kit-wp/issues/154 assigning back to you for review

lucymtc avatar Aug 20 '19 18:08 lucymtc

@lucymtc Implementation brief looks great!

Set temporary property $_existing_tag_account to the analytics class to store the account id of the existing tag when requesting accounts

Just something to keep in mind, this is a necessary evil until we fix the architecture via #340

felixarntz avatar Aug 21 '19 08:08 felixarntz

@lucymtc @aaemnnosttv I just noticed an additional requirement to this one which is highly crucial:

The checks we do on the server-side for an existing tag (in the tag datapoints) are more comprehensive than currently on the client side (e.g. they also cover AMP). We need to make sure the JS code detects in the same way, and the existing e2e tests should be expanded to make sure that works.

felixarntz avatar Aug 22 '19 13:08 felixarntz

@felixarntz I have added the amp tag detection. There are different variations of analytics and we only cover 1 of them in e2e. Should we expand the test for all variations? if so, should we create a new issue for expanding tag verification e2e tests?

lucymtc avatar Aug 26 '19 19:08 lucymtc

@lucymtc Good catch! I think this would be better suited for QUnit though. Can you open an issue to add such tests for the extractTag() function?

felixarntz avatar Aug 27 '19 10:08 felixarntz

I added a hard-coded Google Analytics tag into my theme and then went through the Analytics setup but didn't get an error about an existing analytics tag. I setup my account which already had Analytics after the Site Kit setup, then disconnected Analytics and added it again. At no point did I see an error about an existing snippet being in the theme, and the Analytics settings show:

Screenshot 2019-08-27 20 04 21

So I'm a bit lost as to QAing this one, sorry 😓

tofumatt avatar Aug 28 '19 00:08 tofumatt