Skip to content

Commit

Permalink
Merge pull request #900 from Automattic/fix/874-analytics
Browse files Browse the repository at this point in the history
Add analytics to theme support
  • Loading branch information
westonruter authored Feb 3, 2018
2 parents 5ac374f + a2c290a commit fc6d81c
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 49 deletions.
1 change: 0 additions & 1 deletion amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function amp_after_setup_theme() {
add_action( 'widgets_init', 'AMP_Theme_Support::register_widgets' ); // @todo Let this be called by AMP_Theme_Support::init().
add_action( 'init', 'AMP_Theme_Support::setup_commenting' ); // @todo Let this be called by AMP_Theme_Support::init().
add_action( 'admin_init', 'AMP_Options_Manager::register_settings' );
add_filter( 'amp_post_template_analytics', 'amp_add_custom_analytics' );
add_action( 'wp_loaded', 'amp_post_meta_box' );
add_action( 'wp_loaded', 'amp_add_options_menu' );
add_action( 'parse_query', 'amp_correct_query_when_is_front_page' );
Expand Down
32 changes: 19 additions & 13 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,29 @@ function amp_add_options_menu() {
/**
* Add custom analytics.
*
* This is currently only used for legacy AMP post templates.
*
* @since 0.5
* @see amp_get_analytics()
*
* @param array $analytics Analytics.
* @return array Analytics.
*/
function amp_add_custom_analytics( $analytics ) {
$analytics_entries = AMP_Options_Manager::get_option( 'analytics', array() );
function amp_add_custom_analytics( $analytics = array() ) {
$analytics = amp_get_analytics( $analytics );

if ( ! $analytics_entries ) {
return $analytics;
}

foreach ( $analytics_entries as $entry_id => $entry ) {
$analytics[ $entry_id ] = array(
'type' => $entry['type'],
'attributes' => array(),
'config_data' => json_decode( $entry['config'] ),
);
}
/**
* 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;
}
Expand Down
72 changes: 72 additions & 0 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,78 @@ function amp_print_boilerplate_code() {
echo '<noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>';
}

/**
* Retrieve analytics data added in backend.
*
* @since 0.7
*
* @param array $analytics Analytics entries.
* @return array Analytics.
*/
function amp_get_analytics( $analytics = array() ) {
$analytics_entries = AMP_Options_Manager::get_option( 'analytics', array() );

/**
* 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 paired mode.
*
* @since 0.7
*
* @param array $analytics_entries 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.
*/
$analytics_entries = apply_filters( 'amp_analytics_entries', $analytics_entries );

if ( ! $analytics_entries ) {
return $analytics;
}

foreach ( $analytics_entries as $entry_id => $entry ) {
$analytics[ $entry_id ] = array(
'type' => $entry['type'],
'attributes' => array(),
'config_data' => json_decode( $entry['config'] ),
);
}

return $analytics;
}

/**
* Print analytics data.
*
* @since 0.7
*
* @param array $analytics Analytics entries.
*/
function amp_print_analytics( $analytics ) {
$analytics_entries = amp_get_analytics( $analytics );

if ( empty( $analytics_entries ) ) {
return;
}

// Can enter multiple configs within backend.
foreach ( $analytics_entries as $id => $analytics_entry ) {
if ( ! isset( $analytics_entry['type'], $analytics_entry['attributes'], $analytics_entry['config_data'] ) ) {
/* translators: %1$s is analytics entry ID, %2$s is actual entry keys. */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %1$s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %2$s)', 'amp' ), esc_html( $id ), esc_html( implode( ', ', array_keys( $analytics_entry ) ) ) ), '0.3.2' );
continue;
}
$script_element = AMP_HTML_Utils::build_tag( 'script', array(
'type' => 'application/json',
), wp_json_encode( $analytics_entry['config_data'] ) );

$amp_analytics_attr = array_merge( array(
'id' => $id,
'type' => $analytics_entry['type'],
), $analytics_entry['attributes'] );

echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element ); // WPCS: XSS OK.
}
}

/**
* Get content embed handlers.
*
Expand Down
28 changes: 4 additions & 24 deletions includes/amp-post-template-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,11 @@ function amp_post_template_add_analytics_script( $data ) {
/**
* Print analytics data.
*
* @param AMP_Post_Template $amp_template Template.
* @since 0.3.2
*/
function amp_post_template_add_analytics_data( $amp_template ) {
$analytics_entries = $amp_template->get( 'amp_analytics' );
if ( empty( $analytics_entries ) ) {
return;
}

foreach ( $analytics_entries as $id => $analytics_entry ) {
if ( ! isset( $analytics_entry['type'], $analytics_entry['attributes'], $analytics_entry['config_data'] ) ) {
/* translators: %1$s is analytics entry ID, %2$s is actual entry keys. */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %1$s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %2$s)', 'amp' ), esc_html( $id ), esc_html( implode( ', ', array_keys( $analytics_entry ) ) ) ), '0.3.2' );
continue;
}
$script_element = AMP_HTML_Utils::build_tag( 'script', array(
'type' => 'application/json',
), wp_json_encode( $analytics_entry['config_data'] ) );

$amp_analytics_attr = array_merge( array(
'id' => $id,
'type' => $analytics_entry['type'],
), $analytics_entry['attributes'] );

echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element ); // WPCS: XSS OK.
}
function amp_post_template_add_analytics_data() {
$analytics = amp_add_custom_analytics();
amp_print_analytics( $analytics );
}

/**
Expand Down
2 changes: 2 additions & 0 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public static function register_hooks() {
add_action( 'wp_head', 'amp_add_generator_metadata', 20 );
add_action( 'wp_head', 'amp_print_schemaorg_metadata' );

add_action( 'wp_footer', 'amp_print_analytics' );

/*
* Disable admin bar because admin-bar.css (28K) and Dashicons (48K) alone
* combine to surpass the 50K limit imposed for the amp-custom style.
Expand Down
12 changes: 1 addition & 11 deletions includes/templates/class-amp-post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,7 @@ public function __construct( $post ) {

'post_amp_styles' => array(),

/**
* Add amp-analytics tags.
*
* This filter allows you to easily insert any amp-analytics tags without needing much heavy lifting.
*
* @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.
*/
'amp_analytics' => apply_filters( 'amp_post_template_analytics', array(), $this->post ),
'amp_analytics' => amp_add_custom_analytics(),
);

