-
Notifications
You must be signed in to change notification settings - Fork 4
/
import_data.php
executable file
·255 lines (215 loc) · 8.8 KB
/
import_data.php
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
<?php
// library for importing data from Google Analytics into DB
/**
* Import browser and os statistics
* @param int $year year of the required data
* @param int $month month of the required data
* @return 1 if successful or the mysql error
*/
function import_browser_os($year, $month) {
$ga = new gapi(ga_email,ga_password);
$dimensions = array('year', 'month', 'browser', 'browserVersion', 'operatingSystem', 'operatingSystemVersion', 'isMobile');
$metrics = array('visitors');
$sort_metric = null;
$filter = null;
$start_date = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$end_date = date('Y-m-d', mktime(0, 0, 0, $month+1, 0, $year));
$start_index = 1;
$max_results = 10000;
$segment = ga_no_external;
$ga->requestReportData(ga_profile_id, $dimensions, $metrics, $sort_metric, $filter, $start_date, $end_date, $start_index, $max_results, $segment);
foreach($ga->getResults() as $result):
$query = "INSERT INTO browsers SET ";
$query .= " year = ".$result->getYear();
$query .= ",month = ".$result->getMonth();
$query .= ",browser = '".$result->getBrowser()."'";
$query .= ",browserVersion = '".$result->getBrowserVersion()."'";
$query .= ",operatingSystem = '".$result->getOperatingSystem()."'";
$query .= ",operatingSystemVersion = '".$result->getOperatingSystemVersion()."'";
$query .= ",isMobile = ".($result->getIsMobile() == 'Yes' ? 1 : 0);
$query .= ",visitors = ".$result->getVisitors();
$query .= " ON DUPLICATE KEY UPDATE";
$query .= " visitors = ".$result->getVisitors();
$query .= ";";
$result_query = mysql_query($query);
if (!$result_query) {
die('Invalid query: ' . mysql_error());
}
endforeach;
return 1;
}
/**
* Import screen resolution statistics
* @param int $year year of the required data
* @param int $month month of the required data
* @return 1 if successful or the mysql error
*/
function import_screen_res($year, $month) {
$ga = new gapi(ga_email,ga_password);
$dimensions = array('year', 'month', 'screenResolution');
$metrics = array('visitors');
$sort_metric = null;
$filter = null;
$start_date = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$end_date = date('Y-m-d', mktime(0, 0, 0, $month+1, 0, $year));
$start_index = 1;
$max_results = 10000;
$segment = ga_no_external;
$ga->requestReportData(ga_profile_id, $dimensions, $metrics, $sort_metric, $filter, $start_date, $end_date, $start_index, $max_results, $segment);
foreach($ga->getResults() as $result):
$arr = array();
$arr = explode('x', $result->getScreenResolution());
$query = "INSERT INTO screens SET ";
$query .= " year = ".$result->getYear();
$query .= ",month = ".$result->getMonth();
$query .= ",width = ".$arr[0];
$query .= ",height = ".$arr[1];
$query .= ",visitors = ".$result->getVisitors();
$query .= " ON DUPLICATE KEY UPDATE";
$query .= " visitors = ".$result->getVisitors();
$query .= ";";
$result_query = mysql_query($query);
if (!$result_query) {
die('Invalid query: ' . mysql_error());
}
endforeach;
return 1;
}
/**
* Import countries statistics
* @param int $year year of the required data
* @param int $month month of the required data
* @return 1 if successful or the mysql error
*/
function import_countries($year, $month) {
$ga = new gapi(ga_email,ga_password);
$dimensions = array('year', 'month', 'country');
$metrics = array('visitors');
$sort_metric = null;
$filter = null;
$start_date = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$end_date = date('Y-m-d', mktime(0, 0, 0, $month+1, 0, $year));
$start_index = 1;
$max_results = 10000;
$segment = ga_no_external;
$ga->requestReportData(ga_profile_id, $dimensions, $metrics, $sort_metric, $filter, $start_date, $end_date, $start_index, $max_results, $segment);
foreach($ga->getResults() as $result):
$query = "INSERT INTO countries SET ";
$query .= " year = ".$result->getYear();
$query .= ",month = ".$result->getMonth();
$query .= ",country = '".$result->getCountry()."'";
$query .= ",visitors = ".$result->getVisitors();
$query .= " ON DUPLICATE KEY UPDATE";
$query .= " visitors = ".$result->getVisitors();
$query .= ";";
$result_query = mysql_query($query);
if (!$result_query) {
die('Invalid query: ' . mysql_error());
}
endforeach;
return 1;
}
/**
* Import access time statistics
* @param int $year year of the required data
* @param int $month month of the required data
* @return 1 if successful or the mysql error
*/
function import_access_time($year, $month) {
$ga = new gapi(ga_email,ga_password);
$dimensions = array('date','dayOfWeek','hour');
$metrics = array('visitors','visits','avgTimeOnSite');
$sort_metric = array('date', 'hour');
$filter = null;
$start_date = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$end_date = date('Y-m-d', mktime(0, 0, 0, $month+1, 0, $year));
$start_index = 1;
$max_results = 10000;
$segment = null;
$ga->requestReportData(ga_profile_id, $dimensions, $metrics, $sort_metric, $filter, $start_date, $end_date, $start_index, $max_results, $segment);
foreach($ga->getResults() as $result):
$date = $result->getDate();
$new_date = substr($date, 0, 4)."-".substr($date, 4, 2)."-".substr($date, 6, 2);
$query = "INSERT INTO accesstimes SET ";
$query .= " date = '".$new_date."'";
$query .= ",dayOfWeek = ".$result->getDayOfWeek();
$query .= ",hour = ".$result->getHour();
$query .= ",visitors = ".$result->getVisitors();
$query .= ",visits = ".$result->getVisits();
$query .= ",avgTimeOnSite = ".$result->getAvgTimeOnSite();
$query .= " ON DUPLICATE KEY UPDATE";
$query .= " visitors = ".$result->getVisitors();
$query .= ",visits = ".$result->getVisits();
$query .= ",avgTimeOnSite = ".$result->getAvgTimeOnSite();
$query .= ";";
$result_query = mysql_query($query);
if (!$result_query) {
die('Invalid query: ' . mysql_error());
}
endforeach;
// no external visits
$segment = ga_no_external;
$ga->requestReportData(ga_profile_id, $dimensions, $metrics, $sort_metric, $filter, $start_date, $end_date, $start_index, $max_results, $segment);
foreach($ga->getResults() as $result):
$date = $result->getDate();
$new_date = substr($date, 0, 4)."-".substr($date, 4, 2)."-".substr($date, 6, 2);
$query = "INSERT INTO accesstimes SET ";
$query .= " date = '".$new_date."'";
$query .= ",dayOfWeek = ".$result->getDayOfWeek();
$query .= ",hour = ".$result->getHour();
$query .= ",noext_visitors = ".$result->getVisitors();
$query .= ",noext_visits = ".$result->getVisits();
$query .= ",noext_avgTimeOnSite = ".$result->getAvgTimeOnSite();
$query .= " ON DUPLICATE KEY UPDATE";
$query .= " noext_visitors = ".$result->getVisitors();
$query .= ",noext_visits = ".$result->getVisits();
$query .= ",noext_avgTimeOnSite = ".$result->getAvgTimeOnSite();
$query .= ";";
$result_query = mysql_query($query);
if (!$result_query) {
die('Invalid query: ' . mysql_error());
}
endforeach;
return 1;
}
/**
* Import help pages statistics
* @param int $year year of the required data
* @param int $month month of the required data
* @return 1 if successful or the mysql error
*/
function import_help_page($year, $month) {
$ga = new gapi(ga_email,ga_password);
$dimensions = array('year', 'month', 'pagePath');
$metrics = array('visitors', 'pageviews', 'uniquePageviews', 'avgTimeOnPage');
$sort_metric = null;
$filter = 'ga:pagePath=@/_act/get_page.php?pgName=/help/';
$start_date = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$end_date = date('Y-m-d', mktime(0, 0, 0, $month+1, 0, $year));
$start_index = 1;
$max_results = 10000;
$segment = ga_no_external;
$ga->requestReportData(ga_profile_id, $dimensions, $metrics, $sort_metric, $filter, $start_date, $end_date, $start_index, $max_results, $segment);
foreach($ga->getResults() as $result):
$query = "INSERT INTO helppages SET ";
$query .= " year = ".$result->getYear();
$query .= ",month = ".$result->getMonth();
$query .= ",pagePath = '".$result->getPagePath()."'";
$query .= ",visitors = ".$result->getVisitors();
$query .= ",pageviews = ".$result->getPageviews();
$query .= ",uniquePageviews = ".$result->getUniquePageviews();
$query .= ",avgTimeOnPage = ".$result->getAvgTimeOnPage();
$query .= " ON DUPLICATE KEY UPDATE";
$query .= " visitors = ".$result->getVisitors();
$query .= ",pageviews = ".$result->getPageviews();
$query .= ",uniquePageviews = ".$result->getUniquePageviews();
$query .= ",avgTimeOnPage = ".$result->getAvgTimeOnPage();
$query .= ";";
$result_query = mysql_query($query);
if (!$result_query) {
die('Invalid query: ' . mysql_error());
}
endforeach;
return 1;
}
?>