diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 0e0fa9b75ca..b5dbd116ee4 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -682,8 +682,22 @@ function amp_print_analytics( $analytics ) { if ( '' === $analytics ) { $analytics = []; } + $analytics_entries = amp_get_analytics( $analytics ); + /** + * Triggers before analytics entries are printed as amp-analytics tags. + * + * This is useful for printing additional `amp-analytics` tags to the page without having to refactor any existing + * markup generation logic to use the data structure mutated by the `amp_analytics_entries` filter. For such cases, + * this action should be used for printing `amp-analytics` tags as opposed to using the `wp_footer` and + * `amp_post_template_footer` actions; this will ensure analytics will also be included on AMP Stories. + * + * @since 1.3 + * @param array $analytics_entries Analytics entries, already potentially modified by the amp_analytics_entries filter. + */ + do_action( 'amp_print_analytics', $analytics_entries ); + if ( empty( $analytics_entries ) ) { return; } diff --git a/tests/php/test-amp-analytics-options.php b/tests/php/test-amp-analytics-options.php index 36dfd3d367e..76bab7bac99 100644 --- a/tests/php/test-amp-analytics-options.php +++ b/tests/php/test-amp-analytics-options.php @@ -261,8 +261,19 @@ public function test_amp_print_analytics() { $analytics = amp_get_analytics(); + $trigger_count = 0; + add_action( + 'amp_print_analytics', + function ( $entries ) use ( $analytics, &$trigger_count ) { + $this->assertEquals( $analytics, $entries ); + $trigger_count++; + } + ); + $output = get_echo( 'amp_print_analytics', [ $analytics ] ); + $this->assertEquals( 1, $trigger_count ); + $this->assertStringStartsWith( 'assertContains( 'type="googleanalytics">