is_plugin_activated(); if (!$is_activated) { wp_redirect(admin_url('admin.php?page=presslearn-settings')); exit; } if (isset($_POST['presslearn_toggle_analytics']) && isset($_POST['enable'])) { check_admin_referer('presslearn_toggle_analytics_nonce'); if (current_user_can('manage_options')) { $enable = sanitize_text_field(wp_unslash($_POST['enable'])); if ($enable === 'yes' || $enable === 'no') { update_option('presslearn_analytics_enabled', $enable); $updated = true; } } } $current_tab = 'settings'; $chart_period = 'daily'; $start_date = ''; $end_date = ''; $pages_page = 1; $countries_page = 1; $referrers_page = 1; $keywords_page = 1; if (current_user_can('manage_options') && is_admin()) { if (isset($_GET['tab'])) { $tab_value = sanitize_text_field(wp_unslash($_GET['tab'])); if (in_array($tab_value, array('settings', 'statistics'))) { $current_tab = $tab_value; } } if (isset($_GET['chart_period'])) { $period_value = sanitize_text_field(wp_unslash($_GET['chart_period'])); if (in_array($period_value, array('daily', 'weekly', 'yearly'))) { $chart_period = $period_value; } } if (isset($_GET['start_date'])) { $start_date_value = sanitize_text_field(wp_unslash($_GET['start_date'])); if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $start_date_value)) { $start_date = $start_date_value; } } if (isset($_GET['end_date'])) { $end_date_value = sanitize_text_field(wp_unslash($_GET['end_date'])); if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $end_date_value)) { $end_date = $end_date_value; } } if (isset($_GET['pages_page'])) { $pages_page = max(1, intval(wp_unslash($_GET['pages_page']))); } if (isset($_GET['countries_page'])) { $countries_page = max(1, intval(wp_unslash($_GET['countries_page']))); } if (isset($_GET['referrers_page'])) { $referrers_page = max(1, intval(wp_unslash($_GET['referrers_page']))); } if (isset($_GET['keywords_page'])) { $keywords_page = max(1, intval(wp_unslash($_GET['keywords_page']))); } } if (isset($_POST['save_analytics_settings'])) { check_admin_referer('presslearn_save_analytics_settings_nonce'); if (current_user_can('manage_options')) { $analytics_exclude_admin = isset($_POST['analytics_exclude_admin']) ? sanitize_text_field(wp_unslash($_POST['analytics_exclude_admin'])) : ''; update_option('presslearn_analytics_exclude_admin', $analytics_exclude_admin); $use_cloudflare = isset($_POST['use_cloudflare']) ? 'yes' : 'no'; update_option('presslearn_analytics_use_cloudflare', $use_cloudflare); $cache_time = isset($_POST['analytics_cache_time']) ? intval(wp_unslash($_POST['analytics_cache_time'])) : 300; if ($cache_time < 60) $cache_time = 60; if ($cache_time > 86400) $cache_time = 86400; update_option('presslearn_analytics_cache_time', $cache_time); $settings_updated = true; } } $analytics_enabled = get_option('presslearn_analytics_enabled', 'no'); $analytics_exclude_admin = get_option('presslearn_analytics_exclude_admin', ''); $use_cloudflare = get_option('presslearn_analytics_use_cloudflare', 'no'); $analytics_cache_time = get_option('presslearn_analytics_cache_time', 300); if (function_exists('presslearn_plugin') && method_exists(presslearn_plugin(), 'create_analytics_tables')) { presslearn_plugin()->create_analytics_tables(); } $pages_current_page = 1; $countries_current_page = 1; $referrers_current_page = 1; $keywords_current_page = 1; $items_per_page = 10; $keywords_per_page = 20; if (current_user_can('manage_options') && is_admin()) { $pages_current_page = $pages_page; $countries_current_page = $countries_page; $referrers_current_page = $referrers_page; $keywords_current_page = $keywords_page; $allowed_tabs = array('settings', 'statistics', 'pages', 'countries', 'referrers', 'keywords'); if (!in_array($current_tab, $allowed_tabs)) { $current_tab = 'settings'; } $allowed_periods = array('daily', 'weekly', 'yearly'); if (!in_array($chart_period, $allowed_periods)) { $chart_period = 'daily'; } $pages_current_page = max(1, $pages_current_page); $countries_current_page = max(1, $countries_current_page); $referrers_current_page = max(1, $referrers_current_page); $keywords_current_page = max(1, $keywords_current_page); } $default_end_date = gmdate('Y-m-d'); $default_start_date = gmdate('Y-m-d'); if (empty($start_date)) { $start_date = $default_start_date; } if ($current_tab === 'statistics' && empty($start_date) && empty($end_date)) { $start_date = $default_start_date; $end_date = $default_end_date; } else { if (current_user_can('manage_options') && is_admin()) { if (empty($start_date)) { $start_date = $default_start_date; } if (empty($end_date)) { $end_date = $default_end_date; } if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $start_date)) { $start_date = $default_start_date; } if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $end_date)) { $end_date = $default_end_date; } } else { $start_date = $default_start_date; $end_date = $default_end_date; } } $start_date_obj = new DateTime($start_date); $end_date_obj = new DateTime($end_date); $date_diff = $start_date_obj->diff($end_date_obj); $period_days = $date_diff->days + 1; $period_days = max(1, $period_days); if ($chart_period === 'daily') { $period_days = min($period_days, 60); } elseif ($chart_period === 'weekly') { $period_days = min($period_days, 365); } elseif ($chart_period === 'yearly') { $period_days = min($period_days, 1825); } function alpack_get_analytics_stats($period = 'today', $days = 30, $start_date = null, $end_date = null) { global $wpdb; $table_pageviews = $wpdb->prefix . 'presslearn_pageviews'; $cache_time = get_option('presslearn_analytics_cache_time', 300); $cache_key = 'alpack_analytics_stats_' . md5($period . '_' . $days . '_' . $start_date . '_' . $end_date); $cached_stats = get_transient($cache_key); if ($cached_stats !== false) { return $cached_stats; } $stats = []; if($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_pageviews)) == $table_pageviews) { $where_clause = ''; $params = []; if ($start_date && $end_date) { $where_clause = "WHERE created_at >= %s AND created_at <= %s"; $params[] = $start_date . ' 00:00:00'; $params[] = $end_date . ' 23:59:59'; } else { if($period == 'today') { $where_clause = "WHERE DATE(created_at) = CURDATE()"; } elseif($period == 'week') { $where_clause = "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY)"; $params[] = intval(min($days, 7)); } else { $where_clause = "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY)"; $params[] = intval($days); } } $pageviews = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_pageviews $where_clause", $params)); $visitors = $wpdb->get_var($wpdb->prepare("SELECT COUNT(DISTINCT visitor_id) FROM $table_pageviews $where_clause", $params)); $ip_visitors = $wpdb->get_var($wpdb->prepare("SELECT COUNT(DISTINCT ip) FROM $table_pageviews $where_clause", $params)); $stats = [ 'pageviews' => intval($pageviews), 'visitors' => intval($visitors), 'ip_visitors' => intval($ip_visitors) ]; } else { $stats = [ 'pageviews' => 0, 'visitors' => 0, 'ip_visitors' => 0 ]; } set_transient($cache_key, $stats, $cache_time); return $stats; } function alpack_get_popular_pages($limit = 12, $days = 30, $start_date = null, $end_date = null) { global $wpdb; $table_pageviews = $wpdb->prefix . 'presslearn_pageviews'; $cache_time = get_option('presslearn_analytics_cache_time', 300); $cache_key = 'alpack_popular_pages_' . md5($limit . '_' . $days . '_' . $start_date . '_' . $end_date); $cached_pages = get_transient($cache_key); if ($cached_pages !== false) { return $cached_pages; } $popular_pages = []; if($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_pageviews)) == $table_pageviews) { $where_clause = ''; $params = []; if ($start_date && $end_date) { $where_clause = "WHERE created_at >= %s AND created_at <= %s"; $params[] = $start_date . ' 00:00:00'; $params[] = $end_date . ' 23:59:59'; } else { $where_clause = "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY)"; $params[] = $days; } $results = $wpdb->get_results( $wpdb->prepare( "SELECT url, title, COUNT(*) as views FROM $table_pageviews $where_clause GROUP BY url, title ORDER BY views DESC LIMIT %d", array_merge($params, [$limit]) ), ARRAY_A ); if($results) { foreach($results as $row) { $popular_pages[] = [ 'url' => $row['url'], 'title' => $row['title'], 'views' => intval($row['views']) ]; } } } if(empty($popular_pages)) { $popular_pages = [ [ 'url' => home_url('/'), 'title' => '홈', 'views' => 0 ] ]; } set_transient($cache_key, $popular_pages, $cache_time); return $popular_pages; } function alpack_get_visitor_countries($limit = 15, $days = 30, $start_date = null, $end_date = null) { global $wpdb; $table_pageviews = $wpdb->prefix . 'presslearn_pageviews'; $cache_time = get_option('presslearn_analytics_cache_time', 300); $cache_key = 'alpack_visitor_countries_' . md5($limit . '_' . $days . '_' . $start_date . '_' . $end_date); $cached_countries = get_transient($cache_key); if ($cached_countries !== false) { return $cached_countries; } $visitor_countries = []; if($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_pageviews)) == $table_pageviews) { $where_clause = ''; $params = []; if ($start_date && $end_date) { $where_clause = "WHERE created_at >= %s AND created_at <= %s"; $params[] = $start_date . ' 00:00:00'; $params[] = $end_date . ' 23:59:59'; } else { $where_clause = "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY)"; $params[] = $days; } $where_clause .= " AND country != ''"; $results = $wpdb->get_results( $wpdb->prepare( "SELECT country, COUNT(DISTINCT visitor_id) as count FROM $table_pageviews $where_clause GROUP BY country ORDER BY count DESC LIMIT %d", array_merge($params, [$limit]) ), ARRAY_A ); if($results) { foreach($results as $row) { $visitor_countries[] = [ 'country' => $row['country'], 'count' => intval($row['count']) ]; } } } if(empty($visitor_countries)) { $visitor_countries = [ ['country' => '데이터 없음', 'count' => 0] ]; } set_transient($cache_key, $visitor_countries, $cache_time); return $visitor_countries; } function alpack_get_organic_keywords($limit = 20, $days = 30, $offset = 0, $start_date = null, $end_date = null) { global $wpdb; $table_pageviews = $wpdb->prefix . 'presslearn_pageviews'; $cache_time = get_option('presslearn_analytics_cache_time', 300); $cache_key = 'alpack_organic_keywords_' . md5($limit . '_' . $days . '_' . $offset . '_' . $start_date . '_' . $end_date); $cached_keywords = get_transient($cache_key); if ($cached_keywords !== false) { return $cached_keywords; } $organic_keywords = []; if($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_pageviews)) == $table_pageviews) { $where_clause = ''; $params = []; if ($start_date && $end_date) { $where_clause = "WHERE created_at >= %s AND created_at <= %s"; $params[] = $start_date . ' 00:00:00'; $params[] = $end_date . ' 23:59:59'; } else { $where_clause = "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY)"; $params[] = intval($days); } $where_clause .= " AND referrer != ''"; $results = $wpdb->get_results( $wpdb->prepare( "SELECT referrer, COUNT(*) as count FROM $table_pageviews $where_clause GROUP BY referrer ORDER BY count DESC", $params ), ARRAY_A ); if($results) { $keywords_found = []; foreach($results as $row) { $referrer = $row['referrer']; $keyword = ''; $search_engine = ''; $search_engine_display = ''; if(strpos($referrer, 'google') !== false && strpos($referrer, 'search') !== false) { $search_engine = '구글'; $search_engine_display = '구글'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['q']) ? $params['q'] : ''; if(empty($keyword)) { $keyword = '(not provided)'; } } elseif(strpos($referrer, 'google.co.kr') !== false || strpos($referrer, 'google.com') !== false) { $search_engine = '구글'; $search_engine_display = '구글'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['q']) ? $params['q'] : ''; if(empty($keyword)) { $keyword = '(not provided)'; } } elseif(strpos($referrer, 'search.naver.com') !== false || strpos($referrer, 'naver.com') !== false) { $search_engine = '네이버'; if(strpos($referrer, 'm.search.naver.com') !== false || strpos($referrer, 'm.naver.com') !== false) { $search_engine_display = '네이버 모바일'; } else { $search_engine_display = '네이버'; } parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['query']) ? $params['query'] : ''; } elseif((strpos($referrer, 'daum.net') !== false && strpos($referrer, 'search') !== false) || strpos($referrer, 'search.daum.net') !== false) { $search_engine = '다음'; $search_engine_display = '다음'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['q']) ? $params['q'] : ''; if(empty($keyword)) { $keyword = isset($params['w']) ? $params['w'] : ''; } } elseif(strpos($referrer, 'bing.com') !== false && strpos($referrer, 'search') !== false) { $search_engine = '빙'; $search_engine_display = '빙'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['q']) ? $params['q'] : ''; } elseif(strpos($referrer, 'yahoo.com') !== false && strpos($referrer, 'search') !== false) { $search_engine = '야후'; $search_engine_display = '야후'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['p']) ? $params['p'] : ''; } if(!empty($search_engine)) { if(empty($keyword) && $search_engine === 'Google') { $keyword = '(not provided)'; } if(!empty($keyword)) { $keyword_key = md5($keyword . $search_engine); if(isset($keywords_found[$keyword_key])) { $keywords_found[$keyword_key]['count'] += intval($row['count']); } else { $keywords_found[$keyword_key] = [ 'keyword' => $keyword, 'search_engine' => $search_engine, 'search_engine_display' => $search_engine_display, 'count' => intval($row['count']), 'referrer' => $referrer ]; } } } } usort($keywords_found, function($a, $b) { return $b['count'] - $a['count']; }); $organic_keywords = array_slice(array_values($keywords_found), $offset, $limit); } } if(empty($organic_keywords)) { $organic_keywords = [ [ 'keyword' => '아직 수집된 키워드가 없습니다', 'count' => 0, 'search_engine' => 'None', 'search_engine_display' => 'None' ] ]; } set_transient($cache_key, $organic_keywords, $cache_time); return $organic_keywords; } function alpack_get_total_organic_keywords_count($days = 30, $start_date = null, $end_date = null) { global $wpdb; $table_pageviews = $wpdb->prefix . 'presslearn_pageviews'; $cache_time = get_option('presslearn_analytics_cache_time', 300); $cache_key = 'alpack_total_keywords_count_' . md5($days . '_' . $start_date . '_' . $end_date); $cached_count = get_transient($cache_key); if ($cached_count !== false) { return $cached_count; } $count = 0; if($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_pageviews)) == $table_pageviews) { $where_clause = ''; $params = []; if ($start_date && $end_date) { $where_clause = "WHERE created_at >= %s AND created_at <= %s"; $params[] = $start_date . ' 00:00:00'; $params[] = $end_date . ' 23:59:59'; } else { $where_clause = "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY)"; $params[] = $days; } $where_clause .= " AND referrer != ''"; $results = $wpdb->get_results( $wpdb->prepare( "SELECT referrer FROM $table_pageviews $where_clause GROUP BY referrer", $params ), ARRAY_A ); if($results) { $unique_keywords = []; foreach($results as $row) { $referrer = $row['referrer']; $keyword = ''; $search_engine = ''; if(strpos($referrer, 'google') !== false && strpos($referrer, 'search') !== false) { $search_engine = '구글'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['q']) ? $params['q'] : '(not provided)'; } elseif(strpos($referrer, 'google.co.kr') !== false || strpos($referrer, 'google.com') !== false) { $search_engine = '구글'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['q']) ? $params['q'] : '(not provided)'; } elseif(strpos($referrer, 'search.naver.com') !== false || strpos($referrer, 'naver.com') !== false) { $search_engine = '네이버'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['query']) ? $params['query'] : ''; } elseif((strpos($referrer, 'daum.net') !== false && strpos($referrer, 'search') !== false) || strpos($referrer, 'search.daum.net') !== false) { $search_engine = '다음'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['q']) ? $params['q'] : ''; if(empty($keyword)) { $keyword = isset($params['w']) ? $params['w'] : ''; } } elseif(strpos($referrer, 'bing.com') !== false && strpos($referrer, 'search') !== false) { $search_engine = '빙'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['q']) ? $params['q'] : ''; } elseif(strpos($referrer, 'yahoo.com') !== false && strpos($referrer, 'search') !== false) { $search_engine = '야후'; parse_str(wp_parse_url($referrer, PHP_URL_QUERY), $params); $keyword = isset($params['p']) ? $params['p'] : ''; } if(!empty($search_engine)) { if(empty($keyword) && $search_engine === 'Google') { $keyword = '(not provided)'; } if(!empty($keyword)) { $key = md5($keyword . $search_engine); if(!isset($unique_keywords[$key])) { $unique_keywords[$key] = true; $count++; } } } } } } set_transient($cache_key, $count, $cache_time); return $count; } function alpack_get_referrers($limit = 15, $days = 30, $start_date = null, $end_date = null) { global $wpdb; $table_pageviews = $wpdb->prefix . 'presslearn_pageviews'; $table_referrers = $wpdb->prefix . 'presslearn_referrers'; $cache_time = get_option('presslearn_analytics_cache_time', 300); $cache_key = 'alpack_referrers_' . md5($limit . '_' . $days . '_' . $start_date . '_' . $end_date); $cached_referrers = get_transient($cache_key); if ($cached_referrers !== false) { return $cached_referrers; } $referrers = []; $site_host = wp_parse_url(site_url(), PHP_URL_HOST); if($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_pageviews)) == $table_pageviews) { $where_clause = ''; $params = []; if ($start_date && $end_date) { $where_clause = "WHERE created_at >= %s AND created_at <= %s AND referrer != ''"; $params[] = $start_date . ' 00:00:00'; $params[] = $end_date . ' 23:59:59'; } else { $where_clause = "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY) AND referrer != ''"; $params[] = intval($days); } $results = $wpdb->get_results( $wpdb->prepare( "SELECT referrer, COUNT(*) as count FROM $table_pageviews $where_clause GROUP BY referrer ORDER BY count DESC", $params ), ARRAY_A ); if(!empty($results)) { $referrer_groups = []; foreach($results as $row) { $referrer = $row['referrer']; $count = intval($row['count']); if (empty($referrer)) continue; $referrer_host = wp_parse_url($referrer, PHP_URL_HOST); if (empty($referrer_host)) continue; $group_name = $referrer_host; $type = '외부 유입'; if (strpos($referrer_host, 'google') !== false && (strpos($referrer_host, 'search') !== false || $referrer_host === 'google.com' || $referrer_host === 'google.co.kr' || preg_match('/^(www\.)?google\..+$/', $referrer_host))) { $group_name = '구글'; $type = '자연 유입'; } elseif (strpos($referrer_host, 'naver.com') !== false) { if (strpos($referrer_host, 'blog.naver.com') !== false || strpos($referrer_host, 'm.blog.naver.com') !== false) { $group_name = '네이버 블로그'; $type = '외부 유입'; } else { $group_name = '네이버'; $type = '자연 유입'; } } elseif (strpos($referrer_host, 'daum.net') !== false) { $group_name = '다음'; $type = '자연 유입'; } elseif (strpos($referrer_host, 'facebook.com') !== false) { $group_name = '페이스북'; $type = '외부 유입'; } elseif (strpos($referrer_host, 'instagram.com') !== false) { $group_name = '인스타그램'; $type = '외부 유입'; } elseif (strpos($referrer_host, 'twitter.com') !== false) { $group_name = '트위터'; $type = '외부 유입'; } elseif (strpos($referrer_host, 'bing.com') !== false) { $group_name = '빙'; $type = '자연 유입'; } elseif (strpos($referrer_host, 'yahoo.com') !== false) { $group_name = '야후'; $type = '자연 유입'; } elseif (strpos($referrer_host, 'youtube.com') !== false) { $group_name = '유튜브'; $type = '외부 유입'; } elseif (strpos($referrer_host, 'kakao') !== false) { $group_name = '카카오톡'; $type = '외부 유입'; } else { $type = '외부 유입'; } if (!isset($referrer_groups[$group_name])) { $referrer_groups[$group_name] = [ 'referrer' => $group_name, 'count' => 0, 'details' => [], 'total_urls' => 0, 'type' => $type ]; } $referrer_groups[$group_name]['count'] += $count; $referrer_groups[$group_name]['details'][] = [ 'url' => $referrer, 'count' => $count ]; $referrer_groups[$group_name]['total_urls']++; } foreach ($referrer_groups as $key => $group) { usort($referrer_groups[$key]['details'], function($a, $b) { return $b['count'] - $a['count']; }); if (count($referrer_groups[$key]['details']) > 10) { $referrer_groups[$key]['details'] = array_slice($referrer_groups[$key]['details'], 0, 10); } } $referrers = array_values($referrer_groups); usort($referrers, function($a, $b) { return $b['count'] - $a['count']; }); } else { } } $has_direct = false; foreach($referrers as $ref) { if($ref['referrer'] === '직접 접속') { $has_direct = true; break; } } if(!$has_direct) { $direct_count = 0; if($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_pageviews)) == $table_pageviews) { if ($start_date && $end_date) { $direct_count = $wpdb->get_var($wpdb->prepare(" SELECT COUNT(*) FROM $table_pageviews WHERE (referrer = '' OR referrer IS NULL) AND created_at >= %s AND created_at <= %s ", $start_date . ' 00:00:00', $end_date . ' 23:59:59')); } else { $direct_count = $wpdb->get_var($wpdb->prepare(" SELECT COUNT(*) FROM $table_pageviews WHERE (referrer = '' OR referrer IS NULL) AND created_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY) ", $days)); } } if($direct_count > 0) { array_unshift($referrers, [ 'referrer' => '직접 접속', 'count' => intval($direct_count), 'details' => [ ['url' => '네이버 QR 링크 혹은 직접 URL 입력', 'count' => intval($direct_count)] ], 'total_urls' => 1, 'type' => '직접 유입' ]); } } if(empty($referrers)) { $pageviews_count = 0; if($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_pageviews)) == $table_pageviews) { if ($start_date && $end_date) { $pageviews_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table_pageviews WHERE created_at >= %s AND created_at <= %s", $start_date . ' 00:00:00', $end_date . ' 23:59:59' )); } else { $pageviews_count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table_pageviews WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL %d DAY)", $days )); } } if($pageviews_count > 0) { $referrers = [ [ 'referrer' => '직접 접속', 'count' => intval($pageviews_count), 'details' => [ ['url' => '네이버 QR 링크 혹은 직접 URL 입력', 'count' => intval($pageviews_count)] ], 'total_urls' => 1, 'type' => '직접 유입' ] ]; } else { $referrers = [ [ 'referrer' => '데이터 없음', 'count' => 0, 'details' => [ ['url' => '아직 레퍼러 데이터가 수집되지 않았습니다', 'count' => 0] ], 'total_urls' => 0, 'type' => '' ] ]; } } $result = array_slice($referrers, 0, $limit); set_transient($cache_key, $result, $cache_time); return $result; } function alpack_get_chart_data($period = 'daily', $start_date = null, $end_date = null) { global $wpdb; $table_pageviews = $wpdb->prefix . 'presslearn_pageviews'; $cache_time = get_option('presslearn_analytics_cache_time', 300); $cache_key = 'alpack_chart_data_' . md5($period . '_' . $start_date . '_' . $end_date); $cached_data = get_transient($cache_key); if ($cached_data !== false) { return $cached_data; } $data = [ 'labels' => [], 'pageviews' => [], 'visitors' => [], 'ip_visitors' => [], 'raw_dates' => [] ]; if($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_pageviews)) != $table_pageviews) { set_transient($cache_key, $data, $cache_time); return $data; } $limit = 60; $today = gmdate('Y-m-d'); if ($start_date && $end_date && $start_date == $end_date && $start_date == $today) { $select_period = "HOUR(created_at)"; $select_label = "HOUR(created_at)"; $limit = 24; } else if ($period === 'weekly') { $select_period = "YEARWEEK(created_at, 1)"; $select_label = "MIN(created_at)"; $limit = 52; } else if ($period === 'yearly') { $select_period = "YEAR(created_at)"; $select_label = "YEAR(created_at)"; $group_by = "YEAR(created_at)"; $limit = 10; } else { $select_period = "DATE(created_at)"; $select_label = "DATE(created_at)"; } $table_pageviews = esc_sql($table_pageviews); $select_period = esc_sql($select_period); $select_label = esc_sql($select_label); $where_clause = ''; $sql_params = []; if ($start_date && $end_date) { $where_clause = "WHERE created_at >= %s AND created_at <= %s"; $sql_params[] = $start_date . ' 00:00:00'; $sql_params[] = $end_date . ' 23:59:59'; } $sql = "SELECT {$select_period} as period, {$select_label} as period_label, MIN(created_at) as period_start, MAX(created_at) as period_end, COUNT(*) as pageviews, COUNT(DISTINCT visitor_id) as visitors, COUNT(DISTINCT ip) as ip_visitors FROM {$table_pageviews}"; if (!empty($where_clause)) { $sql .= " $where_clause"; } $sql .= " GROUP BY " . (isset($group_by) ? $group_by : "period") . " ORDER BY period ASC LIMIT %d"; $sql_params[] = $limit; $results = $wpdb->get_results( $wpdb->prepare($sql, $sql_params), ARRAY_A ); if ($results) { if ($start_date && $end_date && $start_date == $end_date && $start_date == $today) { $hour_data = []; foreach ($results as $row) { $hour = intval($row['period_label']); $hour_data[$hour] = $row; } $results = []; for ($hour = 0; $hour < 24; $hour++) { if (isset($hour_data[$hour])) { $results[] = $hour_data[$hour]; } else { $hour_str = sprintf('%02d', $hour); $results[] = [ 'period' => $hour, 'period_label' => $hour_str, 'period_start' => gmdate('Y-m-d H:i:s', strtotime("$today $hour_str:00:00")), 'period_end' => gmdate('Y-m-d H:i:s', strtotime("$today $hour_str:59:59")), 'pageviews' => 0, 'visitors' => 0, 'ip_visitors' => 0 ]; } } } foreach ($results as $row) { if ($start_date && $end_date && $start_date == $end_date && $start_date == $today) { $hour = intval($row['period_label']); $data['labels'][] = $hour . '시'; } else if ($period === 'weekly') { $week_start = new DateTime($row['period_start']); $week_end = new DateTime($row['period_end']); $data['labels'][] = $week_start->format('m/d') . '~' . $week_end->format('m/d'); } else if ($period === 'yearly') { if (!empty($row['period_label'])) { $year = intval($row['period_label']); $data['labels'][] = $year . '년'; } else if (!empty($row['period_start'])) { $year_date = new DateTime($row['period_start']); $data['labels'][] = $year_date->format('Y년'); } else { $data['labels'][] = '데이터 없음'; } } else { $day_date = new DateTime($row['period_label']); $data['labels'][] = $day_date->format('m/d'); } $data['pageviews'][] = intval($row['pageviews']); $data['visitors'][] = intval($row['visitors']); $data['ip_visitors'][] = intval($row['ip_visitors']); $data['raw_dates'][] = [ 'start' => $row['period_start'], 'end' => $row['period_end'], 'period' => ($start_date && $end_date && $start_date == $end_date && $start_date == $today) ? 'hourly' : $period ]; } } else { set_transient($cache_key, $data, $cache_time); return $data; } set_transient($cache_key, $data, $cache_time); return $data; } $dummy_stats = [ 'today' => alpack_get_analytics_stats('today', 0, $start_date, $end_date), 'week' => alpack_get_analytics_stats('week', $period_days, $start_date, $end_date), 'month' => alpack_get_analytics_stats('month', $period_days, $start_date, $end_date) ]; $popular_pages = alpack_get_popular_pages(50, $period_days, $start_date, $end_date); $total_pages = ceil(count($popular_pages) / $items_per_page); $pages_offset = ($pages_current_page - 1) * $items_per_page; $pages_to_display = array_slice($popular_pages, $pages_offset, $items_per_page); $visitor_countries = alpack_get_visitor_countries(50, $period_days, $start_date, $end_date); $total_countries = ceil(count($visitor_countries) / $items_per_page); $countries_offset = ($countries_current_page - 1) * $items_per_page; $countries_to_display = array_slice($visitor_countries, $countries_offset, $items_per_page); $referrers = alpack_get_referrers(30, $period_days, $start_date, $end_date); $total_referrers = ceil(count($referrers) / $items_per_page); $referrers_offset = ($referrers_current_page - 1) * $items_per_page; $referrers_to_display = array_slice($referrers, $referrers_offset, $items_per_page); $total_keywords_count = alpack_get_total_organic_keywords_count($period_days, $start_date, $end_date); $total_keywords_pages = ceil($total_keywords_count / $keywords_per_page); $keywords_offset = ($keywords_current_page - 1) * $keywords_per_page; $organic_keywords = alpack_get_organic_keywords($keywords_per_page, $period_days, $keywords_offset, $start_date, $end_date); $chart_data = alpack_get_chart_data($chart_period, $start_date, $end_date); $GLOBALS['presslearn_chart_data'] = $chart_data; $GLOBALS['presslearn_chart_period'] = $chart_period; $GLOBALS['presslearn_referrers_to_display'] = $referrers_to_display; ?>

