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
Merged
2 changes: 1 addition & 1 deletion assets/sass/components/global/_googlesitekit-noscript.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
}

.no-js {
.no-js:not([amp-version]) {

[id^="js-googlesitekit-"] {
display: none;
Expand Down
9 changes: 7 additions & 2 deletions includes/Modules/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ protected function print_amp_gtag() {
}

$gtag_amp_opt = array(
'vars' => array(
'vars' => array(
'gtag_id' => $tracking_id,
'config' => array(
$tracking_id => array(
Expand All @@ -392,6 +392,7 @@ protected function print_amp_gtag() {
),
),
),
'optoutElementId' => '__gaOptOutExtension',
);

/**
Expand Down Expand Up @@ -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() ) : ?>
<script type="application/ld+json" id="__gaOptOutExtension"></script>
<?php else : ?>
<script type="text/javascript">window["_gaUserPrefs"] = { ioo : function() { return true; } }</script>
<?php endif; ?>
<?php
}

Expand Down
13 changes: 12 additions & 1 deletion includes/Modules/Tag_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,20 @@ protected function print_amp_gtm() {
return;
}

// Add the optoutElementId for compatibility with our Analytics opt-out mechanism.
// This configuration object will be merged with the configuration object returned
// by the `config` attribute URL.
$gtm_amp_opt = array(
'optoutElementId' => '__gaOptOutExtension',
);

?>
<!-- Google Tag Manager added by Site Kit -->
<amp-analytics config="<?php echo esc_url( "https://www.googletagmanager.com/amp.json?id=$container_id" ); ?>" data-credentials="include"></amp-analytics>
<amp-analytics config="<?php echo esc_url( "https://www.googletagmanager.com/amp.json?id=$container_id" ); ?>" data-credentials="include">
<script type="application/json">
<?php echo wp_json_encode( $gtm_amp_opt ); ?>
</script>
</amp-analytics>
<!-- End Google Tag Manager -->
<?php
}
Expand Down