Skip to content

Commit

Permalink
Add the ability to exclude specific custom post types from tracking. …
Browse files Browse the repository at this point in the history
…Also adds helper function for esc_js_deep(). Fixes #21.
  • Loading branch information
JustinSainton committed Jul 14, 2014
1 parent 816a1f9 commit de45c5c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
66 changes: 57 additions & 9 deletions analytics-wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

/**
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -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
)
);
}

}
}

Expand Down Expand Up @@ -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' ) );
Expand Down
30 changes: 29 additions & 1 deletion includes/class.segment-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,39 @@ public static function track_search_callback() {
?>
<label for="track_searches">
<input name="<?php echo esc_attr( $name ); ?>" type="checkbox" id="track_searches" value="1" <?php checked( 1, $settings['track_searches'] ); ?> />
<?php _e( '.', 'segment' ); ?>
<?php _e( 'Automatically track events when your users view the search results page.', 'segment' ); ?>
</label>
<p class="description"><?php _e( 'These will be "Viewed Search Page" events with a &ldquo;query&rdquo; property.', 'segment' ); ?></p>
<?php

}

/**
* [exclude_custom_post_types description]
*
* @todo If no post types exist, we shouldn't show this setting at all.
* @return [type] [description]
*/
public static function exclude_custom_post_types() {
$cpts = get_post_types( array( 'public' => 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 ) {
?>
<label for="exclude_custom_post_types_<?php echo esc_attr( $cpt->name ); ?>">
<input name="<?php echo esc_attr( $name ); ?>" type="checkbox" id="exclude_custom_post_types_<?php echo esc_attr( $cpt->name ); ?>" value="<?php echo esc_attr( $cpt->name ); ?>" <?php checked( in_array( $cpt->name, (array) $settings['exclude_custom_post_types'] ) ); ?> />
<?php echo esc_html( $cpt->label ); ?><br />
</label>
<?php
}
?>
<p class="description"><?php _e( 'Select which, if any, custom post types to exclude tracking on. Selecting a post type here will exclude tracking on the single and archive pages for the post type.', 'segment' ); ?></p>
<?php
}

/**
* Our core validation routine.
*
Expand Down Expand Up @@ -189,6 +215,8 @@ public static function core_validation( $input ) {

$input[ $int ] = isset( $input[ $int ] ) ? absint( $input[ $int ] ) : '';



return apply_filters( 'segment_settings_core_validation', $input );
}

Expand Down
2 changes: 1 addition & 1 deletion integrations/intercom.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function segment_intercom_integration( $identify, $settings ) {
$identify['traits']['created'] = $user->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 )
Expand Down
2 changes: 1 addition & 1 deletion templates/identify.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<script type="text/javascript">
analytics.identify( <?php echo '"' . intval( $user_id ) . '"' ?><?php if ( ! empty( $traits ) ) { echo ', ' . json_encode( array_map( 'esc_js', $traits ) ); } else { echo ', {}'; } ?><?php if ( ! empty( $options ) ) { echo ', ' . json_encode( array_map( 'esc_js', $options ) ); } ?>);
analytics.identify( <?php echo '"' . intval( $user_id ) . '"' ?><?php if ( ! empty( $traits ) ) { echo ', ' . json_encode( Segment_Analytics_WordPress::esc_js_deep( $traits ) ); } else { echo ', {}'; } ?><?php if ( ! empty( $options ) ) { echo ', ' . json_encode( Segment_Analytics_WordPress::esc_js_deep( $options ) ); } ?>);
</script>
2 changes: 1 addition & 1 deletion templates/page.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script type="text/javascript">
analytics.page(<?php echo '"' . esc_js( $category ) . '"' ?><?php if ( ! empty( $name ) ) echo ', "' . esc_js( $name ) . '"' ?><?php if ( ! empty( $properties ) ) { echo ', ' . json_encode( array_map( 'esc_js', $properties ) ); } else { echo ', {}'; } ?><?php if ( ! empty( $options ) ) { echo ', ' . json_encode( array_map( 'esc_js', $options ) ); } ?>);
analytics.page(<?php echo '"' . esc_js( $category ) . '"' ?><?php if ( ! empty( $name ) ) echo ', "' . esc_js( $name ) . '"' ?><?php if ( ! empty( $properties ) ) { echo ', ' . json_encode( Segment_Analytics_WordPress::esc_js_deep( $properties ) ); } else { echo ', {}'; } ?><?php if ( ! empty( $options ) ) { echo ', ' . json_encode( Segment_Analytics_WordPress::esc_js_deep( $options ) ); } ?>);
<?php
if ( $http_event ) :
?>
Expand Down
2 changes: 1 addition & 1 deletion templates/track.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script type="text/javascript">
analytics.track(<?php echo '"' . esc_js( $event ) . '"' ?><?php if ( ! empty( $properties ) ) { echo ', ' . json_encode( array_map( 'esc_js', $properties ) ); } else { echo ', {}'; } ?><?php if ( ! empty( $options ) ) { echo ', ' . json_encode( array_map( 'esc_js', $options ) ); } ?>);
analytics.track(<?php echo '"' . esc_js( $event ) . '"' ?><?php if ( ! empty( $properties ) ) { echo ', ' . json_encode( Segment_Analytics_WordPress::esc_js_deep( $properties ) ); } else { echo ', {}'; } ?><?php if ( ! empty( $options ) ) { echo ', ' . json_encode( Segment_Analytics_WordPress::esc_js_deep( $options ) ); } ?>);
<?php
if ( $http_event ) :
?>
Expand Down

0 comments on commit de45c5c

Please sign in to comment.