From de45c5caf278e0a969b0889b07dd42cf7353de5a Mon Sep 17 00:00:00 2001 From: JustinSainton Date: Mon, 14 Jul 2014 07:30:07 +0000 Subject: [PATCH] Add the ability to exclude specific custom post types from tracking. Also adds helper function for esc_js_deep(). Fixes #21. --- analytics-wordpress.php | 66 +++++++++++++++++++++++++---- includes/class.segment-settings.php | 30 ++++++++++++- integrations/intercom.php | 2 +- templates/identify.php | 2 +- templates/page.php | 2 +- templates/track.php | 2 +- 6 files changed, 90 insertions(+), 14 deletions(-) diff --git a/analytics-wordpress.php b/analytics-wordpress.php index 0df0282..c2f070b 100644 --- a/analytics-wordpress.php +++ b/analytics-wordpress.php @@ -248,7 +248,7 @@ class Segment_Analytics_WordPress { 'track_login_page' => false, // Whether or not we should track custom events for the Search page. - 'track_searches' => 1 + 'exclude_custom_post_types' => array(), ); /** @@ -469,6 +469,11 @@ public function register_settings() { 'title' => __( 'Track Posts', 'segment' ), 'callback' => array( 'Segment_Settings', 'track_posts_callback' ), ), + array( + 'name' => 'exclude_post_types', + 'title' => __( 'Exclude Post Types', 'segment' ), + 'callback' => array( 'Segment_Settings', 'exclude_custom_post_types' ), + ), array( 'name' => 'track_pages', 'title' => __( 'Track Pages', 'segment' ), @@ -888,14 +893,18 @@ private function get_current_page_track() { // we filter those out. The event name is based on the post's type, // and is uppercased. if ( is_single() && ! is_attachment() ) { - $categories = implode( ', ', wp_list_pluck( get_categories( get_the_ID() ), 'name' ) ); - $track = array( - 'event' => sprintf( __( 'Viewed %s', 'segment' ), ucfirst( get_post_type() ) ), - 'properties' => array( - 'title' => single_post_title( '', false ), - 'category' => $categories - ) - ); + + if ( ! self::is_excluded_post_type() ) { + $categories = implode( ', ', wp_list_pluck( get_categories( get_the_ID() ), 'name' ) ); + $track = array( + 'event' => sprintf( __( 'Viewed %s', 'segment' ), ucfirst( get_post_type() ) ), + 'properties' => array( + 'title' => single_post_title( '', false ), + 'category' => $categories + ) + ); + } + } } @@ -1131,6 +1140,45 @@ public static function setup_settings() { update_option( Segment_Analytics_WordPress::get_instance()->get_option_name(), Segment_Analytics_WordPress::get_instance()->defaults ); } + /** + * Helper function, essentially a replica of stripslashes_deep, but for esc_js. + * + * @since 1.0.0 + * + * @param mixed $value Handles arrays, strings and objects that we are trying to escape for JS. + * @return mixed $value esc_js()'d value. + */ + public static function esc_js_deep( $value ) { + if ( is_array( $value ) ) { + $value = array_map( array( __CLASS__, 'esc_js_deep' ), $value ); + } elseif ( is_object( $value ) ) { + $vars = get_object_vars( $value ); + foreach ( $vars as $key => $data ) { + $value->{$key} = self::esc_js_deep( $data ); + } + } elseif ( is_string( $value ) ) { + $value = esc_js( $value ); + } + + return $value; + } + + /** + * Checks if current post type is excluded or not. + * Intended to be used on singular views. + * + * @since 1.0.0 + * + * @return boolean Whether or not post type is excluded + */ + public static function is_excluded_post_type() { + $settings = $this->get_settings(); + + $cpts = isset( $settings['exclude_custom_post_types'] ) ? $settings['exclude_custom_post_types'] : array(); + + return in_array( get_post_type(), $cpts ); + } + } register_activation_hook( __FILE__, array( 'Segment_Analytics_WordPress', 'setup_settings' ) ); diff --git a/includes/class.segment-settings.php b/includes/class.segment-settings.php index 3d58340..08c8e1c 100644 --- a/includes/class.segment-settings.php +++ b/includes/class.segment-settings.php @@ -148,13 +148,39 @@ public static function track_search_callback() { ?>

true, '_builtin' => false ), 'objects' ); + $settings = Segment_Analytics_WordPress::get_instance()->get_settings(); + $name = Segment_Analytics_WordPress::get_instance()->get_option_name() . '[exclude_custom_post_types][]'; + + $settings['exclude_custom_post_types'] = isset( $settings['exclude_custom_post_types'] ) ? $settings['exclude_custom_post_types'] : array(); + + foreach ( $cpts as $cpt ) { + ?> + + +

+ user_registered; } - $identify['options'] = is_array( $identify['options'] ) ? $identify['options'] : array(); + $identify['options'] = isset( $identify['options'] ) ? $identify['options'] : array(); $identify['options']['Intercom'] = array( 'user_hash' => hash( 'sha256', $settings['use_intercom_secure_mode'] . $user_email ) diff --git a/templates/identify.php b/templates/identify.php index aba7ab2..c23e23d 100644 --- a/templates/identify.php +++ b/templates/identify.php @@ -1,3 +1,3 @@ \ No newline at end of file diff --git a/templates/page.php b/templates/page.php index 78b6f07..f970c6c 100644 --- a/templates/page.php +++ b/templates/page.php @@ -1,5 +1,5 @@