Last active
March 7, 2016 15:44
-
-
Save pshapiro/a87b6affcb44ea51f7ad to your computer and use it in GitHub Desktop.
SEL - Enable Google Analytics for AMP WordPress Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'amp_post_template_head', 'amp_post_template_add_analytics_js' ); | |
function amp_post_template_add_analytics_js( $amp_template ) { | |
$post_id = $amp_template->get( 'post_id' ); | |
?> | |
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script> | |
<?php | |
} | |
add_action( 'amp_post_template_footer', 'xyz_amp_add_analytics' ); | |
function xyz_amp_add_analytics( $amp_template ) { | |
$post_id = $amp_template->get( 'post_id' ); | |
?> | |
<amp-analytics type="googleanalytics" id="analytics1"> | |
<script type="application/json"> | |
{ | |
"vars": { | |
"account": "UA-XXXXX-Y" | |
}, | |
"triggers": { | |
"trackPageview": { | |
"on": "visible", | |
"request": "pageview" | |
} | |
} | |
} | |
</script> | |
</amp-analytics> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment