Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AMP validation when tracking is disabled #1252

Merged
merged 12 commits into from
Mar 16, 2020

Conversation

aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Mar 13, 2020

Summary

Addresses issue #1251

Relevant technical choices

  • Targets master branch for merge
  • Updates selector used to hide JS render targets for AMP.
    AMP render contexts will always have the no-js class on the html element, even if it isn't disabled in the browser. However, if it is disabled in the browser, additional attributes will not be present on the element such as amp-version. By ensuring that this attribute is not present, when hiding with if no-js we can prevent the Site Kit admin bar menu item from being hidden when it shouldn't (i.e. front end AMP render).
  • Adds AMP opt-out using a script tag with type of application/ld+json (which is valid HTML for AMP).
    Ideally this would use text/plain (it's empty so it doesn't really matter) but this is done for compatibility with the AMP plugin which currently reports 2/3 of the valid script types as invalid and thus removes them.

With JS enabled

<html class="no-js i-amphtml-singledoc i-amphtml-standalone" lang="en-US" amp data-ampdevmode amp-version="2003031842100" style="padding-top: 0px !important;">

image

With JS disabled

<html class="no-js" lang="en-US" amp data-ampdevmode>

image

No AMP validation errors
image

Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 4.7 and PHP 5.6.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

tofumatt
tofumatt previously approved these changes Mar 13, 2020
Copy link
Collaborator

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code here makes sense and I was able to access a site with AMP enabled in standard and transitional mode without errors using this branch.

felixarntz
felixarntz previously approved these changes Mar 13, 2020
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, pending #1251 (comment)

@aaemnnosttv aaemnnosttv dismissed stale reviews from felixarntz and tofumatt via f2af477 March 16, 2020 11:20
@aaemnnosttv aaemnnosttv requested a review from felixarntz March 16, 2020 11:46
'groups' => 'default',
'linker' => array(
'domains' => array( $this->get_home_domain() ),
),
),
'optoutElementId' => '__gaOptOutExtension',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -1273,7 +1274,11 @@ private function get_home_domain() {
private function print_tracking_opt_out() {
?>
<!-- <?php esc_html_e( 'Google Analytics user opt-out added via Site Kit by Google', 'google-site-kit' ); ?> -->
<script type="text/javascript">window["_gaUserPrefs"] = { ioo : function() { return true; } }</script>
<?php if ( $this->context->is_amp() ) : // TODO: update script type to `text/plain` when supported by AMP plugin. ?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove the comment here per ampproject/amp-wp#4392

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I suppose that is no longer relevant 😄

Done!

@aaemnnosttv aaemnnosttv requested a review from felixarntz March 16, 2020 18:28
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaemnnosttv Your changes were looking good. During testing though, I noticed that we need to add the optoutElementId indicator to our Tag Manager snippet as well. amp-analytics will automatically combine that config object then with the one from the URL in the config attribute (see https://github.com/ampproject/amphtml/blob/b0aff1b9a59d3ca8f10a6ebe4d6eaeda8c826e61/extensions/amp-analytics/0.1/config.js#L208). It's necessary for us to specify it because the remote Tag Manager configuration also supports only the _gaUserPrefs.ioo way by default, not per outputElementId.

I've tested (and basically QAd this already), for both individual GA and GTM snippets in AMP, and it all appears to work as expected (I don't see myself in Analytics FE live stats when "Exclude logged-in users" is enabled, while I do see myself in there otherwise).

@felixarntz felixarntz merged commit 9d1e01a into master Mar 16, 2020
@felixarntz felixarntz deleted the hotfix/1251-amp-validation branch March 16, 2020 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants