Remove wp_head Analytics and AdSense tag verification
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, orwp_footerhook in the backend. - During Analytics setup, with an existing Analytics tag already added to the frontend via a
wp_headaction, 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_headaction, 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-accountsdatapoint in Analytics https://github.com/google/site-kit-wp/blob/eeec310f16421555f3c9ce8b46daed2e3858b049/includes/Modules/Analytics.php#L1051-L1061 - Remove
tagdatapoint from Analytics that will no longer be used. - From
tag-permissiondatapoint return account id and property instead oftrue, this is data already returned byhas_access_to_property()so we only need to return the value of$has_access_to_propertyhere https://github.com/google/site-kit-wp/blob/eeec310f16421555f3c9ce8b46daed2e3858b049/includes/Modules/Analytics.php#L591 - In analytics/setup.js remove call to
getExistingTag( ‘analytics’ )insidegetAccounts()and call incomponentDidMount(), make request totag-permissionall before callinggetAccounts(). This will save existing tag found and tag-permission response insessionStorage. - Remove any reference of
responseData.existingTaginsidegetAccounts()and just setstate.existingTagwith response fromgetExistingTag( ‘analytics’ ). data.deleteCache( 'analytics', 'existingTag' );should run only run if request totag-permissionthrows error.- Add argument to
getAccounts()with the response oftag-permissionlikegetAccounts( existingTagData ), whereexistingTagDatawill contain the account ID and property id, then replace the current checks ofresonseData.existingTagwithexistingTagDatahttps://github.com/google/site-kit-wp/blob/eeec310f16421555f3c9ce8b46daed2e3858b049/assets/js/modules/analytics/setup.js#L265 - Add optional queryArgs to
get-accountsrequest to send the account ID and property ID that we already know fromtag-permissionresponse. - Set temporary property
$_existing_tag_accountto 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_idhere https://github.com/google/site-kit-wp/blob/develop/includes/Modules/Analytics.php#L1047 with value of$_existing_tag_accountand 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
tagdatapoint 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.
@ThierryA @marrrmarrr adding this to the definition board with the note that there is an existing PR for this ticket.
@bjackson27 Which PR are you referring to?
@felixarntz https://github.com/google/site-kit-wp/pull/311
@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.
@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.
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 - Could we add a label with what type of issue this is (i.e. bug, enhancement etc.)? Thanks!
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
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.
@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.
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
@felixarntz also assigning this ticket to you so it's in your radar for reviewing the doc with suggested updates for IB.
@felixarntz assigning to you for Implementation brief review.
@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
@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 Implementation brief looks great!
Set temporary property
$_existing_tag_accountto 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
@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 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 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?
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:
So I'm a bit lost as to QAing this one, sorry 😓