google_api 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ test.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google_api.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ondra
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,453 @@
1
+ Google Analytics
2
+ ================
3
+
4
+ News
5
+ ----
6
+
7
+ For now is released beta version. Pleas use issues for problems.
8
+
9
+ Installation
10
+ ------------
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ `gem 'google_api'`
15
+
16
+ And then execute:
17
+
18
+ `$ bundle`
19
+
20
+ Or install it yourself as:
21
+
22
+ `$ gem install google_api`
23
+
24
+ Configuration
25
+ -------------
26
+
27
+ ```ruby
28
+ GoogleApi.configure do
29
+ client_id "1"
30
+
31
+ ga do
32
+ client_id "2"
33
+ end
34
+ end
35
+
36
+ # ----- OR -----
37
+
38
+ GoogleApi.config.client_id = "4"
39
+ GoogleApi.config.ga.client_id = "5"
40
+ ```
41
+
42
+ <table>
43
+ <thead>
44
+ <tr>
45
+ <th>name</th>
46
+ <th>example</th>
47
+ <th>note</th>
48
+ </tr>
49
+ </thead>
50
+
51
+ <tbody>
52
+ <tr>
53
+ <td><b>client_id</b></td>
54
+ <td><i>123456.apps.googleusercontent.com</i></td>
55
+ <td rowspan="3">required for oauth 2</td>
56
+ </tr>
57
+ <tr>
58
+ <td><b>client_secret</b></td>
59
+ <td><i>123456123456123456</i></td>
60
+ </tr>
61
+ <tr>
62
+ <td><b>redirect_uri</b></td>
63
+ <td><i>http://localhost/oauth2callback</i></td>
64
+ </tr>
65
+ <tr>
66
+ <td><b>client_developer_email</b></td>
67
+ <td><i>123456@developer.gserviceaccount.com</i></td>
68
+ <td rowspan="3">required for login by cert</td>
69
+ </tr>
70
+ <tr>
71
+ <td><b>client_cert_file</b></td>
72
+ <td><i>/home/user/app/123456-privatekey.p12</i></td>
73
+ </tr>
74
+ <tr>
75
+ <td><b>key_secret</b></td>
76
+ <td><i>notasecret</i></td>
77
+ </tr>
78
+ <tr>
79
+ <td colspan="3">only for ga</td>
80
+ </tr>
81
+ <tr>
82
+ <td><b>cache</b></td>
83
+ <td><i>default: </i><b>GoogleApi::Cache.new</b></td>
84
+ <td>more information <a href="#Cache">Cache</a></td>
85
+ </tr>
86
+ </tbody>
87
+ </table>
88
+
89
+ <a name="Cache"></a>
90
+ Cache
91
+ -----
92
+
93
+ Cache must have these methods:
94
+
95
+ `write(key, value, expire)` - expire=0 for never expire<br>
96
+ `read(key)`<br>
97
+ `exists?(key)`<br>
98
+ `delete(key)`
99
+
100
+ Session
101
+ -------
102
+
103
+ There is a 3 way for starting sesssion.
104
+
105
+ ### By cert file
106
+
107
+ `First you must add your developer email to google analytics profile.`
108
+
109
+ ```ruby
110
+ GoogleApi::Ga::Session.login_by_cert
111
+ ```
112
+
113
+ If login return false, trying login again.
114
+
115
+ ```ruby
116
+ GoogleApi::Ga::Session.login_by_cert!
117
+ ```
118
+
119
+ ### By oauth 2
120
+
121
+ ```ruby
122
+ # return uri for oauth 2 login
123
+ GoogleApi::Ga::Session.login
124
+
125
+ # after callback
126
+ # code = code key in query (params[:code])
127
+ GoogleApi::Ga::Session.login(code)
128
+ ```
129
+
130
+ **In rails:**
131
+
132
+ ```ruby
133
+ redirect_to(GoogleApi::Ga::Session.login)
134
+
135
+ # in page specified in redirect_uri
136
+ GoogleApi::Ga::Session.login(params[:code])
137
+ ```
138
+
139
+ ### By oauth 2 via line (browser needed)
140
+
141
+ This will create TCPServer. After login will be closed.
142
+
143
+ **server:** optional is localhost, must be full path!<br>
144
+ **port:** on which port the server will listen
145
+
146
+ ```ruby
147
+ # default:
148
+ # server = http://localhost/oauth2callback
149
+ # port = 0 - get first free port
150
+ GoogleApi::Ga::Session.login_by_line(server, port)
151
+ ```
152
+
153
+ Management
154
+ ----------
155
+
156
+ <table>
157
+ <thead>
158
+ <tr>
159
+ <th colspan="2">Account ~~~ Webproperty ~~~ Profile ~~~ Goal ~~~ Segment</th>
160
+ </tr>
161
+ </thead>
162
+ <tbody>
163
+ <tr>
164
+ <td><b>all</b></td>
165
+ <td>find all</td>
166
+ </tr>
167
+ <tr>
168
+ <td><b>find(id)</b></td>
169
+ <td>find one by id</td>
170
+ </tr>
171
+ <tr>
172
+ <td><b>refresh</b></td>
173
+ <td>refresh data</td>
174
+ </tr>
175
+ </tbody>
176
+ </table>
177
+
178
+ #### Account
179
+
180
+ ```ruby
181
+ # Variables: id, name, created, updated
182
+ # Methods: webproperties
183
+
184
+ GoogleApi::Ga::Account
185
+ ```
186
+
187
+ #### Webproperty
188
+
189
+ ```ruby
190
+ # Variables: id, name, created, updated, accountId, websiteUrl
191
+ # Methods: account, profiles
192
+
193
+ GoogleApi::Ga::Webproperty
194
+ ```
195
+
196
+ #### Profile
197
+
198
+ ```ruby
199
+ # Variables: id, name, created, updated, accountId, websiteUrl, currency, timezone
200
+ # Methods: webproperty, goals
201
+
202
+ GoogleApi::Ga::Profile
203
+ ```
204
+
205
+ #### Goal
206
+
207
+ ```ruby
208
+ # Variables: accountId, webPropertyId, profileId, value, active, type, goal
209
+ # Methods: profile
210
+
211
+ GoogleApi::Ga::Goal
212
+ ```
213
+
214
+ #### Segment
215
+
216
+ ```ruby
217
+ # Variables: segmentId, definition
218
+
219
+ GoogleApi::Ga::Segment
220
+ ```
221
+
222
+ Set default id
223
+ --------------
224
+
225
+ ```ruby
226
+ GoogleApi::Ga.id(123456) # profile id
227
+ ```
228
+
229
+ Data
230
+ ----
231
+
232
+ First you can play on the playground: http://ga-dev-tools.appspot.com/explorer/.
233
+
234
+ ```ruby
235
+ GoogleApi::Ga::Data
236
+ ```
237
+ <br><br><br>
238
+ > ## ids
239
+ >
240
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#ids" target="_blank" style="float:right">doc</a>
241
+ >
242
+ > **alias:** id
243
+ >
244
+ > Id of profile, by default is use id from GoogleApi::Ga.id.
245
+
246
+ <br>
247
+
248
+ > ## start_date
249
+ >
250
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate" target="_blank" style="float:right">doc</a>
251
+ >
252
+ > **alias:** from
253
+ >
254
+ > **default:** _Date.today_<br>
255
+ > **parameters:**<br>
256
+ > &nbsp;&nbsp; _String_ in YYYY-MM-DD or _Date_ or _DateTime_ or _Time_<br>
257
+ > &nbsp;&nbsp; _Integer_ for add or sub days from _Date.today_
258
+
259
+ <br>
260
+
261
+ > ## end_date
262
+ >
263
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate" target="_blank" style="float:right">doc</a>
264
+ >
265
+ > **alias:** from
266
+ >
267
+ > **default:** _Date.today_<br>
268
+ > **parameters:**<br>
269
+ > &nbsp;&nbsp; _String_ in YYYY-MM-DD or _Date_ or _DateTime_ or _Time_<br>
270
+ > &nbsp;&nbsp; _Integer_ for add or sub days from _Date.today_
271
+
272
+ <br>
273
+
274
+ > ## metrics
275
+ >
276
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#metrics" target="_blank" style="float:right">doc</a>
277
+ >
278
+ > required parameters
279
+ >
280
+ > **alias:** select
281
+ >
282
+ > **parameters:** Array with String or Symbol, String (not compiled, <i>"ga:visitors"</i>) or Symbol (compiled, <i>:visitors</i>)
283
+
284
+ <br>
285
+
286
+ > ## dimensions
287
+ >
288
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#dimensions" target="_blank" style="float:right">doc</a>
289
+ >
290
+ > **alias:** with
291
+ >
292
+ > **parameters:** Array with String or Symbol, String (not compiled, <i>"ga:visitors"</i>) or Symbol (compiled, <i>:visitors</i>)
293
+
294
+ <br>
295
+
296
+ > ## sort
297
+ >
298
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#sort" target="_blank" style="float:right">doc</a>
299
+ >
300
+ > **parameters:** Array with String or Symbol, String (not compiled, <i>"ga:visitors"</i>) or Symbol (compiled, <i>:visitors</i>)
301
+
302
+ <br>
303
+
304
+ > ## filters
305
+ >
306
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters" target="_blank" style="float:right">doc</a>
307
+ >
308
+ > **alias:** where
309
+ >
310
+ > **parameters:**<br>
311
+ > {(attribute operator value) & (attribute operator value) | (attribute operator value)} or String (not compiled)
312
+
313
+ <br>
314
+
315
+ > ## segment
316
+ >
317
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#segment" target="_blank" style="float:right">doc</a>
318
+ >
319
+ > **parameters:**<br>
320
+ > {(attribute operator value) & (attribute operator value) | (attribute operator value)} or String (not compiled) <br>
321
+ > with {} automaticly add "dynamic::"
322
+
323
+ <br>
324
+
325
+ > ## start_index
326
+ >
327
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startIndex" target="_blank" style="float:right">doc</a>
328
+ >
329
+ > **alias:** offset
330
+ >
331
+ > **parameters:** Integer from 1.
332
+
333
+ <br>
334
+
335
+ > ## max_results
336
+ >
337
+ > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#maxResults" target="_blank" style="float:right">doc</a>
338
+ >
339
+ > **alias:** limit
340
+ >
341
+ > **parameters:** Integer from 1 to 10 000.
342
+
343
+ <br><br><br>
344
+
345
+ > ## cache
346
+ >
347
+ > For how long in minutes will be data cached. Use 0 for never expire.
348
+
349
+ <br>
350
+
351
+ > ## clear
352
+ >
353
+ > Clear stored data.
354
+
355
+ ### Fetch data
356
+
357
+ You can use one of these. Data is stored in the class.
358
+
359
+ <b>`all`</b> - `[header, rows]`<br>
360
+ <b>`rows`</b> - rows returned by google analytics<br>
361
+ <b>`header`</b> - header of data, (`["ga:day", "ga:month", "ga:visitis"]`)<br>
362
+ <b>`count`</b> - number of rows<br>
363
+ <b>`each`</b> - each as you expected, (`|data|` or `|index, data|`)
364
+
365
+ ### Clear stored data and fetch again
366
+
367
+ clear:<br>
368
+ <b>`clear`</b><br>
369
+
370
+ clear and fetch new:<br>
371
+ <b>`all!`</b>, <b>`rows!`</b>, <b>`header!`</b>, <b>`count!`</b>, <b>`each!`</b><br><br>
372
+
373
+ If you add some parameters clear is called automaticlly.
374
+
375
+ Examples
376
+ --------
377
+
378
+ **Start session:**
379
+ ```ruby
380
+ # set configuration
381
+ GoogleApi.config.ga.client_cert_file = "privatekey.p12"
382
+ GoogleApi.config.ga.client_developer_email = "123456@developer.gserviceaccount.com"
383
+
384
+ # start session
385
+ GoogleApi::Ga::Session.login_by_cert!
386
+
387
+ # get profile id
388
+ id = GoogleApi::Ga::Profile.all.first.id
389
+
390
+ # set default id
391
+ GoogleApi::Ga.id(id)
392
+ ```
393
+ <br>
394
+ **Starting session by line:**<br>
395
+
396
+ First install launchy:<br>
397
+ `gem install launchy`
398
+ ```ruby
399
+ # callback_uri and port can be blank - auto start server at localhost
400
+ GoogleApi::Ga::Session.login_by_line(callback_uri, port)
401
+
402
+ # 1) create server
403
+ # 2) launch browser and redirect to google api
404
+ # 3) confirm and google api redirect to localhost
405
+ # 4) server get code and start session
406
+ # 5) close server
407
+
408
+ # Error if not login
409
+ GoogleApi::Ga::Session.check_session!
410
+ ```
411
+ <br>
412
+ **Management of accounts:**
413
+ ```ruby
414
+ accounts = GoogleApi::Ga::Account.all # all accounts
415
+
416
+ accounts.first.webproperties # webproperties for account
417
+
418
+ GoogleApi::Ga::Webproperty.all # all webproperties
419
+
420
+ GoogleApi::Ga::Profile.all # all profiles
421
+
422
+ GoogleApi::Ga::Goal.all # all goal
423
+
424
+ GoogleApi::Ga::Segment.all # all segment
425
+ ```
426
+ <br>
427
+ **Count of visitors between previous month and today.**
428
+ ```ruby
429
+ GoogleApi::Ga::Data.from(-30).select(:visits).rows
430
+ ```
431
+ <br>
432
+ **Count of visitors between previous month and today - 2, and cache it for 30 minutes.**
433
+ ```ruby
434
+ GoogleApi::Ga::Data.from(-30).to(DateTime.now - 2).select(:visits).cache(30).rows
435
+ ```
436
+ <br>
437
+ **Visitors by day, month, year from Czech Republic. Browser is Firefox and Opera or Chrome**
438
+ ```ruby
439
+ GoogleApi::Ga::Data.from(-30)
440
+ .select(:visits)
441
+ .with(:day, :month, :year)
442
+ .where{(country == "Czech Republic") & (browser == "Firefox") &
443
+ (browser == "Opera") | (browser == "Chrome")}
444
+ .sort(:year, :month, :day)
445
+ .rows
446
+ # OR
447
+ GoogleApi::Ga::Data.from(-30)
448
+ .select(:visits)
449
+ .with(:day, :month, :year)
450
+ .where("ga:country==Czech Republic;ga:browser==Firefox;ga:browser==Opera,ga:browser==Chrome")
451
+ .sort(:year, :month, :day)
452
+ .rows
453
+ ```