View source
<?php
function ga_stats_menu() {
$items = array();
$items['admin/config/services/ga_stats'] = array(
'title' => 'Google Analytics Statistics',
'description' => 'Configuration for Google Analytics Statistics',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ga_stats_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'ga_stats.admin.inc',
);
$items['admin/config/services/ga_stats/auth'] = array(
'title' => 'Authentication',
'description' => 'Submit API credentials for Google Analytics Statistics',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ga_stats_auth_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'ga_stats.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/services/ga_stats/default'] = array(
'title' => 'Google Analytics Statistics',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
function ga_stats_views_api() {
return array(
'api' => 2,
);
}
function ga_stats_cron() {
if (ga_stats_is_ready() && ga_stats_needs_update()) {
ga_stats_update_counts();
}
}
function ga_stats_needs_update() {
return ga_stats_data_expiration_date() <= $_SERVER['REQUEST_TIME'];
}
function ga_stats_data_expiration_date() {
$last = variable_get('ga_stats_last_update', 0);
$timeframe = variable_get('ga_stats_enabled_timeframes', array(
'today' => TRUE,
'month' => TRUE,
));
if (array_key_exists('hour', $timeframe)) {
$expiration = 60 * 10;
}
else {
$expiration = 60 * 30;
}
return $last + $expiration;
}
function ga_stats_schedule_update() {
variable_set('ga_stats_last_update', $_SERVER['REQUEST_TIME']);
return $_SERVER['REQUEST_TIME'];
}
function ga_stats_update_counts() {
require_once 'includes/ga.inc';
return _ga_stats_update_counts();
}
function ga_stats_is_ready() {
return variable_get('ga_stats_email', FALSE) && variable_get('ga_stats_password', FALSE);
}
function ga_stats_ga_metrics($all = FALSE) {
$metrics = array(
'pageviews' => 'Page Views',
'uniquePageviews' => 'Unique Page Views',
'avgTimeOnPage' => 'Average Time on Page',
'entrances' => 'Entrance Page',
);
if (!$all) {
$enabled = variable_get('ga_stats_enabled_metrics', array(
'pageviews' => TRUE,
));
$metrics = array_intersect_key($metrics, array_filter($enabled));
}
return $metrics;
}
function ga_stats_ga_timeframes($form_options = FALSE, $all = FALSE) {
if ($form_options) {
$timeframes = array(
'hour' => 'in the past hour',
'today' => 'in the past 24 hours',
'2days' => 'in the past 48 hours',
'week' => 'in the past 7 days',
'month' => 'in the past 31 days',
'year' => 'in the past 365 days',
'forever' => 'since 2005 began',
);
}
else {
$timeframes = array(
'hour' => array(
'secsToSub' => 60 * 60,
'filter' => 'hour==' + date('G'),
'title' => 'in the past hour',
),
'today' => array(
'secsToSub' => 60 * 60 * 24,
'filter' => 'day==' + date('j'),
'title' => 'in the past 24 hours',
),
'2days' => array(
'secsToSub' => 60 * 60 * 24 * 2,
'title' => 'in the past 48 hours',
),
'week' => array(
'secsToSub' => 60 * 60 * 24 * 7,
'title' => 'in the past 7 days',
),
'month' => array(
'secsToSub' => 60 * 60 * 24 * 31,
'title' => 'in the past 31 days',
),
'year' => array(
'secsToSub' => 60 * 60 * 24 * 365,
'title' => 'in the past 365 days',
),
'forever' => array(
'secsToSub' => time() - strtotime('2005-01-01'),
'title' => 'since 2005 began',
),
);
}
if (!$all) {
$enabled = variable_get('ga_stats_enabled_timeframes', array(
'today' => TRUE,
'month' => TRUE,
));
$timeframes = array_intersect_key($timeframes, array_filter($enabled));
}
return $timeframes;
}