Skip to content

Instantly share code, notes, and snippets.

@swissspidy
Last active November 23, 2023 09:43
Show Gist options
  • Save swissspidy/d60b152c9676b7315f37cd82460e6adf to your computer and use it in GitHub Desktop.
Save swissspidy/d60b152c9676b7315f37cd82460e6adf to your computer and use it in GitHub Desktop.
Mini-plugin to add an additional Google Analytics tracking code to web stories
<?php
/**
* Plugin Name: Web Stories Additional GA Tracking
* Description: Mini-plugin to add an additional Google Analytics tracking code to web stories
* Plugin URI: https://wp.stories.google/
* Author: Pascal Birchler, Google
* Author URI: https://opensource.google.com/
* Version: 0.0.1
* License: Apache License 2.0
* License URI: https://www.apache.org/licenses/LICENSE-2.0
*/
// SEE https://amp.dev/documentation/components/amp-pixel/
add_action(
'web_stories_print_analytics',
static function () {
// TODO: REPLACE WITH YOUR OWN GOOGLE ANALYTICS MEASUREMENT ID.
$tracking_id = 'G-12345';
?>
<amp-story-auto-analytics gtag-id="<?php echo esc_attr( $tracking_id ); ?>"></amp-story-auto-analytics>
<?php
}
);
@swissspidy
Copy link
Author

In case someone stumbles upon this and would like to disable Site Kit's analytics output on stories (so that you can add a separate one using this plugin here), you can do so using the following filter:

add_filter( 'googlesitekit_analytics-4_tag_amp_blocked', static function( $blocked ) { if ( is_singular( 'web-story' ) ) { return true; } return $blocked; } );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment