get_error_message();
echo "Something went wrong: $error_message";
} else {
print_r($response['body']);
}
die();
}
/*
* Show Analytics of single post/page in wp-admin under EDIT screen.
*/
public static function show_admin_single_analytics() {
global $post;
$back_exclude_posts = explode( ',', get_option( 'post_analytics_exclude_posts_back' ));
if ( is_array( $back_exclude_posts ) ) {
if ( in_array( $post->ID, $back_exclude_posts ) ) {
_e('This post is excluded and will not show Analytics.');
return;
}
}
$urlPost = '';
$wp_analytify = new WP_Analytify();
$urlPost = parse_url( get_permalink( $post->ID ) );
$start_date = ''; $end_date = ''; $urlpost = '' ;
$is_access_level = get_option( 'post_analytics_access_back' );
if( $wp_analytify->pa_check_roles( $is_access_level ) ){ ?>
get_single_admin_analytics( $start_date, $end_date, $post->ID, 0 ); ?>
roles;
if ( isset( $roles[0] ) and in_array( $roles[0], get_option( 'display_tracking_code' ) )) {
}
else{
echo ' ';
if ( get_option( 'analytify_tracking_code' ) == 'universal' ) { ?>
';
}
}
/**
* Add a link to the settings page to the plugins list
*/
public function pa_plugin_links( $links, $file ) {
static $this_plugin;
if ( empty( $this_plugin ) ){
$this_plugin = 'wp-analytify/wp-analytify.php';
}
if ( $file == $this_plugin ) {
$settings_link = '';
echo '
';
echo '
';
foreach( $tabs as $tab => $name ) {
$class = ( $tab == $current ) ? ' nav-tab-active' : '';
echo "$name";
}
echo '
';
}
/**
* Get profiles from user Google Analytics account profiles.
*/
public function pt_get_analytics_accounts() {
try {
if( get_option( 'pa_google_token' ) !='' ) {
$profiles = $this->service->management_profiles->listManagementProfiles( "~all", "~all" );
return $profiles;
}
else{
echo '
' . __( 'You must authenticate to access your web profiles.', 'wp-analytify' ) . '
';
}
}
catch (Exception $e) {
die('An error occured: ' . $e->getMessage() . '\n');
}
}
public function pa_setting_url() {
return admin_url('admin.php?page=analytify-settings');
}
public function pt_save_data( $key_google_token ) {
update_option( 'post_analytics_token', $key_google_token );
$this->pa_connect();
return true;
}
/**
* Warning messages.
*/
public function profile_warning() {
$profile_id = get_option( "pt_webprofile" );
$acces_token = get_option( "post_analytics_token" );
if (! isset( $acces_token ) || empty( $acces_token )) {
echo "
" . __( "Analytify is not active. Please Authenticate in order to get started using this plugin.", 'wp-analytify' )."
";
}
else{
if (! isset( $profile_id ) || empty( $profile_id )){
echo '
' . __( 'Google Analytics Profile is not set. Set the Profile ', 'wp-analytify' ) . '
';
}
}
}
public function get_single_front_analytics( $content ) {
global $post, $wp_analytify;
$front_access = get_option( 'post_analytics_access' );
if ( is_single() || is_page() ) {
$post_type = get_post_type( $post->ID );
if( strlen( get_option( 'analytify_posts_stats_front' ) < 3) ) {
return $content;
}
if( is_array( get_option( 'analytify_posts_stats_front' )) and !in_array( $post_type, get_option( 'analytify_posts_stats_front' ) ) ) {
return $content;
}
if ( is_array( get_option( 'post_analytics_exclude_posts_front' ) ) ) {
if ( in_array( get_the_ID(), get_option( 'post_analytics_exclude_posts_front' ) ) ) {
return $content;
}
}
// Showing stats to guests
if ( $front_access[0] == 'every-one' || $this->pa_check_roles( $front_access )) {
$post_analytics_settings_front = array();
$post_analytics_settings_front = get_option( 'post_analytics_settings_front' );
$urlPost = parse_url( get_permalink( $post->ID ) );
if ( $urlPost['host'] == 'localhost' )
$filter = 'ga:pagePath==/'; //.$u_post['path'];
else
$filter = 'ga:pagePath==' .$urlPost['path']. '';
if( get_the_time('Y', $post->ID) < 2005 ) {
$start_date = '2005-01-01';
}
else {
$start_date = get_the_time('Y-m-d', $post->ID);
}
$end_date = date('Y-m-d');
ob_start();
include ANALYTIFY_ROOT_PATH . '/inc/front-menus.php';
if(! empty( $post_analytics_settings_front )){
if (is_array( $post_analytics_settings_front )){
$stats = $this->pa_get_analytics( 'ga:sessions,ga:bounces,ga:newUsers,ga:entrances,ga:pageviews,ga:sessionDuration,ga:avgSessionDuration,ga:users',$start_date, $end_date, false, false, $filter);
if ( isset( $stats->totalsForAllResults ) ) {
include ANALYTIFY_ROOT_PATH . '/views/front/general-stats.php';
pa_include_general( $this, $stats);
}
}
}
$content .= ob_get_contents();
ob_get_clean();
}
}
return $content;
}
/*
* get the Analytics data from ajax() call
*
*/
public function get_ajax_single_admin_analytics() {
$startDate = '';
$endDate = '';
$postID = 0 ;
$startDate = $_POST['start_date'];
$endDate = $_POST['end_date'];
$postID = $_POST['post_id'];
$this->get_single_admin_analytics( $startDate, $endDate, $postID, 1 );
die();
}
/*
* get the Analytics data for wp-admin posts/pages.
*
*/
public function get_single_admin_analytics( $start_date = '', $end_date = '', $postID = 0, $ajax = 0 ) {
global $post;
//$urlPost = parse_url( get_permalink( $post->ID ) );
if ( $postID == 0 ) {
$u_post = '/'; //$urlPost['path'];
}
else{
$u_post = parse_url( get_permalink( $postID ) );
}
if ( $u_post['host'] == 'localhost' )
$filter = false;
else
$filter = 'ga:pagePath==' .$u_post['path']. '';
if ( $start_date == '' ) {
$s_date = get_the_time('Y-m-d', $post->ID);
if(get_the_time('Y', $post->ID) < 2005){
$s_date = '2005-01-01';
}
}
else{
$s_date = $start_date;
}
if ( $end_date == '' ) {
$e_date = date('Y-m-d');
}
else{
$e_date = $end_date;
}
$show_settings = array();
$show_settings = get_option('post_analytics_settings_back');
// Stop here, if user has disable backend analytics i.e OFF
if ( get_option( 'post_analytics_disable_back' ) == 1 and $ajax == 0) {
return;
}
echo '
Displaying Analytics of this page from ' . date("jS F, Y", strtotime($s_date)) . ' to '. date("jS F, Y", strtotime($e_date)) . '
';
if( !empty( $show_settings )){
if (is_array( $show_settings )){
if (in_array( 'show-overall-back', $show_settings )) {
$stats = $this->pa_get_analytics( 'ga:sessions,ga:bounces,ga:newUsers,ga:entrances,ga:pageviews,ga:sessionDuration,ga:avgSessionDuration,ga:users',$s_date, $e_date, false, false, $filter);
if ( isset( $stats->totalsForAllResults ) ) {
include ANALYTIFY_ROOT_PATH . '/views/admin/single-general-stats.php';
wpa_single_include_general( $this, $stats);
}
}
}
}
}
/*
* Pretty numbers
*/
function wpa_pretty_numbers( $num ) {
return round(($num/1000),2).'k';
}
/*
* format numbers
*/
function wpa_number_format( $num ) {
return number_format($num);
}
/*
* Pretty time to display
*/
function pa_pretty_time( $time ) {
// Check if numeric
if ( is_numeric($time) ) {
$value = array(
"years" => '00',
"days" => '00',
"hours" => '',
"minutes" => '',
"seconds" => ''
);
if ($time >= 31556926) {
$value["years"] = floor($time / 31556926);
$time = ($time % 31556926);
} //$time >= 31556926
if ($time >= 86400)
{
$value["days"] = floor($time / 86400);
$time = ($time % 86400);
} //$time >= 86400
if ($time >= 3600)
{
$value["hours"] = str_pad(floor($time / 3600), 1, 0, STR_PAD_LEFT);
$time = ($time % 3600);
} //$time >= 3600
if ($time >= 60)
{
$value["minutes"] = str_pad(floor($time / 60), 1, 0, STR_PAD_LEFT);
$time = ($time % 60);
} //$time >= 60
$value["seconds"] = str_pad(floor($time), 1, 0, STR_PAD_LEFT);
# Get the hour:minute:second version
if($value['hours']!=''){
$attach_hours='
h ';
}
if($value['minutes']!=''){
$attach_min='
min ';
}
if($value['seconds']!=''){
$attach_sec='
sec';
}
return $value['hours'] .@$attach_hours. $value['minutes'] .@$attach_min. $value['seconds'].$attach_sec;
//return $value['hours'] . ':' . $value['minutes'] . ':' . $value['seconds'];
} //is_numeric($time)
else
{
return false;
}
}
public function pa_check_roles( $access_level ) {
if ( is_user_logged_in () && isset ( $access_level ) ) {
global $current_user;
$roles = $current_user->roles;
/*if ( ( current_user_can ( 'manage_options' ) ) ) {
return true;
}*/
if ( in_array ( $roles[0], $access_level ) ) {
return true;
}
else {
return false;
}
}
}
public function pa_welcome_message() {
$pointer_content = '
Analytify - Google Analytics for WordPress.
';
$pointer_content .= '
Thank you for activating Analytify Plugin. Enjoy Google Analytics for everything in WordPress.
';
?>
pa_check_warnings();
} //end if
?>