Ask your WordPress questions! Pay money and get answers fast! (more info)

google analytics data for only author of post

  • SOLVED

hi, i have a multi-author blog where user from front end can submit a post, now i want display a google analytics data for only owners-author of post on the front end of website,

i have found 2 plugin that display analytics data of single post on front end

- https://wordpress.org/plugins/wd-google-analytics/

- https://wordpress.org/plugins/google-analytics-dashboard-for-wp/ ( i asked to the author of this plugin if i can do this, and the answer is yes but require a customization with action hook)


but , both display the analytics data to all user and not only author of post.

now i want ask if already exist a plugin to do this , or if possible create a snippet to restrict the plugin mentioned.

thanks

Answers (3)

2016-11-24

Rempty answers:

Hello markus88, this code will hide to non-administrator users the google analytics stats link, administrator users can see all.
add all this to your functions.php


if ( ! class_exists( 'GADWP_Backend_Item_Reports' ) ) {
final class GADWP_Backend_Item_Reports {
private $gadwp;
public function __construct() {
$this->gadwp = GADWP();

if ( GADWP_Tools::check_roles( $this->gadwp->config->options['ga_dash_access_back'] ) && 1 == $this->gadwp->config->options['backend_item_reports'] ) {
// Add custom column in Posts List
add_filter( 'manage_posts_columns', array( $this, 'add_columns' ) );

// Populate custom column in Posts List
add_action( 'manage_posts_custom_column', array( $this, 'add_icons' ), 10, 2 );

// Add custom column in Pages List
add_filter( 'manage_pages_columns', array( $this, 'add_columns' ) );

// Populate custom column in Pages List
add_action( 'manage_pages_custom_column', array( $this, 'add_icons' ), 10, 2 );
}
}

public function add_icons( $column, $id ) {
global $wp_version;
$current_user=get_currentuserinfo();
$post_author = get_post_field( 'post_author', $id );
$userdata = wp_get_current_user();

if ( $column != 'gadwp_stats' ) {
return;
}

if ( $post_author==$current_user->ID OR in_array( 'administrator', (array) $userdata->roles )) {
if ( version_compare( $wp_version, '3.8.0', '>=' ) ) {
echo '<a id="gadwp-' . $id . '" title="' . get_the_title( $id ) . '" href="#' . $id . '" class="gadwp-icon dashicons-before dashicons-chart-area"></a>';
} else {
echo '<a id="gadwp-' . $id . '" title="' . get_the_title( $id ) . '" href="#' . $id . '"><img class="gadwp-icon-oldwp" src="' . GADWP_URL . 'admin/images/gadash-icon.png"</a>';
}
}

}

public function add_columns( $columns ) {
return array_merge( $columns, array( 'gadwp_stats' => __( 'Analytics', 'google-analytics-dashboard-for-wp' ) ) );
}
}
}


if ( ! class_exists( 'GADWP_Frontend_Item_Reports' ) ) {

final class GADWP_Frontend_Item_Reports {

private $gadwp;

public function __construct() {
$this->gadwp = GADWP();

add_action( 'admin_bar_menu', array( $this, 'custom_adminbar_node' ), 999 );
}

function custom_adminbar_node( $wp_admin_bar ) {
if ( GADWP_Tools::check_roles( $this->gadwp->config->options['ga_dash_access_front'] ) && $this->gadwp->config->options['frontend_item_reports'] ) {
/* @formatter:off */
$args = array( 'id' => 'gadwp-1',
'title' => '<span class="ab-icon"></span><span class="">' . __( "Analytics", 'google-analytics-dashboard-for-wp' ) . '</span>',
'href' => '#1',
);
/* @formatter:on */
global $post;
$current_user=get_currentuserinfo();
$userdata = wp_get_current_user();
$post_author = get_post_field( 'post_author', $post->ID );
if ( $post_author==$current_user->ID OR in_array( 'administrator', (array) $userdata->roles )) {
$wp_admin_bar->add_node( $args );
}
}
}
}
}


//Will be nice if you can rise the prize :D


markus88 comments:

hi, thanks for reply, i have 2 problem with your code:

1) the homepage are displayed to all user
2) my standard user-author have role "subscriber" with this role never analytics data are displayed, this because then plugin display the data from role "Contributor"

thanks


Rempty comments:

where do you show the icon to display the analytics?
the plugin only allow in the post list admin or in the admin topbar


markus88 comments:

hi, yes the plugin display the button on topbar but only from role "contributor", I wish it were viewed through a snippet or any shortcode in single.php template

2016-11-24

Arnav Joy answers:

you want to show data on details page of the property post ?


markus88 comments:

yes, i want every user can display analytics data onlyof post owners, the mentioned plugin already have a option to display on front end the analytics data of single post, but this data are displayed to all user

2016-11-24

Prabin Parajuli answers:

Hello,

I think this reference site will help you to solve your problem.

The author has written details how you can add author tracking code on your WordPress site.

http://www.vsellis.com/add-author-tracking-google-analytics-wordpress-website/


markus88 comments:

hi, thanks for reply

i have already read this post, but this guide don't solve my problem, the mentioned plugin already have a option to view the data of the single post separated , but these data are displayed to all users, while I would like them to display only to the owners-author of the post