View source
<?php
define('YANDEX_METRICS_SEARCH_PHRASES_QUANTITY', 12);
define('YANDEX_METRICS_POPULAR_CONTENT_COUNT', 30);
define('YANDEX_METRICS_POPULAR_CONTENT_BLOCK_LIMIT', 15);
function yandex_metrics_reports_perm() {
return array(
'access Yandex.Metrics report',
);
}
function yandex_metrics_reports_menu() {
$items['yandex_metrics/oauth'] = array(
'access arguments' => array(
'administer Yandex.Metrics settings',
),
'page callback' => 'yandex_metrics_reports_oauth_callback',
'type' => MENU_CALLBACK,
);
$items['admin/settings/yandex_metrics/authorization'] = array(
'access arguments' => array(
'administer Yandex.Metrics settings',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'yandex_metrics_reports_authorization',
),
'title' => 'Authorization',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['admin/settings/yandex_metrics/reports'] = array(
'access arguments' => array(
'administer Yandex.Metrics settings',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'yandex_metrics_reports_reports',
),
'title' => 'Reports',
'type' => MENU_LOCAL_TASK,
'weight' => 3,
);
$items['admin/reports/yandex_metrics_summary'] = array(
'access arguments' => array(
'access Yandex.Metrics report',
),
'page callback' => 'yandex_metrics_reports_report',
'title' => 'Yandex.Metrics Summary Report',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/reports/yandex_metrics_summary/%'] = array(
'access arguments' => array(
'access Yandex.Metrics report',
),
'page callback' => 'yandex_metrics_reports_report',
'page arguments' => array(
3,
),
'title' => 'Yandex.Metrics Summary Report',
'load arguments' => array(
3,
),
'type' => MENU_CALLBACK,
);
$items['admin/yandex_metrics_ajax/%/%/%'] = array(
'access arguments' => array(
'access Yandex.Metrics report',
),
'page callback' => 'yandex_metrics_reports_ajax',
'page arguments' => array(
2,
3,
4,
),
'load arguments' => array(
2,
3,
4,
),
'type' => MENU_CALLBACK,
);
return $items;
}
function yandex_metrics_reports_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$block[0]['info'] = t('Popular content');
return $block;
case 'configure':
$form = array();
if ($delta == 0) {
$options = array(
'day' => t('Today'),
'yesterday' => t('Yesterday'),
'week' => t('Week'),
'month' => t('Month'),
);
$form['yandex_metrics_reports_popular_content_date_period'] = array(
'#type' => 'select',
'#title' => t('Select date period'),
'#description' => t('This date period will be used to filter popular content.'),
'#options' => $options,
'#default_value' => variable_get('yandex_metrics_reports_popular_content_date_period', 'week'),
);
$form['yandex_metrics_reports_popular_content_links_count'] = array(
'#type' => 'select',
'#title' => t('Select count of links to show'),
'#description' => t('The count of links that shown in the block.'),
'#options' => drupal_map_assoc(array(
5,
10,
15,
)),
'#default_value' => variable_get('yandex_metrics_reports_popular_content_links_count', 10),
);
}
return $form;
case 'save':
if ($delta == 0) {
variable_set('yandex_metrics_reports_popular_content_links_count', $edit['yandex_metrics_reports_popular_content_links_count']);
$previous_date_period = variable_get('yandex_metrics_reports_popular_content_date_period', 'week');
$new_date_period = $edit['yandex_metrics_reports_popular_content_date_period'];
if ($previous_date_period != $new_date_period) {
variable_set('yandex_metrics_reports_popular_content_date_period', $new_date_period);
yandex_metrics_reports_save_popular_content();
}
}
break;
case 'view':
default:
if (user_access('access content')) {
$check_links_count_variable = variable_get('yandex_metrics_reports_popular_content_links_count', 'undefined');
$check_date_period_variable = variable_get('yandex_metrics_reports_popular_content_date_period', 'undefined');
if ($check_links_count_variable == 'undefined' && $check_date_period_variable == 'undefined') {
yandex_metrics_reports_save_popular_content();
variable_set('yandex_metrics_reports_popular_content_links_count', 10);
variable_set('yandex_metrics_reports_popular_content_date_period', 'week');
}
$links = array();
$links_count = variable_get('yandex_metrics_reports_popular_content_links_count', 10);
$result = db_query_range("SELECT url, page_title FROM {yandex_metrics_reports_popular_content} ORDER BY page_views DESC", 0, YANDEX_METRICS_POPULAR_CONTENT_BLOCK_LIMIT);
while ($data = db_fetch_object($result)) {
if (count($links) >= $links_count) {
break;
}
$links[] = array(
'title' => $data->page_title,
'href' => $data->url,
);
}
$block = array();
if (!empty($links)) {
$block['subject'] = t('Popular content');
$block['content'] = theme('links', $links, array(
'class' => 'item-list action-links',
));
}
return $block;
}
}
}
function yandex_metrics_reports_filter_form() {
$form = array();
$current_filter = arg(3) ? arg(3) : 'week';
$options = array(
'day' => t('Today'),
'yesterday' => t('Yesterday'),
'week' => t('Week'),
'month' => t('Month'),
);
$form['filter'] = array(
'#type' => 'select',
'#title' => t('Quick filter'),
'#default_value' => $current_filter,
'#options' => $options,
'#attributes' => array(
'onchange' => 'this.form.submit();',
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => array(
'style' => 'display:none;',
),
);
return $form;
}
function yandex_metrics_reports_filter_form_submit($form, &$form_state) {
$filter = $form_state['values']['filter'];
if (!empty($filter)) {
drupal_goto('admin/reports/yandex_metrics_summary/' . $filter);
}
}
function _yandex_metrics_reports_filter_to_date_range($filter) {
switch ($filter) {
case 'day':
return array(
'start_date' => date('Ymd'),
'end_date' => date('Ymd'),
);
case 'yesterday':
return array(
'start_date' => date('Ymd', time() - 60 * 60 * 24),
'end_date' => date('Ymd', time() - 60 * 60 * 24),
);
case 'week':
default:
return array(
'start_date' => date('Ymd', time() - 60 * 60 * 24 * 6),
'end_date' => date('Ymd'),
);
case 'month':
return array(
'start_date' => date('Ymd', time() - 60 * 60 * 24 * 31),
'end_date' => date('Ymd'),
'group' => 'week',
);
}
}
function yandex_metrics_reports_report($filter = 'week') {
$counter_id = yandex_metrics_reports_get_counter_for_current_site();
if (empty($counter_id)) {
drupal_set_message(t('Please create Yandex.Metrics counter for the site first. See more details !link.', array(
'!link' => l(t('here'), 'admin/settings/yandex_metrics'),
)), 'error');
return '';
}
$counter_code = variable_get('yandex_metrics_counter_code', '');
if (empty($counter_code)) {
drupal_set_message(t('Perhaps you have not yet placed Yandex.Metrics counter code on the site. You can do this !link.', array(
'!link' => l(t('here'), 'admin/settings/yandex_metrics'),
)), 'notice');
}
$authorisation_token = variable_get('yandex_metrics_reports_auth_token', '');
if (empty($authorisation_token)) {
drupal_set_message(t('Please make sure that your application is authorized !link.', array(
'!link' => l(t('here'), 'admin/settings/yandex_metrics/authorization'),
)), 'error');
return '';
}
drupal_add_css(drupal_get_path('module', 'yandex_metrics_reports') . '/css/yandex_metrics_reports.css');
$output = '';
$output .= drupal_get_form('yandex_metrics_reports_filter_form');
$reports = yandex_metrics_reports_get_active_list();
$js_settings = array(
'modulePath' => drupal_get_path('module', 'yandex_metrics_reports'),
'cleanUrls' => variable_get('clean_url', 0),
'reportList' => json_encode(array_keys($reports)),
);
drupal_add_js(array(
'yandex_metrics_reports' => $js_settings,
), 'setting');
drupal_add_js(drupal_get_path('module', 'yandex_metrics_reports') . '/js/yandex_metrics_reports.js');
$output .= '<input type="hidden" id="yandex_metrics_reports_counter_id" value="' . $counter_id . '" />';
$output .= '<input type="hidden" id="yandex_metrics_reports_filter" value="' . $filter . '" />';
foreach ($reports as $report_name => $report_data) {
$output .= '<div class="yandex_metrics_reports-report" id="yandex_metrics_reports_' . $report_name . '"></div>';
}
return $output;
}
function _yandex_metrics_reports_search_phrases($counter_id, $filter) {
$output = '';
$date_range = _yandex_metrics_reports_filter_to_date_range($filter);
$parameters = array(
'id' => $counter_id,
'date1' => $date_range['start_date'],
'date2' => $date_range['end_date'],
);
$report_phrases = yandex_metrics_reports_retreive_data('/stat/sources/phrases', $parameters);
$phrases = json_decode($report_phrases->data);
if (empty($phrases->data)) {
return t('There is no information about search phrases for the selected date range.');
}
$phrases_totals_visits = $phrases->totals->visits;
$header = array(
t('Visits (%)'),
t('Phrase'),
);
$data = array();
$i = 1;
foreach ($phrases->data as $value) {
if ($i > YANDEX_METRICS_SEARCH_PHRASES_QUANTITY) {
break;
}
$percentage = round(100 * $value->visits / $phrases_totals_visits, 2);
$data[] = array(
$percentage,
check_plain($value->phrase),
);
$i++;
}
return theme('table', $header, $data, array(), t("Popular Search Phrases"));
}
function _yandex_metrics_reports_sources_chart($counter_id, $filter) {
$output = '';
$date_range = _yandex_metrics_reports_filter_to_date_range($filter);
$parameters = array(
'id' => $counter_id,
'date1' => $date_range['start_date'],
'date2' => $date_range['end_date'],
);
$results = yandex_metrics_reports_retreive_data('/stat/sources/summary', $parameters);
$summary = json_decode($results->data);
if (empty($summary->data)) {
return t('There is no information about traffic sources for the selected date range.');
}
$chart = array(
'#chart_id' => 'chart_sources',
'#title' => chart_title(t('Traffic Sources'), '000000', 15),
'#type' => CHART_TYPE_PIE,
'#size' => chart_size(500, 200),
'#adjust_resolution' => TRUE,
);
$sum = $summary->totals->visits;
$i = 1;
foreach ($summary->data as $value) {
$name = check_plain($value->name);
$chart['#data'][] = (int) $value->visits;
$chart['#labels'][] = $i;
$chart['#legends'][] = $i . '. ' . $name . ' (' . round($value->visits * 100 / $sum) . '%' . ')';
$chart['#data_colors'][] = chart_unique_color('sources_' . $i, 'yandex_metrics_reports');
$i++;
}
return chart_render($chart);
}
function _yandex_metrics_reports_visits_chart($counter_id, $filter) {
$output = '';
$date_range = _yandex_metrics_reports_filter_to_date_range($filter);
$parameters = array(
'id' => $counter_id,
'date1' => $date_range['start_date'],
'date2' => $date_range['end_date'],
);
if (isset($date_range['group'])) {
$parameters['group'] = $date_range['group'];
}
$results = yandex_metrics_reports_retreive_data('/stat/traffic/summary', $parameters);
$visits = json_decode($results->data);
if (empty($visits->data)) {
return t('There is no information about page views and visitors for the selected date range.');
}
$dates = array();
$page_views = array();
$visitors = array();
$new_visitors = array();
if (count($visits->data) == 1) {
$z_point = $visits->data[0];
$page_views[] = (int) $z_point->page_views;
$visitors[] = (int) $z_point->visitors;
$new_visitors[] = (int) $z_point->new_visitors;
$position = 50;
}
else {
$position = NULL;
}
foreach ($visits->data as $value) {
$dates[] = check_plain($value->date);
$page_views[] = (int) $value->page_views;
$visitors[] = (int) $value->visitors;
$new_visitors[] = (int) $value->new_visitors;
}
$dates = array_reverse($dates);
$page_views = array_reverse($page_views);
$visitors = array_reverse($visitors);
$new_visitors = array_reverse($new_visitors);
$chart = array(
'#chart_id' => 'chart_visits',
'#title' => chart_title(t('Page Views, Visitors, New Visitors'), '000000', 15),
'#type' => CHART_TYPE_LINE,
'#size' => chart_size(500, 250),
'#bar_size' => chart_bar_size(17, 3),
'#adjust_resolution' => TRUE,
'#legend_position' => CHART_LEGEND_BOTTOM,
);
$chart['#data']['views'] = $page_views;
$chart['#data']['visitors'] = $visitors;
$chart['#data']['new_visitors'] = $new_visitors;
$max = 0;
foreach ($chart['#data'] as $data) {
$max_new = max($data);
if ($max_new > $max) {
$max = $max_new;
}
}
$chart['#data_colors']['views'] = '0000FF';
$chart['#data_colors']['visitors'] = '008000';
$chart['#data_colors']['new_visitors'] = 'FF0000';
$chart['#legends']['views'] = t('Page Views');
$chart['#legends']['visitors'] = t('Visitors');
$chart['#legends']['new_visitors'] = t('New Visitors');
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
foreach ($dates as $date) {
$timestamp = strtotime($date);
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(date('d.m.y', $timestamp), $position);
}
return chart_render($chart);
}
function _yandex_metrics_reports_popular_content($counter_id, $filter) {
$output = '';
$date_range = _yandex_metrics_reports_filter_to_date_range($filter);
$parameters = array(
'id' => $counter_id,
'date1' => $date_range['start_date'],
'date2' => $date_range['end_date'],
);
$report_content = yandex_metrics_reports_retreive_data('/stat/content/popular', $parameters);
$content = json_decode($report_content->data);
if (empty($content->data)) {
return t('There is no information about popular content for the selected date range.');
}
$header = array(
t('URL'),
t('Page Views'),
);
$data = array();
$i = 1;
foreach ($content->data as $value) {
if ($i > YANDEX_METRICS_POPULAR_CONTENT_COUNT) {
break;
}
$page_views = (int) $value->page_views;
$data[] = array(
l($value->url, $value->url, array(
'attributes' => array(
'target' => '_blank',
),
)),
$page_views,
);
$i++;
}
return theme('table', $header, $data, array(), t("Popular Content"));
}
function yandex_metrics_reports_authorization() {
$authorisation_token = variable_get('yandex_metrics_reports_auth_token', '');
if (!empty($authorisation_token)) {
$authorization_status_text = '<span style="color:green;">' . t('The application is already authorized.') . '</span>';
}
else {
$authorization_status_text = '<span style="color:red;">' . t('The application is not authorized yet.') . '</span>';
}
$form['yandex_metrics_reports_auth_text'] = array(
'#value' => $authorization_status_text,
'#type' => 'item',
);
$form['yandex_metrics_reports_client_id'] = array(
'#default_value' => variable_get('yandex_metrics_reports_client_id', ''),
'#title' => t('Client ID'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('Enter your application client ID in this field.'),
);
$form['yandex_metrics_reports_client_secret'] = array(
'#default_value' => variable_get('yandex_metrics_reports_client_secret', ''),
'#title' => t('Client Secret'),
'#type' => 'textfield',
'#description' => t('Enter your application secret in this field.'),
);
$form['authorize'] = array(
'#type' => 'submit',
'#value' => !empty($authorisation_token) ? t('Re-Authorize Application') : t('Authorize Application'),
);
return $form;
}
function yandex_metrics_reports_authorization_submit($form, &$form_state) {
variable_set('yandex_metrics_reports_client_id', $form_state['values']['yandex_metrics_reports_client_id']);
variable_set('yandex_metrics_reports_client_secret', $form_state['values']['yandex_metrics_reports_client_secret']);
$client_id = variable_get('yandex_metrics_reports_client_id', '');
if (empty($client_id)) {
drupal_set_message(t("Please set Client ID first."));
return;
}
$request_url = "https://oauth.yandex.ru/authorize?response_type=code&client_id=" . $client_id;
drupal_goto($request_url);
}
function yandex_metrics_reports_oauth_callback() {
if (empty($_GET['code'])) {
watchdog('yandex_metrics_reports', 'The "code" parameter is empty.', array(), WATCHDOG_WARNING);
drupal_set_message(t('An error has occurred. Please try again.'), 'error');
drupal_goto('admin/settings/yandex_metrics/authorization');
}
$client_secret = variable_get('yandex_metrics_reports_client_secret', '');
$client_id = variable_get('yandex_metrics_reports_client_id', '');
$data = 'grant_type=authorization_code&client_id=' . $client_id . '&code=' . $_GET['code'];
if (!empty($client_secret)) {
$data .= '&client_secret=' . $client_secret;
}
$result = drupal_http_request("https://oauth.yandex.ru/token", array(), 'POST', $data);
if (isset($result->error)) {
watchdog('yandex_metrics_reports', 'Token request seems to be fail, due to "%error".', array(
'%error' => $result->code . ' ' . $result->error,
), WATCHDOG_WARNING);
drupal_set_message(t('An error has occurred. Please try again.'), 'error');
drupal_goto('admin/settings/yandex_metrics/authorization');
}
$response = json_decode($result->data);
variable_set('yandex_metrics_reports_auth_token', $response->access_token);
watchdog('yandex_metrics_reports', 'Token request is successful.');
drupal_set_message(t('Congratulations! Your application has been authorized by Yandex.Metrics service.'));
drupal_goto('admin/settings/yandex_metrics/authorization');
}
function yandex_metrics_reports_retreive_data($service_uri, $parameters = array(), $result_type = 'json') {
$parameters['oauth_token'] = variable_get('yandex_metrics_reports_auth_token', '');
$query_parts = array();
foreach ($parameters as $key => $value) {
$query_parts[] = $key . '=' . $value;
}
$parameter_string = implode('&', $query_parts);
$full_service_url = "http://api-metrika.yandex.ru" . $service_uri . "." . $result_type . "?" . $parameter_string;
return drupal_http_request($full_service_url);
}
function yandex_metrics_reports_get_counter_for_current_site() {
$counter_id = variable_get('yandex_metrics_reports_counter_id', '');
if (!empty($counter_id)) {
return $counter_id;
}
$result = yandex_metrics_reports_retreive_data('/counters', array(
'field' => 'mirrors',
));
if (isset($result->error)) {
watchdog('yandex_metrics_reports', 'Counters request seems to be fail, due to "%error".', array(
'%error' => $result->code . ' ' . $result->error,
), WATCHDOG_WARNING);
return;
}
$counters = json_decode($result->data);
$current_host = $_SERVER['HTTP_HOST'];
$decoded_domain = _yandex_metrics_reports_idna_decode($current_host);
if ($decoded_domain != FALSE && $decoded_domain != $current_host) {
$current_host = $decoded_domain;
}
foreach ($counters->counters as $key => $counter) {
if ($counter->site == $current_host) {
variable_set('yandex_metrics_reports_counter_id', $counter->id);
return $counter->id;
}
if (isset($counter->mirrors) && in_array($current_host, $counter->mirrors)) {
variable_set('yandex_metrics_reports_counter_id', $counter->id);
return $counter->id;
}
}
return FALSE;
}
function yandex_metrics_reports_save_popular_content($cron = FALSE) {
$counter_code = variable_get('yandex_metrics_counter_code', '');
if (empty($counter_code) && !$cron) {
drupal_set_message(t('Perhaps you have not yet placed Yandex.Metrics counter code on the site. You can do this !link.', array(
'!link' => l(t('here'), 'admin/settings/yandex_metrics'),
)), 'notice');
return;
}
$counter_id = yandex_metrics_reports_get_counter_for_current_site();
if (empty($counter_id)) {
if ($cron) {
watchdog('yandex_metrics_reports', 'Cron: counter ID is not set.', array(), WATCHDOG_WARNING);
}
else {
drupal_set_message(t('Please create Yandex.Metrics counter for the site first. See more details !link.', array(
'!link' => l(t('here'), 'admin/settings/yandex_metrics'),
)), 'error');
}
return;
}
$authorisation_token = variable_get('yandex_metrics_reports_auth_token', '');
if (empty($authorisation_token)) {
if ($cron) {
watchdog('yandex_metrics_reports', 'Cron: application is not authorised.', array(), WATCHDOG_WARNING);
}
else {
drupal_set_message(t('Please make sure that your application is authorized !link.', array(
'!link' => l(t('here'), 'admin/settings/yandex_metrics/authorization'),
)), 'error');
}
return;
}
$filter = variable_get('yandex_metrics_reports_popular_content_date_period', 'week');
$date_range = _yandex_metrics_reports_filter_to_date_range($filter);
$parameters = array(
'id' => $counter_id,
'date1' => $date_range['start_date'],
'date2' => $date_range['end_date'],
);
$report_content = yandex_metrics_reports_retreive_data('/stat/content/popular', $parameters);
$content = json_decode($report_content->data);
if (!empty($content->data)) {
db_query("TRUNCATE TABLE {yandex_metrics_reports_popular_content}");
$counter = 1;
foreach ($content->data as $value) {
$page_title = '';
$parsed_url = parse_url($value->url);
$pos = strpos($parsed_url['path'], base_path());
if ($pos !== FALSE) {
$path = substr_replace($parsed_url['path'], '', $pos, strlen(base_path()));
if ($path == '') {
$page_title = variable_get('site_name', t('Front page'));
}
else {
$internal_path = drupal_get_normal_path($path);
if (strpos($internal_path, 'node/') === 0) {
$temp = explode('/', $internal_path);
$nid = $temp[1];
$node = node_load($nid);
$page_title = empty($node->page_title) ? $node->title : $node->page_title;
}
elseif (strpos($internal_path, 'taxonomy/term') === 0) {
$temp = explode('/', $internal_path);
$tid = $temp[2];
$term = taxonomy_get_term($tid);
$page_title = $term->name;
}
else {
$page_title = $path;
}
}
}
db_query("INSERT INTO {yandex_metrics_reports_popular_content} (url, page_title, page_views) VALUES ('%s', '%s', %d)", $value->url, $page_title, $value->page_views);
if ($counter++ >= YANDEX_METRICS_POPULAR_CONTENT_BLOCK_LIMIT) {
break;
}
}
watchdog('yandex_metrics_reports', 'Popular content for %filter has been saved.', array(
'%filter' => $filter,
), WATCHDOG_NOTICE);
}
}
function yandex_metrics_reports_cron() {
if (!_yandex_metrics_reports_is_popular_content_enabled()) {
return;
}
yandex_metrics_reports_save_popular_content(TRUE);
}
function yandex_metrics_reports_ajax($counter_id, $filter, $type) {
$output = '';
$reports = yandex_metrics_reports_get_list();
if (isset($reports[$type]) && isset($reports[$type]['callback']) && function_exists($reports[$type]['callback'])) {
$output = call_user_func($reports[$type]['callback'], $counter_id, $filter);
}
print $output;
die;
}
function yandex_metrics_reports_reports() {
$form = array();
$form['reports'] = array(
'#type' => 'fieldset',
'#title' => t('Reports visibility settings'),
'#description' => t('Choose reports to display on Yandex.Metrics Summary Report page.'),
);
$reports = yandex_metrics_reports_get_list(TRUE);
foreach ($reports as $report_name => $report_data) {
$form['reports']['yandex_metrics_reports_' . $report_name . '_visible'] = array(
'#type' => 'checkbox',
'#title' => $report_data['title'],
'#default_value' => variable_get('yandex_metrics_reports_' . $report_name . '_visible', TRUE),
);
}
return system_settings_form($form);
}
function _yandex_metrics_reports_geo_chart($counter_id, $filter) {
$output = '';
$date_range = _yandex_metrics_reports_filter_to_date_range($filter);
$parameters = array(
'id' => $counter_id,
'date1' => $date_range['start_date'],
'date2' => $date_range['end_date'],
);
$results = yandex_metrics_reports_retreive_data('/stat/geo', $parameters);
$geo = json_decode($results->data);
if (empty($geo->data)) {
return t('There is no information about geography of visits for the selected date range.');
}
$chart = array(
'#chart_id' => 'chart_geo',
'#title' => chart_title(t('Geography of Visits'), '000000', 15),
'#type' => CHART_TYPE_PIE,
'#size' => chart_size(500, 230),
'#adjust_resolution' => TRUE,
);
$total_visits = $geo->totals->visits;
foreach ($geo->data as $key => $value) {
if ($value->name == "Не определено") {
$total_visits -= $value->visits;
unset($geo->data[$key]);
break;
}
}
$i = 1;
$sum_visits = 0;
foreach ($geo->data as $value) {
$visits = (int) $value->visits;
if ($i > 10) {
$others_visits = $total_visits - $sum_visits;
$chart['#data'][] = $others_visits;
$chart['#labels'][] = t('Others') . ' (' . round($others_visits * 100 / $total_visits, 1) . '%' . ')';
$chart['#data_colors'][] = chart_unique_color('geo_' . $i, 'yandex_metrics_reports');
break;
}
$sum_visits += $visits;
$name = check_plain($value->name);
$chart['#data'][] = $visits;
$chart['#labels'][] = $name . ' (' . round($visits * 100 / $total_visits, 1) . '%' . ')';
$chart['#data_colors'][] = chart_unique_color('geo_' . $i, 'yandex_metrics_reports');
$i++;
}
return chart_render($chart);
}
function yandex_metrics_reports_chart_color_schemes(&$colors) {
$colors['yandex_metrics_reports'] = array(
'ff3030',
'ff8800',
'00b6f7',
'95d900',
'ffea00',
'bb7bf1',
'0080ff',
'ff8888',
'87888e',
'457f51',
'b5b6be',
);
}
function _yandex_metrics_reports_get_idna_library_path() {
$file = 'idna_convert.class.php';
$library = 'idna_convert';
if (module_exists('libraries') && file_exists(libraries_get_path($library) . "/{$file}")) {
return libraries_get_path($library) . "/{$file}";
}
else {
$paths = array(
drupal_get_path('module', 'yandex_metrics'),
drupal_get_path('module', 'yandex_metrics') . "/libraries",
drupal_get_path('module', 'yandex_metrics_reports'),
drupal_get_path('module', 'yandex_metrics_reports') . "/libraries",
'profiles/' . variable_get('install_profile', 'default') . '/libraries',
'profiles/' . variable_get('install_profile', 'default') . '/libraries/' . $library,
'sites/all/libraries',
'sites/all/libraries/' . $library,
);
foreach ($paths as $library_path) {
$path = $library_path . "/{$file}";
if (file_exists($path)) {
return $path;
}
}
}
return FALSE;
}
function _yandex_metrics_reports_idna_decode($domain) {
$idna_library_path = _yandex_metrics_reports_get_idna_library_path();
if ($idna_library_path == FALSE) {
return $domain;
}
require_once $idna_library_path;
$IDN = new idna_convert();
return $IDN
->decode($domain);
}
function yandex_metrics_reports_help($path, $arg) {
switch ($path) {
case 'admin/help#yandex_metrics_reports':
$output = '';
$output .= '<h3>' . t('About the module') . '</h3>';
$output .= '<p>' . t('The <a href="@yandex_metrika" target="_blank">Yandex.Metrics</a> service is European alternative of Google Analytics. This is a free tool that helps you to increase the conversion rate of your site.', array(
'@yandex_metrika' => 'http://metrika.yandex.ru/',
)) . '</p>';
$output .= '<p>' . t('The Yandex.Metrics Reports module allows to authorize the site on Yandex and view basic reports by key effectiveness indicators.') . '</p>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Authorizing site') . '</dt>';
$output .= '<dd>' . t('To get access to the <a href="@reports-url">reports</a> register Yandex application at !app_register_link. Then enter your application Client ID and Client Secret in the appropriate fields on the <a href="@authorization-page">Authorization</a> page. The full list of your Yandex applications is located !app_list_link.', array(
'!app_register_link' => l('https://oauth.yandex.ru/client/new', 'https://oauth.yandex.ru/client/new', array(
'attributes' => array(
'target' => '_blank',
),
)),
'!app_list_link' => l(t('here'), 'https://oauth.yandex.ru/client/my', array(
'attributes' => array(
'target' => '_blank',
),
)),
'@reports-url' => url('admin/reports/yandex_metrics_summary'),
'@authorization-page' => url('admin/settings/yandex_metrics/authorization'),
)) . '</dd>';
$output .= '<dt>' . t('Configuring reports') . '</dt>';
$output .= '<dd>' . t('You can choose necessary reports <a href="@report-setting-page">here</a> to display on <a href="@summary-report-page">Yandex.Metrics Summary Report</a> page. Also, we recommend you to enable AJAX for reports by checking appropriate field on the <a href="@report-setting-page">report settings page</a>.', array(
'@summary-report-page' => url('admin/reports/yandex_metrics_summary'),
'@report-setting-page' => url('admin/settings/yandex_metrics/reports'),
)) . '</dd>';
$output .= '</dl>';
return $output;
case 'admin/settings/yandex_metrics/authorization':
$output = '<p>' . t('To get access to the <a href="@reports-url">reports</a> register Yandex application at !app_register_link. Then enter your application Client ID and Client Secret in the appropriate fields on this page. The full list of your Yandex applications is located !app_list_link.', array(
'!app_register_link' => l('https://oauth.yandex.ru/client/new', 'https://oauth.yandex.ru/client/new', array(
'attributes' => array(
'target' => '_blank',
),
)),
'!app_list_link' => l(t('here'), 'https://oauth.yandex.ru/client/my', array(
'attributes' => array(
'target' => '_blank',
),
)),
'@reports-url' => url('admin/reports/yandex_metrics_summary'),
)) . '</p>';
$output .= '<p>' . t('Your Yandex application Callback URI: @callback-uri', array(
'@callback-uri' => url('yandex_metrics/oauth', array(
'absolute' => TRUE,
)),
)) . '</p>';
return $output;
case 'admin/settings/yandex_metrics/reports':
return '<p>' . t('You can choose necessary reports to display on <a href="@summary-report-page">Yandex.Metrics Summary Report</a> page. Also, we recommend you to enable AJAX for reports by checking the appropriate field on this page.', array(
'@summary-report-page' => url('admin/reports/yandex_metrics_summary'),
)) . '</p>';
}
}
function _yandex_metrics_reports_is_popular_content_enabled() {
$result = db_query("SELECT * FROM {blocks} WHERE module = 'yandex_metrics_reports' AND delta = '0' AND status = 1");
$row = db_fetch_array($result);
return !empty($row);
}
function yandex_metrics_reports_yandex_metrics_reports_list() {
$reports = array();
$reports['visits_chart'] = array(
'title' => t('Page Views, Visitors, New Visitors'),
'callback' => '_yandex_metrics_reports_visits_chart',
);
$reports['sources_chart'] = array(
'title' => t('Traffic Sources'),
'callback' => '_yandex_metrics_reports_sources_chart',
);
$reports['search_phrases'] = array(
'title' => t('Popular Search Phrases'),
'callback' => '_yandex_metrics_reports_search_phrases',
);
$reports['popular_content'] = array(
'title' => t('Popular Content'),
'callback' => '_yandex_metrics_reports_popular_content',
);
$reports['geo_chart'] = array(
'title' => t('Geography of Visits'),
'callback' => '_yandex_metrics_reports_geo_chart',
);
return $reports;
}
function yandex_metrics_reports_get_list($reset = FALSE) {
static $reports;
if (!isset($reports) || $reset) {
$cache = cache_get('yandex_metrics_reports_list');
if ($cache === FALSE || $reset) {
$reports = array();
foreach (module_implements('yandex_metrics_reports_list') as $module) {
$module_report_list = module_invoke($module, 'yandex_metrics_reports_list');
if (isset($module_report_list) && is_array($module_report_list)) {
$reports = array_merge($reports, $module_report_list);
}
}
drupal_alter('yandex_metrics_reports_list', $reports);
cache_set('yandex_metrics_reports_list', $reports);
}
else {
$reports = $cache->data;
}
}
return $reports;
}
function yandex_metrics_reports_get_active_list() {
$reports = yandex_metrics_reports_get_list();
foreach ($reports as $report_name => $report_data) {
if (!variable_get('yandex_metrics_reports_' . $report_name . '_visible', TRUE) || !function_exists($report_data['callback'])) {
unset($reports[$report_name]);
}
}
return $reports;
}
function yandex_metrics_reports_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'yandex_metrics_reports') . '/views',
);
}