$this->build_post_content();
Expand Down
48 changes: 48 additions & 0 deletions tests/test-amp-analytics-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,52 @@ function test_two_analytics_components_added() {
libxml_use_internal_errors( $libxml_previous_state );

}

/**
* Test amp_get_analytics()
*
* @covers amp_get_analytics()
*/
public function test_amp_get_analytics() {
$this->insert_one_option(
$this->vendor,
$this->config_one
);

$analytics = amp_get_analytics();
$this->assertEquals( 1, count( $analytics ) );

$key = key( $analytics );
$this->assertArrayHasKey( 'type', $analytics[ $key ] );
$this->assertEquals( 'googleanalytics', $analytics[ $key ]['type'] );

add_theme_support( 'amp' );
add_filter( 'amp_analytics_entries', function( $analytics ) use ( $key ) {
$analytics[ $key ]['type'] = 'test';
return $analytics;
} );
$analytics = amp_get_analytics();
$this->assertEquals( 'test', $analytics[ $key ]['type'] );
}

/**
* Test amp_print_analytics()
*
* @covers amp_print_analytics()
*/
public function test_amp_print_analytics() {
$this->insert_one_option(
$this->vendor,
$this->config_one
);

$analytics = amp_get_analytics();

ob_start();
amp_print_analytics( $analytics );
$output = ob_get_clean();

$this->assertEquals( 0, strpos( $output, '<amp-analytics' ) );
}

}

0 comments on commit fc6d81c

Please sign in to comment.