function yandex_metrics_save_popular_content in Yandex.Metrics 6
Same name and namespace in other branches
- 7 yandex_metrics.module \yandex_metrics_save_popular_content()
Fetch Popuplar content from Yandex.metrika and save it to the database.
2 calls to yandex_metrics_save_popular_content()
- yandex_metrics_block in ./
yandex_metrics.module - Implementation of hook_block().
- yandex_metrics_cron in ./
yandex_metrics.module - Implementation of hook_cron().
File
- ./
yandex_metrics.module, line 931 - The main code of Yandex.Metrics module.
Code
function yandex_metrics_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_get_counter_for_current_site();
if (empty($counter_id)) {
if ($cron) {
watchdog('yandex_metrics', '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_auth_token', '');
if (empty($authorisation_token)) {
if ($cron) {
watchdog('yandex_metrics', '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_popular_content_date_period', 'week');
$date_range = _yandex_metrics_filter_to_date_range($filter);
$parameters = array(
'id' => $counter_id,
'date1' => $date_range['start_date'],
'date2' => $date_range['end_date'],
);
// Fetch popular content urls.
$report_content = yandex_metrics_retreive_data('/stat/content/popular', $parameters);
$content = json_decode($report_content->data);
if (!empty($content->data)) {
// Remove outdated data.
db_query("TRUNCATE TABLE {yandex_metrics_popular_content}");
$counter = 1;
foreach ($content->data as $value) {
// Obtain page title for current url.
$page_title = '';
$parsed_url = parse_url($value->url);
// Get rid of base_path(typically "/" symbol) from the url.
$pos = strpos($parsed_url['path'], base_path());
if ($pos !== FALSE) {
// Get path alias in order to obtain page title.
$path = substr_replace($parsed_url['path'], '', $pos, strlen(base_path()));
if ($path == '') {
// If path alias is empty than it's the Front page.
$page_title = variable_get('site_name', t('Front page'));
}
else {
// Convert path alias to internal Drupal path.
$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_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', 'Popular content for %filter has been saved.', array(
'%filter' => $filter,
), WATCHDOG_NOTICE);
}
}