기능이 활성화되었습니다.

기능이 비활성화 상태입니다.

대시보드 / 씬 애널리틱스

설정 정보가 성공적으로 저장되었습니다.

설정 통계

씬 애널리틱스

페이지 별 애널리틱스 기능이 포함되어 있는 경량 애널리틱스 기능입니다.

플러그인 기능 활성화

CloudFlare 사용

관리자 IP 통계 제외

캐싱 시간 설정

통계 데이터 삭제

방문자 통계

사이트 방문자 및 페이지뷰 통계를 확인할 수 있습니다.

기간별 요약 (차트)

1) { $page_params .= '&pages_page=' . esc_attr($pages_page); } if ($countries_page > 1) { $page_params .= '&countries_page=' . esc_attr($countries_page); } if ($referrers_page > 1) { $page_params .= '&referrers_page=' . esc_attr($referrers_page); } if ($keywords_page > 1) { $page_params .= '&keywords_page=' . esc_attr($keywords_page); } ?>
1) { echo ''; } if ($countries_page > 1) { echo ''; } if ($referrers_page > 1) { echo ''; } if ($keywords_page > 1) { echo ''; } ?>
~

기간별 요약

페이지뷰

순 방문자 (IP 기준)

방문자 (브라우저 기준)

최근 7일

