How to make google analytics tag work. (Really)
-
For those of you pulling your hair out trying to use a Google Analytics tag with the AMP plugin: I can get a tag to fire if I do the following:
add_action( ‘wp_body_open’, ‘add_google_tag_manager_noscript_after_body_code’ );
function add_google_tag_manager_noscript_after_body_code() {
echo ‘<!– Google Tag Manager (noscript) –>
<noscript><iframe src=”https://www.googletagmanager.com/ns.html?id=GTM-xxxxxxx” height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>
<!– End Google Tag Manager (noscript) –>’;
?>
<!– **AMP** for Google Analytics site tag –>
<amp-analytics type=”gtag” data-credentials=”include”>
<script type=”application/json”>
{
“vars” : {“gtag_id”: “UA-xxxxxxxxx-x”,
“config” : {“UA-xxxxxxxxx-x”: { “groups”: “default” } }
}
}
</script>
</amp-analytics>
<?php
}You must use Universal Analytics so put your own number in place of mine above.
Google Tag manager still doesn’t work but I just left the code in anyway. It could probably be removed.
- The topic ‘How to make google analytics tag work. (Really)’ is closed to new replies.