Last active
September 26, 2023 14:23
-
-
Save swissspidy/215bd5ab6ef93b548a93596d2aefe9b2 to your computer and use it in GitHub Desktop.
Mini-plugin to add an additional Google Analytics tracking code to web stories.
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
<?php | |
/** | |
* Plugin Name: Web Stories Custom Google Analytics | |
* 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 | |
*/ | |
add_action( | |
'web_stories_print_analytics', | |
static function () { | |
// TODO: REPLACE WITH YOUR OWN GOOGLE ANALYTICS MEASUREMENT ID. | |
$tracking_id = 'G-12345'; | |
?> | |
<amp-analytics type="gtag" data-credentials="include"> | |
<script type="application/json"> | |
{ | |
"vars": { | |
"gtag_id": "<?php echo esc_attr( $tracking_id ); ?>", | |
"config": { | |
"<?php echo esc_attr( $tracking_id ); ?>": { | |
"groups": "default" | |
} | |
} | |
}, | |
"triggers": { | |
"trackClickThrough": { | |
"on": "story-click-through", | |
"tagName": "a", | |
"request": "click ", | |
"vars": { | |
"event_name": "custom", | |
"event_action": "story_click_through", | |
"event_category": "${title}", | |
"send_to": "<?php echo esc_attr( $tracking_id ); ?>" | |
} | |
}, | |
"pageAttachmentEnter": { | |
"on": "story-page-attachment-enter", | |
"request": "event", | |
"vars": { | |
"event_name": "custom", | |
"event_action": "story_page_attachment_enter", | |
"event_category": "${title}", | |
"send_to": "<?php echo esc_attr( $tracking_id ); ?>" | |
} | |
} | |
} | |
} | |
</script> | |
</amp-analytics> | |
<?php | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment