forked from discoverygarden/google_analytics_reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_analytics_api.module
180 lines (162 loc) · 6.21 KB
/
google_analytics_api.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* @file
* Implements the API through which Google Analytics data can be accessed.
*/
/* Number of seconds in a day. */
define('GOOGLE_ANALYTICS_REPORTS_DAY', 60 * 60 * 24);
/* Number of seconds in a week. */
define('GOOGLE_ANALYTICS_REPORTS_WEEK', GOOGLE_ANALYTICS_REPORTS_DAY * 7);
/**
* Implementation of hook_help().
*/
function google_analytics_api_help($path, $arg) {
switch ($path) {
case 'admin/settings/google-analytics-api':
case 'admin/help#google_analytics_api':
$output = '<p>' . t('Google Analytics Reports and it\'s API module provides site administrators and programmers a rich API to generate graphical reports based on Google Analytics data. The reports module includes a helpful sitewide report located under <a href="!link">Drupal\'s default administrator reports</a>, and blocks for both sitewide and path-based reports.', array(
'!link' => url('admin/reports'),
)) . '</p>';
$output .= '<p>' . t('Programmers can build complex queries against Google Analytics and display them in any format they like. To get started coding your own reports, visit the <a href="!link">Google Analytics documentation page on drupal.org</a>.', array(
'!link' => url('http://drupal.org/node/1138274'),
)) . '</p>';
return $output;
break;
}
}
/**
* Implementation of hook_menu().
*/
function google_analytics_api_menu() {
$items['admin/config/system/google-analytics-reports'] = array(
'title' => 'Google Analytics Reports',
'description' => 'Configure your account, profiles, and reports.',
'page callback' => 'drupal_get_form',
'file' => 'google_analytics_api.pages.inc',
'page arguments' => array('google_analytics_api_admin'),
'access arguments' => array('administer google analytics reports'),
);
/* OAuth callback from Google */
$items['google-analytics-reports/oauth'] = array(
'title' => 'Google Analytics Reports OAuth Callback',
'access callback' => TRUE,
'page callback' => 'google_analytics_reports_oauth_callback',
'type' => MENU_CALLBACK,
'file' => 'google_analytics_api.pages.inc',
);
return $items;
}
/**
* Implementation of hook_permission().
*/
function google_analytics_api_permission() {
return array(
'administer google analytics reports' => array(
'title' => t('administer google analytics reports'),
),
);
}
/**
* Implements hook_theme().
*/
function google_analytics_api_theme() {
return array(
'google_analytics_api_profile_label' => array(
'arguments' => array('profile' => NULL),
),
);
}
/**
* Instantiate a new GAFeed object.
*/
function google_analytics_api_new_gafeed() {
module_load_include('inc', 'google_analytics_api', 'GAFeed.lib');
$key = variable_get('google_analytics_reports_consumer_key', 'anonymous');
$secret = variable_get('google_analytics_reports_consumer_secret', 'anonymous');
$oauth_token = variable_get('google_analytics_reports_oauth_token', NULL);
$oauth_token_secret = variable_get('google_analytics_reports_oauth_token_secret', NULL);
return new GAFeed($key, $secret, $oauth_token, $oauth_token_secret);
}
/**
* Request report data.
*
* @param $params
* An associative array containing:
* - profile_id: required [default=variable_get('google_analytics_reports_profile_id')]
* - metrics: required.
* - dimensions: optional [default=none]
* - sort_metric: optional [default=none]
* - filters: optional [default=none]
* - segment: optional [default=none]
* - start_date: optional [default=GA release date]
* - end_date: optional [default=today]
* - start_index: optional [default=1]
* - max_results: optional [default=10,000]
* @param $cache_options
* An optional associative array containing:
* - cid: optional [default=md5 hash]
* - expire: optional [default=CACHE_TEMPORARY]
* - refresh: optional [default=FALSE]
*/
function google_analytics_api_report_data($params = array(), $cache_options = array()) {
$params_defaults = array(
'profile_id' => 'ga:' . variable_get('google_analytics_reports_profile_id', 0),
);
$params += $params_defaults;
$GAFeed = google_analytics_api_new_gafeed();
$GAFeed->queryReportFeed($params, $cache_options);
return $GAFeed;
}
/*
* Programatically revoke token.
*/
function google_analytics_api_revoke() {
$GAFeed = google_analytics_api_new_gafeed();
$GAFeed->revokeToken();
variable_del('google_analytics_reports_profile_id');
variable_del('google_analytics_reports_consumer_key');
variable_del('google_analytics_reports_consumer_secret');
variable_del('google_analytics_reports_oauth_token');
variable_del('google_analytics_reports_oauth_token_secret');
variable_del('google_analytics_reports_cache_length');
}
/**
* Sets the expiry timestamp for cached queries.
*
* Default is 3 days.
*
* @return The UNIX timestamp to expire the query at.
*/
function google_analytics_reports_cache_time() {
return time() + variable_get('google_analytics_reports_cache_length', 259200);
}
/**
* Theme the full string label of profiles.
*
* @return Themed string label.
*/
function theme_google_analytics_api_profile_label($variables) {
return $variables['profile']->name . ' (' . $variables['profile']->id . ')';
}
/**
* Implements hook_domain_conf().
*/
function google_analytics_api_domain_conf() {
$profile_id = variable_get('google_analytics_reports_profile_id', 0);
$form['google_analytics_reports'] = array(
'#type' => 'fieldset',
'#title' => t('Google Analytics Reports'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
if ($profile_id) {
module_load_include('inc', 'google_analytics_api', 'google_analytics_api.pages');
$admin_form = google_analytics_api_admin();
$form['google_analytics_reports']['google_analytics_reports_profile_id'] = $admin_form['ga']['google_analytics_reports_profile_id'];
$form['google_analytics_reports']['google_analytics_reports_profile_id']['#description'] = t('Choose your Google Analytics profile.');
}
else {
$form['google_analytics_reports']['authorize']['#markup'] = '<p>' . t('You must <a href="!url">authorize</a> Drupal to use your Analytics account before you can view reports.', array('!url' => url('admin/config/system/google-analytics-reports'))) . '</p>';
}
return $form;
}