Skip to content

Instantly share code, notes, and snippets.

@swissspidy
Last active November 23, 2023 09:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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
}
);
@ilsajjh
Copy link

ilsajjh commented Nov 18, 2022

Hi Pascal,

Thank you very much for sharing this mini plugin.

Could you please explain how I can install it on my wordpress website?

Thank you,
Keesha

@swissspidy
Copy link
Author

@ilsajjh Click on "Download ZIP" at the top and then go to WordPress admin under Plugins -> Add New and upload the ZIP file as a new plugin to activate.

@ilsajjh
Copy link

ilsajjh commented Nov 18, 2022

Perfect, thank you. So if I understand correctly, this will help me in tracking the traffic from the web stories separately. Now, do you know how I can ensure that the overall analytics (Site kit) will no longer include the web stories traffic? Basically, I want to make sure I can show the traffic to my website without the web stories traffic. Many thanks for your help!

@swissspidy
Copy link
Author

I don't think there's currently a way to do that. I recommend asking in the Site Kit support forums for a way to disable the analytics output on specific pages like single stories.

@mikezielonka
Copy link

@ilsajjh @swissspidy I use the Code Snippets Plugin to add code like this to my customer's sites. https://wordpress.org/plugins/code-snippets/. Adding it as a stand alone plugin works great too

This solution will store the web stories traffic in a separate property in GA. The other option is to use 2 GA views and create a filter to exclude traffic starting with /web-stories/ in your main view and then only include traffic from them in a new view.

@ilsajjh
Copy link

ilsajjh commented Nov 18, 2022

@mikezielonka thank you so much for the feedback. Would you have the capacity for such a booking for my website?

@mikezielonka
Copy link

@ilsajjh shoot me an email. me at mikezielonka dot com

@moniquebocardi
Copy link

Hi!

Can this mini plugin work with GA4?

@swissspidy
Copy link
Author

No, GA4 does not currently support AMP / Web Stories.

@mikezielonka
Copy link

@swissspidy I've started seeing issues with this little plugin on sites running site kit. here is an example of where I'm seeing it: https://kitchenconfidante.com/web-stories/coconut-ube-rolls

@swissspidy
Copy link
Author

@mikezielonka Define "issues"? I see an amp-story-auto-analytics tag with UA-16723097-3 and a regular one by SK with UA-16723097-2. Plus an amp-pixel by Jetpack. No AMP validation issues.

Note that amp-story-auto-analytics and the regular amp-analytics by SK track slightly different things.

Depending on the issues you're seeing, I'd recommend opening a topic in our support forums or check out the GA help center. The comments section here is not the most ideal place.

@mikezielonka
Copy link

@swissspidy I figured I'd start here in case anyone else is running into he issue.

Most of my creator and blogger customers have to track web stories separate from their main UA and GA4 properties for their ad networks. Something changed, which is causing both accounts to fire on web stories when in the past they didnt.

the link to the dot org post is here.

@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