Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add amp_print_analytics action triggering before printing entries as amp-analytics #3106

Merged
merged 1 commit into from
Aug 29, 2019

Conversation

westonruter
Copy link
Member

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.

See:

@westonruter westonruter added Enhancement New feature or improvement of an existing one AMP Stories labels Aug 27, 2019
@westonruter westonruter added this to the v1.3 milestone Aug 27, 2019
@googlebot googlebot added the cla: yes Signed the Google CLA label Aug 27, 2019
Copy link
Collaborator

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Copy link
Collaborator

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter Question regarding Reader mode: Does the amp-analytics extension get loaded there automatically? I was under the assumption that, so far, it only was if there were actual Analytics entries in the AMP plugin's option. However, now that there is the hook, there might be a need for the extension even without entries from the option.

Can you clarify?

@westonruter
Copy link
Member Author

westonruter commented Aug 29, 2019

Question regarding Reader mode: Does the amp-analytics extension get loaded there automatically? I was under the assumption that, so far, it only was if there were actual Analytics entries in the AMP plugin's option. However, now that there is the hook, there might be a need for the extension even without entries from the option.

@felixarntz Excellent point. Currently if the $entries is empty then the amp-analytics extension will not be included by default:

'amp_analytics' => amp_add_custom_analytics(),

function amp_add_custom_analytics( $analytics = [] ) {
$analytics = amp_get_analytics( $analytics );
/**
* Add amp-analytics tags.
*
* This filter allows you to easily insert any amp-analytics tags without needing much heavy lifting.
* This filter should be used to alter entries for legacy AMP templates.
*
* @since 0.4
*
* @param array $analytics An associative array of the analytics entries we want to output. Each array entry must have a unique key, and the value should be an array with the following keys: `type`, `attributes`, `script_data`. See readme for more details.
* @param WP_Post $post The current post.
*/
$analytics = apply_filters( 'amp_post_template_analytics', $analytics, get_queried_object() );
return $analytics;
}

/**
* Add analytics scripts.
*
* @param array $data Data.
* @return array Data.
*/
function amp_post_template_add_analytics_script( $data ) {
if ( ! empty( $data['amp_analytics'] ) ) {
$data['amp_component_scripts']['amp-analytics'] = 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js';
}
return $data;
}

So you will need to make sure that Site Kit does:

add_filter(
	'amp_post_template_data',
	function( $data ) {
		$data['amp_component_scripts'] = array_merge(
			$data['amp_component_scripts'],
			array(
				'amp-analytics' => true,
			)
		);
		return $data;
	}
);

This will be needed until the entire Reader mode templates are sent through the sanitizers, at which point the required components will be automatically detected. See #2202.

@westonruter westonruter merged commit 75674df into develop Aug 29, 2019
@westonruter westonruter deleted the add/amp_print_analytics_event branch August 29, 2019 20:22
@felixarntz
Copy link
Collaborator

@westonruter

Currently if the $entries is empty then the amp-analytics extension will not be included by default:

For reader mode, Site Kit already covers that. How about AMP Stories though? How do I make sure the extension gets loaded in the AMP Stories template? That happens by the sanitizer too I assume?

@westonruter
Copy link
Member Author

Correct. The AMP Stories single template is post-processed in its entirety, so any component script requirements are auto-detected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA Enhancement New feature or improvement of an existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants