Loading .gitignore 0 → 100644 +2 −0 Original line number Diff line number Diff line composer.* vendor/ class.ga-page-stats.php +63 −51 Original line number Diff line number Diff line <?php require_once(ABSPATH . '/vendor/autoload.php'); require_once(GA_PAGE_STATS__PLUGIN_DIR . '/vendor/autoload.php'); use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; use Google\Analytics\Data\V1beta\DateRange; use Google\Analytics\Data\V1beta\Dimension; use Google\Analytics\Data\V1beta\Filter; use Google\Analytics\Data\V1beta\Filter\StringFilter; use Google\Analytics\Data\V1beta\FilterExpression; use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType; use Google\Analytics\Data\V1beta\Metric; class GAPageStats { private static $instance = null; private $view_id = null; private $analytics = null; private $property_id = null; private $key_file = null; private $start_date = null; private $client = null; protected function __construct() { $ga_options = get_option('ga_page_stats_options'); $this->view_id = $ga_options['view_id']; $client = new Google_Client(); $client->setApplicationName("Hello Analytics Reporting"); $client->setAuthConfig($ga_options['key_file']); $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); $this->analytics = new Google_Service_AnalyticsReporting($client); $this->property_id = $ga_options['property_id']; $this->key_file = $ga_options['key_file']; $this->start_date = $ga_options['start_date']; } protected function __clone() { } Loading @@ -34,51 +41,56 @@ class GAPageStats { public static function getPageStats($path) { $ga = self::getInstance(); // Create the DateRange object. $dateRange = new Google_Service_AnalyticsReporting_DateRange(); $dateRange->setStartDate("2012-11-01"); $dateRange->setEndDate("today"); // Create the page views metric. $pageViews = new Google_Service_AnalyticsReporting_Metric(); $pageViews->setExpression("ga:pageviews"); $pageViews->setAlias("Page Views"); // Create the users metric. $users = new Google_Service_AnalyticsReporting_Metric(); $users->setExpression("ga:users"); $users->setAlias("Users"); // Create the page path dimension. $pagePath = new Google_Service_AnalyticsReporting_Dimension(); $pagePath->setName("ga:pagePath"); // Create the ReportRequest object. $request = new Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId($ga->view_id); $request->setDateRanges($dateRange); $request->setDimensions(array($pagePath)); $request->setMetrics(array($pageViews, $users)); $request->setFiltersExpression("ga:pagePath=~" . $path); $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); $body->setReportRequests(array($request)); $reports = $ga->analytics->reports->batchGet($body); if (count($reports) == 0) if (empty($ga->property_id) || empty($ga->key_file) || !file_exists($ga->key_file)) { return array('pageViews' => 0, 'users' => 0); } $KEY_ENV = ('GOOGLE_APPLICATION_CREDENTIALS=' . $ga->key_file); putenv($KEY_ENV); $report = $reports[0]; $rows = $report->getData()->getRows(); if (!isset($ga->client)) { $ga->client = new BetaAnalyticsDataClient(); } $response = $ga->client->runReport([ 'property' => 'properties/' . $ga->property_id, 'dateRanges' => [ new DateRange([ 'start_date' => $ga->start_date, 'end_date' => 'today', ]) ], 'dimensions' => [], 'metrics' => [ new Metric([ 'name' => 'screenPageViews' ]), new Metric([ 'name' => 'totalUsers' ]), ], 'dimensionFilter' => new FilterExpression([ 'filter' => new Filter([ 'field_name' => 'pagePath', 'string_filter' => new Filter\StringFilter([ 'match_type' => Filter\StringFilter\MatchType::EXACT, 'value' => $path, 'case_sensitive' => false, ]), ]) ]), ]); $rows = $response->getRows(); if (count($rows) == 0) return array('pageViews' => 0, 'users' => 0); $row = $rows[0]; $metrics = $row->getMetrics(); $values = $metrics[0]->getValues(); return array('pageViews' => $values[0], 'users' => $values[1]); $metrics = $row->getMetricValues(); if (count($metrics) != 2) return array('pageViews' => 0, 'users' => 0); $pageViews = $metrics[0]->getValue(); $users = $metrics[1]->getValue(); return array('pageViews' => $pageViews, 'users' => $users); } } ga-page-stats.php +1 −1 Original line number Diff line number Diff line Loading @@ -43,7 +43,7 @@ function ga_page_stats_content_filter($content) { register_activation_hook(__FILE__, 'ga_page_stats_activation'); function ga_page_stats_activation($network_wide) { $option_name = 'ga_page_stats_options'; $option = array('view_id' => '', 'key_file' => '', $option = array('property_id' => '', 'key_file' => '', 'start_date' => 'today', 'show_default' => false, 'separator' => false); if (is_multisite() && $network_wide) { Loading options.php +40 −21 Original line number Diff line number Diff line <?php require_once(GA_PAGE_STATS__PLUGIN_DIR . 'class.ga-page-stats.php'); define('GA_PAGE_STATS__DEFAULT_START_DATE', '2015-08-14'); function verify_start_date($start_date) { $tokens = explode("-", $start_date); if (count($tokens) != 3 || $tokens[0] < 2015 || $tokens[1] < 8 || $tokens[2] < 14) { return GA_PAGE_STATS__DEFAULT_START_DATE; } return $start_date; } $gaps_title = __('Google Analytics page statistics', 'ga-page-stats'); if (!empty($_POST['Submit'])) { check_admin_referer('ga-page-stats_options'); $ga_options = array(); $ga_options['view_id'] = !empty($_POST['view_id_text']) ? addslashes(trim(wp_filter_kses($_POST['view_id_text']))) : ''; $ga_options['property_id'] = !empty($_POST['property_id_text']) ? addslashes(trim(wp_filter_kses($_POST['property_id_text']))) : ''; $ga_options['key_file'] = !empty($_POST['key_file_text']) ? addslashes(trim(wp_filter_kses($_POST['key_file_text']))) : ''; $ga_options['start_date'] = !empty($_POST['start_date_text']) ? verify_start_date($_POST['start_date_text']) : GA_PAGE_STATS__DEFAULT_START_DATE; $ga_options['show_default'] = !empty($_POST['show_default_check']); $ga_options['separator'] = !empty($_POST['separator_check']); Loading @@ -34,8 +46,10 @@ if (!empty($_POST['Submit'])) { } $ga_options = get_option('ga_page_stats_options'); $view_id = $ga_options['view_id']; $property_id = $ga_options['property_id']; $key_file = $ga_options['key_file']; $start_date = $ga_options['start_date']; $show_default = $ga_options['show_default']; $separator = $ga_options['separator']; Loading @@ -45,7 +59,7 @@ if (!empty($text)) { } $root = "/" . implode("/", array_slice(explode("/", get_home_url()), 3)); $r = GAPageStats::getPageStats($root); $r = GAPageStats::getPageStats($root . '/'); $page_views = number_format($r['pageViews']); $users = number_format($r['users']); $stats = "Page views: $page_views; users: $users"; Loading @@ -62,10 +76,10 @@ $stats = "Page views: $page_views; users: $users"; </td> </tr> <tr> <th scope="row" valign="top"><?php _e('View ID', 'ga-page-stats'); ?></th> <th scope="row" valign="top"><?php _e('Property ID', 'ga-page-stats'); ?></th> <td> <input type="text" name="view_id_text" value="<?php echo stripslashes($view_id); ?>" <input type="text" name="property_id_text" value="<?php echo stripslashes($property_id); ?>" size="30" /> </td> </tr> Loading @@ -78,6 +92,13 @@ $stats = "Page views: $page_views; users: $users"; </td> </tr> <tr> <th scope="row" valign="top"><?php _e('Start date', 'ga-page-stats'); ?></th> <td> <input type="text" name="start_date_text" value="<?php echo stripslashes($start_date); ?>" size="30" /> </td> </tr> <tr> <th scope="row" valign="top"><?php _e('Show by default', 'ga-page-stats'); ?></th> <td> Loading @@ -86,7 +107,6 @@ $stats = "Page views: $page_views; users: $users"; echo 'value="checked" checked '; ?>/> </td> </tr> <tr> <tr> <th scope="row" valign="top"><?php _e('Show separator', 'ga-page-stats'); ?></th> <td> Loading @@ -95,7 +115,6 @@ $stats = "Page views: $page_views; users: $users"; echo 'value="checked" checked '; ?>/> </td> </tr> <tr> </table> <p class="submit"> <input type="submit" name="Submit" class="button" Loading Loading
class.ga-page-stats.php +63 −51 Original line number Diff line number Diff line <?php require_once(ABSPATH . '/vendor/autoload.php'); require_once(GA_PAGE_STATS__PLUGIN_DIR . '/vendor/autoload.php'); use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; use Google\Analytics\Data\V1beta\DateRange; use Google\Analytics\Data\V1beta\Dimension; use Google\Analytics\Data\V1beta\Filter; use Google\Analytics\Data\V1beta\Filter\StringFilter; use Google\Analytics\Data\V1beta\FilterExpression; use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType; use Google\Analytics\Data\V1beta\Metric; class GAPageStats { private static $instance = null; private $view_id = null; private $analytics = null; private $property_id = null; private $key_file = null; private $start_date = null; private $client = null; protected function __construct() { $ga_options = get_option('ga_page_stats_options'); $this->view_id = $ga_options['view_id']; $client = new Google_Client(); $client->setApplicationName("Hello Analytics Reporting"); $client->setAuthConfig($ga_options['key_file']); $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); $this->analytics = new Google_Service_AnalyticsReporting($client); $this->property_id = $ga_options['property_id']; $this->key_file = $ga_options['key_file']; $this->start_date = $ga_options['start_date']; } protected function __clone() { } Loading @@ -34,51 +41,56 @@ class GAPageStats { public static function getPageStats($path) { $ga = self::getInstance(); // Create the DateRange object. $dateRange = new Google_Service_AnalyticsReporting_DateRange(); $dateRange->setStartDate("2012-11-01"); $dateRange->setEndDate("today"); // Create the page views metric. $pageViews = new Google_Service_AnalyticsReporting_Metric(); $pageViews->setExpression("ga:pageviews"); $pageViews->setAlias("Page Views"); // Create the users metric. $users = new Google_Service_AnalyticsReporting_Metric(); $users->setExpression("ga:users"); $users->setAlias("Users"); // Create the page path dimension. $pagePath = new Google_Service_AnalyticsReporting_Dimension(); $pagePath->setName("ga:pagePath"); // Create the ReportRequest object. $request = new Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId($ga->view_id); $request->setDateRanges($dateRange); $request->setDimensions(array($pagePath)); $request->setMetrics(array($pageViews, $users)); $request->setFiltersExpression("ga:pagePath=~" . $path); $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); $body->setReportRequests(array($request)); $reports = $ga->analytics->reports->batchGet($body); if (count($reports) == 0) if (empty($ga->property_id) || empty($ga->key_file) || !file_exists($ga->key_file)) { return array('pageViews' => 0, 'users' => 0); } $KEY_ENV = ('GOOGLE_APPLICATION_CREDENTIALS=' . $ga->key_file); putenv($KEY_ENV); $report = $reports[0]; $rows = $report->getData()->getRows(); if (!isset($ga->client)) { $ga->client = new BetaAnalyticsDataClient(); } $response = $ga->client->runReport([ 'property' => 'properties/' . $ga->property_id, 'dateRanges' => [ new DateRange([ 'start_date' => $ga->start_date, 'end_date' => 'today', ]) ], 'dimensions' => [], 'metrics' => [ new Metric([ 'name' => 'screenPageViews' ]), new Metric([ 'name' => 'totalUsers' ]), ], 'dimensionFilter' => new FilterExpression([ 'filter' => new Filter([ 'field_name' => 'pagePath', 'string_filter' => new Filter\StringFilter([ 'match_type' => Filter\StringFilter\MatchType::EXACT, 'value' => $path, 'case_sensitive' => false, ]), ]) ]), ]); $rows = $response->getRows(); if (count($rows) == 0) return array('pageViews' => 0, 'users' => 0); $row = $rows[0]; $metrics = $row->getMetrics(); $values = $metrics[0]->getValues(); return array('pageViews' => $values[0], 'users' => $values[1]); $metrics = $row->getMetricValues(); if (count($metrics) != 2) return array('pageViews' => 0, 'users' => 0); $pageViews = $metrics[0]->getValue(); $users = $metrics[1]->getValue(); return array('pageViews' => $pageViews, 'users' => $users); } }
ga-page-stats.php +1 −1 Original line number Diff line number Diff line Loading @@ -43,7 +43,7 @@ function ga_page_stats_content_filter($content) { register_activation_hook(__FILE__, 'ga_page_stats_activation'); function ga_page_stats_activation($network_wide) { $option_name = 'ga_page_stats_options'; $option = array('view_id' => '', 'key_file' => '', $option = array('property_id' => '', 'key_file' => '', 'start_date' => 'today', 'show_default' => false, 'separator' => false); if (is_multisite() && $network_wide) { Loading
options.php +40 −21 Original line number Diff line number Diff line <?php require_once(GA_PAGE_STATS__PLUGIN_DIR . 'class.ga-page-stats.php'); define('GA_PAGE_STATS__DEFAULT_START_DATE', '2015-08-14'); function verify_start_date($start_date) { $tokens = explode("-", $start_date); if (count($tokens) != 3 || $tokens[0] < 2015 || $tokens[1] < 8 || $tokens[2] < 14) { return GA_PAGE_STATS__DEFAULT_START_DATE; } return $start_date; } $gaps_title = __('Google Analytics page statistics', 'ga-page-stats'); if (!empty($_POST['Submit'])) { check_admin_referer('ga-page-stats_options'); $ga_options = array(); $ga_options['view_id'] = !empty($_POST['view_id_text']) ? addslashes(trim(wp_filter_kses($_POST['view_id_text']))) : ''; $ga_options['property_id'] = !empty($_POST['property_id_text']) ? addslashes(trim(wp_filter_kses($_POST['property_id_text']))) : ''; $ga_options['key_file'] = !empty($_POST['key_file_text']) ? addslashes(trim(wp_filter_kses($_POST['key_file_text']))) : ''; $ga_options['start_date'] = !empty($_POST['start_date_text']) ? verify_start_date($_POST['start_date_text']) : GA_PAGE_STATS__DEFAULT_START_DATE; $ga_options['show_default'] = !empty($_POST['show_default_check']); $ga_options['separator'] = !empty($_POST['separator_check']); Loading @@ -34,8 +46,10 @@ if (!empty($_POST['Submit'])) { } $ga_options = get_option('ga_page_stats_options'); $view_id = $ga_options['view_id']; $property_id = $ga_options['property_id']; $key_file = $ga_options['key_file']; $start_date = $ga_options['start_date']; $show_default = $ga_options['show_default']; $separator = $ga_options['separator']; Loading @@ -45,7 +59,7 @@ if (!empty($text)) { } $root = "/" . implode("/", array_slice(explode("/", get_home_url()), 3)); $r = GAPageStats::getPageStats($root); $r = GAPageStats::getPageStats($root . '/'); $page_views = number_format($r['pageViews']); $users = number_format($r['users']); $stats = "Page views: $page_views; users: $users"; Loading @@ -62,10 +76,10 @@ $stats = "Page views: $page_views; users: $users"; </td> </tr> <tr> <th scope="row" valign="top"><?php _e('View ID', 'ga-page-stats'); ?></th> <th scope="row" valign="top"><?php _e('Property ID', 'ga-page-stats'); ?></th> <td> <input type="text" name="view_id_text" value="<?php echo stripslashes($view_id); ?>" <input type="text" name="property_id_text" value="<?php echo stripslashes($property_id); ?>" size="30" /> </td> </tr> Loading @@ -78,6 +92,13 @@ $stats = "Page views: $page_views; users: $users"; </td> </tr> <tr> <th scope="row" valign="top"><?php _e('Start date', 'ga-page-stats'); ?></th> <td> <input type="text" name="start_date_text" value="<?php echo stripslashes($start_date); ?>" size="30" /> </td> </tr> <tr> <th scope="row" valign="top"><?php _e('Show by default', 'ga-page-stats'); ?></th> <td> Loading @@ -86,7 +107,6 @@ $stats = "Page views: $page_views; users: $users"; echo 'value="checked" checked '; ?>/> </td> </tr> <tr> <tr> <th scope="row" valign="top"><?php _e('Show separator', 'ga-page-stats'); ?></th> <td> Loading @@ -95,7 +115,6 @@ $stats = "Page views: $page_views; users: $users"; echo 'value="checked" checked '; ?>/> </td> </tr> <tr> </table> <p class="submit"> <input type="submit" name="Submit" class="button" Loading