순 방문자 (IP 기준)

최근 30일

순 방문자 (IP 기준)

인기 페이지 ()

페이지 조회수
1): ?>
1) { $pagination_base_url .= "&pages_page=" . esc_attr($pages_page); } if ($countries_page > 1) { $pagination_base_url .= "&countries_page=" . esc_attr($countries_page); } if ($referrers_page > 1) { $pagination_base_url .= "&referrers_page=" . esc_attr($referrers_page); } if ($keywords_page > 1) { $pagination_base_url .= "&keywords_page=" . esc_attr($keywords_page); } if ($pages_current_page > 1): ?> /

유입 경로

$referrer): ?>
경로 페이지뷰(PV)
' . esc_html('자연 유입') . ''; } elseif (isset($referrer['type']) && $referrer['type'] === '외부 유입') { echo '' . esc_html('외부 유입') . ''; } elseif ($referrer['referrer'] !== '직접 접속' && $referrer['referrer'] !== '데이터 없음') { echo '' . esc_html('외부 유입') . ''; } ?>
1): ?>
1) { $pagination_base_url .= "&pages_page=" . esc_attr($pages_page); } if ($countries_page > 1) { $pagination_base_url .= "&countries_page=" . esc_attr($countries_page); } if ($referrers_page > 1) { $pagination_base_url .= "&referrers_page=" . esc_attr($referrers_page); } if ($keywords_page > 1) { $pagination_base_url .= "&keywords_page=" . esc_attr($keywords_page); } if ($referrers_current_page > 1): ?> /

자연 유입 키워드 ()

키워드 검색 엔진 조회수
1): ?>
1) { $pagination_base_url .= "&pages_page=" . esc_attr($pages_page); } if ($countries_page > 1) { $pagination_base_url .= "&countries_page=" . esc_attr($countries_page); } if ($referrers_page > 1) { $pagination_base_url .= "&referrers_page=" . esc_attr($referrers_page); } if ($keywords_current_page > 1): ?> /

방문자 국가

국가 페이지뷰(PV)
1): ?>
1) { $pagination_base_url .= "&pages_page=" . esc_attr($pages_page); } if ($referrers_page > 1) { $pagination_base_url .= "&referrers_page=" . esc_attr($referrers_page); } if ($keywords_page > 1) { $pagination_base_url .= "&keywords_page=" . esc_attr($keywords_page); } if ($countries_current_page > 1): ?> /