-
Notifications
You must be signed in to change notification settings - Fork 7
/
google_analytics_reports.install
262 lines (228 loc) · 8.68 KB
/
google_analytics_reports.install
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* @file
* Contains install and update functions for Google Analytics Reports module.
*/
/**
* Implements hook_install().
*/
function google_analytics_reports_install() {
db_update('system')
->fields(array('weight' => 11))
->condition('name', 'google_analytics_reports', '=')
->execute();
}
/**
* Implements hook_requirements().
*/
function google_analytics_reports_requirements($phase) {
$requirements = array();
// Ensure translations don't break during installation.
$t = get_t();
// Do not install module if old Google Analytics Reports Views module
// is exists.
if (($phase == 'install') || ($phase == 'runtime')) {
if (module_exists('google_analytics_reports_views')) {
$requirements['google_analytics_reports_views'] = array(
'title' => $t('Google Analytics Reports'),
'value' => $t('You must uninstall and permanently delete all files of <strong>Google Analytics Reports Views</strong> module before installing the new version of Google Analytics Reports module.'),
'severity' => REQUIREMENT_ERROR,
);
}
}
// Warning about using Google Analytics Reports blocks without Ajax loading.
if (($phase == 'runtime') && (!module_exists('ajaxblocks'))) {
$theme_default = variable_get('theme_default');
module_load_include('inc', 'block', 'block.admin');
// List of all blocks.
$blocks = block_admin_display_prepare_blocks($theme_default);
foreach ($blocks as $block) {
// Check if Google Analytics Reports blocks are enabled.
if (($block['module'] == 'views') && ($block['status']) && (strpos($block['info'], 'Google Analytics Reports') === 0)) {
$requirements['google_analytics_reports_blocks'] = array(
'title' => $t('Google Analytics Reports blocks'),
'value' => $t('You are using Google Analytics Reports blocks which may increase page load time because of requests to Google Analytics. It is recommended to load these blocks using Ajax via !ajaxblocks module.', array('!ajaxblocks' => l($t('Ajax Blocks'), 'https://www.drupal.org/project/ajaxblocks'))),
'severity' => REQUIREMENT_WARNING,
);
}
}
}
return $requirements;
}
/**
* Divide start_end filter into two start_date and end_date filters.
*/
function google_analytics_reports_update_7300() {
// Find all Google Analytics views.
$ga_views = db_select('views_view', 'v')
->fields('v', array('vid'))
->condition('base_table', 'google_analytics')
->execute()
->fetchAll();
foreach ($ga_views as $ga_view) {
// Find all displays settings.
$ga_views_displays = db_select('views_display', 'v')
->fields('v', array('id', 'display_options'))
->condition('vid', $ga_view->vid)
->execute()
->fetchAll();
if ($ga_views_displays) {
foreach ($ga_views_displays as $ga_views_display) {
$display_options = unserialize($ga_views_display->display_options);
// If view has start_end filter.
if (isset($display_options['filters']['start_end'])) {
// Copy start_end filter into start_date and end_date filters.
$start_date = $end_date = $start_end = $display_options['filters']['start_end'];
$start_date['operator'] = $end_date['operator'] = '=';
$start_date['value']['min'] = $end_date['value']['min'] = '';
$start_date['value']['max'] = $end_date['value']['max'] = '';
$start_date['value']['value'] = $start_end['value']['max'];
$start_date['id'] = $start_date['field'] = 'start_date';
$end_date['value']['value'] = $start_end['value']['min'];
$end_date['id'] = $end_date['field'] = 'end_date';
unset($display_options['filters']['start_end']);
$display_options['filters']['start_date'] = $start_date;
$display_options['filters']['end_date'] = $end_date;
$display_options = serialize($display_options);
db_update('views_display')
->fields(array('display_options' => $display_options))
->condition('vid', $ga_view->vid)
->condition('id', $ga_views_display->id)
->execute();
}
}
}
}
}
/**
* Convert datapoins with (n) in name into datapoints with XX.
*/
function google_analytics_reports_update_7301() {
// Find all Google Analytics views.
$ga_views = db_select('views_view', 'v')
->fields('v', array('vid'))
->condition('base_table', 'google_analytics')
->execute()
->fetchAll();
foreach ($ga_views as $ga_view) {
// Find all displays settings.
$ga_views_displays = db_select('views_display', 'v')
->fields('v', array('id', 'display_options'))
->condition('vid', $ga_view->vid)
->execute()
->fetchAll();
if ($ga_views_displays) {
foreach ($ga_views_displays as $ga_views_display) {
// Convert (n) into XX.
$replaced_data = str_replace('(n)', 'XX', $ga_views_display->display_options);
// Fix serialize data.
// See http://stackoverflow.com/a/21389439/3365600.
$fixed_data = preg_replace_callback('!s:(\d+):"(.*?)";!', function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
}, $replaced_data);
db_update('views_display')
->fields(array('display_options' => $fixed_data))
->condition('vid', $ga_view->vid)
->condition('id', $ga_views_display->id)
->execute();
}
}
}
}
/**
* Replace deprecated datapoins with new ones.
*/
function google_analytics_reports_update_7302() {
// Find all Google Analytics views.
$ga_views = db_select('views_view', 'v')
->fields('v', array('vid'))
->condition('base_table', 'google_analytics')
->execute()
->fetchAll();
foreach ($ga_views as $ga_view) {
// Find all displays settings.
$ga_views_displays = db_select('views_display', 'v')
->fields('v', array('id', 'display_options'))
->condition('vid', $ga_view->vid)
->execute()
->fetchAll();
if ($ga_views_displays) {
foreach ($ga_views_displays as $ga_views_display) {
$old_datapoins = array(
'visitorType',
'visitCount',
'daysSinceLastVisit',
'visitors',
'newVisits',
'percentNewVisits',
'visitLength',
'visits',
'timeOnSite',
'entranceBounceRate',
'visitBounceRate',
'avgTimeOnSite',
'goalValuePerVisit',
'pageviewsPerVisit',
'searchVisits',
'percentVisitsWithSearch',
'visitsWithEvent',
'eventsPerVisitWithEvent',
'visitsToTransaction',
'transactionsPerVisit',
'transactionRevenuePerVisit',
'socialInteractionsPerVisit',
'visitorAgeBracket',
'visitorGender',
);
$new_datapoints = array(
'userType',
'sessionCount',
'daysSinceLastSession',
'users',
'newUsers',
'percentNewSessions',
'sessionDurationBucket',
'sessions',
'sessionDuration',
'bounceRate',
'bounceRate',
'avgSessionDuration',
'goalValuePerSession',
'pageviewsPerSession',
'searchSessions',
'percentSessionsWithSearch',
'sessionsWithEvent',
'eventsPerSessionWithEvent',
'sessionsToTransaction',
'transactionsPerSession',
'transactionRevenuePerSession',
'socialInteractionsPerSession',
'userAgeBracket',
'userGender',
);
// Replace deprecated datapoins with new ones.
$replaced_data = str_replace($old_datapoins, $new_datapoints, $ga_views_display->display_options);
// Fix serialize data.
// See http://stackoverflow.com/a/21389439/3365600.
$fixed_data = preg_replace_callback('!s:(\d+):"(.*?)";!', function($match) {
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
}, $replaced_data);
db_update('views_display')
->fields(array('display_options' => $fixed_data))
->condition('vid', $ga_view->vid)
->condition('id', $ga_views_display->id)
->execute();
}
}
}
}
/**
* Disable and uninstall old Google Analytics Reports Views module.
*/
function google_analytics_reports_update_7303(&$sandbox) {
if (module_exists('google_analytics_reports_views')) {
module_disable(array('google_analytics_reports_views'));
drupal_uninstall_modules(array('google_analytics_reports_views'));
return t('Old Google Analytics Reports Views module has been successfully uninstalled.');
}
}