Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Next

* Deprecated: Universal Analytics APIs (Core Reporting v3, Management v3) and ClientLogin used by `Garb` have been shut down by Google; README now points to GA4 replacement gems
* Replaced deprecated `MultiJson` with stdlib `JSON` and dropped the `multi_json` dependency
* Added `ostruct` as a runtime dependency (no longer a default gem starting with Ruby 3.5)
* Declared `required_ruby_version` as `>= 3.2`, matching the CI matrix
* Fixed "method redefined" warning when the same report method is added more than once
* [#75](https://github.com/Sija/garb/pull/75): Fixed `Garb::Model#all_results` (`:all => true`) ignoring `:offset` after the first page - [@dblock](https://github.com/dblock)

Version 0.9.8
Expand Down
54 changes: 40 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,32 @@ Garb [![Gem Version](https://badge.fury.io/rb/garb.svg)](https://badge.fury.io/r

https://github.com/Sija/garb

Google Shutting Down Deprecated Auth on April 20th, 2015
--------------------------------------------------------
Deprecation Notice
------------------

**This gem is no longer functional against Google's live services and is kept for historical purposes only.**

* `Garb` targets the Google Analytics **Core Reporting API v3** and **Management API v3** (Universal Analytics). Google stopped processing Universal Analytics data in July 2023 (July 2024 for Analytics 360) and has since shut both APIs down. See https://support.google.com/analytics/answer/11583528
* Username/password authentication (`Garb::Session.login`) relies on **ClientLogin**, which Google shut down on April 20th, 2015.
* Google Analytics 4 (GA4) uses a different API with a different data model (e.g. `sessions`/`screenPageViews` instead of `ga:visits`/`ga:pageviews`, properties instead of profiles), so `Garb` cannot be pointed at it.

Replacements
------------

Use one of these actively maintained, Google-published gems instead:

| Gem | Replaces | Notes |
| --- | --- | --- |
| [`google-analytics-data`](https://rubygems.org/gems/google-analytics-data) | Reporting (`Garb::Model`, `results`) | Official GA4 Data API client (gRPC-based, recommended) |
| [`google-analytics-admin`](https://rubygems.org/gems/google-analytics-admin) | `Garb::Management::*` | Official GA4 Admin API client (accounts, properties, data streams, key events) |
| [`google-apis-analyticsdata_v1beta`](https://rubygems.org/gems/google-apis-analyticsdata_v1beta) | Reporting | REST-based GA4 Data API client, lighter alternative to the gRPC one |
| [`google-apis-analyticsadmin_v1beta`](https://rubygems.org/gems/google-apis-analyticsadmin_v1beta) | `Garb::Management::*` | REST-based GA4 Admin API client |

On April 20th, Google will be shutting down ClientLogin, AuthSub, and OAuth 1.0. https://groups.google.com/forum/#!topic/google-analytics-api-notify/g8wbdUqEDd0
Authentication for all of the above is handled by [`googleauth`](https://rubygems.org/gems/googleauth) (service accounts, OAuth 2.0, Application Default Credentials).

Legato
------
[Legato](https://github.com/tpitale/legato), previously recommended here as a successor, also targets the retired Universal Analytics API and is no longer an option.

There's rewritten and (moar) actively maintained version of the library in the form of [Legato](https://github.com/tpitale/legato).
You might want to check it out as it's mostly compatible with `Garb`.
Getting started with GA4: [Data API overview](https://developers.google.com/analytics/devguides/reporting/data/v1) and [quickstart](https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart)

Important Changes
-----------------
Expand All @@ -35,6 +51,8 @@ Basic Usage
Single User Login
-----------------

**Defunct:** ClientLogin was shut down by Google in April 2015; this method no longer works.

```ruby
Garb::Session.api_key = api_key # required for 2-step authentication
Garb::Session.login(username, password)
Expand All @@ -43,6 +61,8 @@ Garb::Session.login(username, password)
OAuth Access Token
------------------

The only authentication method that was still supported until the Universal Analytics APIs were retired.

```ruby
Garb::Session.access_token = access_token # an instance of OAuth2::Client
```
Expand Down Expand Up @@ -153,6 +173,8 @@ profile.exits(filters: { :page_path.eql => '/extend/effectively-using-git-with-s
SSL
---

**Defunct:** this only applies to single user (ClientLogin) authentication, see above.

Version 0.2.3 includes support for real ssl encryption for SINGLE USER authentication. First do:

```ruby
Expand Down Expand Up @@ -184,15 +206,19 @@ TODOS
Requirements
------------

* active_support >= 2.2
* multi_json >= 1.3
* Ruby (tested on 3.2, 3.3 and 3.4)
* activesupport >= 2.2
* ostruct

Running Tests
-------------

Requirements for Testing
------------------------
The test suite stubs all network calls, so no Google credentials or API keys are needed:

* shoulda
* mocha
* bourne
```sh
bundle install
bundle exec rake
```

Install
-------
Expand Down
3 changes: 2 additions & 1 deletion garb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = 'garb'
gem.require_paths = ['lib']
gem.required_ruby_version = '>= 3.2'
gem.version = Garb::VERSION

gem.add_dependency 'activesupport', '>= 2.2'
gem.add_dependency 'multi_json', '>= 1.3'
gem.add_dependency 'ostruct', '~> 0.6'
end
2 changes: 1 addition & 1 deletion lib/garb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'cgi'
require 'ostruct'

require 'multi_json'
require 'json'

module Garb
autoload :VERSION, 'garb/version'
Expand Down
2 changes: 1 addition & 1 deletion lib/garb/management/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(session, path)
end

def parsed_response
@parsed_response ||= MultiJson.load(response.body)
@parsed_response ||= JSON.parse(response.body)
end

def entries
Expand Down
8 changes: 8 additions & 0 deletions lib/garb/profile_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ def self.add_report_method(klass)
method_name = klass.name.to_s.demodulize.underscore
return if method_name.empty?

# silence "method redefined" warnings without leaving a window where
# the method is undefined: aliasing marks the current definition as
# referenced, and removing the alias leaves the original in place
if method_defined?(method_name, false)
alias_method :__garb_redefine, method_name
remove_method :__garb_redefine
end

class_eval <<-CODE
def #{method_name}(opts = {}, &block)
#{klass}.results(self, opts, &block)
Expand Down
2 changes: 1 addition & 1 deletion lib/garb/report_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def rows

def data
unless @data
@data = MultiJson.load @response_body
@data = JSON.parse @response_body
@data = @data.each_with_object({}) do |pair, data|
key, value = pair
data[key.underscore.to_sym] = value
Expand Down
2 changes: 1 addition & 1 deletion lib/garb/request/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def send_request

def handle_response(response)
unless response.kind_of?(Net::HTTPSuccess) || (response.respond_to?(:status) && response.status == 200)
body, parsed = response.body, MultiJson.load(response.body) rescue nil
body, parsed = response.body, JSON.parse(response.body) rescue nil
Comment thread
Sija marked this conversation as resolved.
Resolved
if parsed && (error = parsed['error'])
klass = case error['code']
when 400 then BadRequestError
Expand Down
2 changes: 1 addition & 1 deletion test/unit/garb/management/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AccountTest < MiniTest::Unit::TestCase

context "an Account" do
setup do
entry = MultiJson.load(read_fixture("account_management.json"))['items'].first
entry = JSON.parse(read_fixture('account_management.json'))['items'].first
Comment thread
Sija marked this conversation as resolved.
Resolved
@account = Account.new(entry, Session)
end

Expand Down
4 changes: 2 additions & 2 deletions test/unit/garb/management/feed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class FeedTest < MiniTest::Unit::TestCase
end

should "have a parsed response" do
MultiJson.stubs(:load)
JSON.stubs(:parse)
@feed.stubs(:response).returns(stub(:body => 'response body'))
@feed.parsed_response

assert_received(MultiJson, :load) {|e| e.with('response body')}
assert_received(JSON, :parse) { |e| e.with('response body') }
end

should "have entries from the parsed response" do
Expand Down
2 changes: 1 addition & 1 deletion test/unit/garb/management/goal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GoalTest < MiniTest::Unit::TestCase

context "a Goal" do
setup do
entry = MultiJson.load(read_fixture("goal_management.json"))["items"].first
entry = JSON.parse(read_fixture('goal_management.json'))['items'].first
Comment thread
Sija marked this conversation as resolved.
Resolved
@goal = Goal.new(entry, Session)
end

Expand Down
2 changes: 1 addition & 1 deletion test/unit/garb/management/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProfileTest < MiniTest::Unit::TestCase

context "A Profile" do
setup do
entry = MultiJson.load(read_fixture("ga_profile_management.json"))["items"].first
entry = JSON.parse(read_fixture('ga_profile_management.json'))['items'].first
Comment thread
Sija marked this conversation as resolved.
Resolved
@profile = Profile.new(entry, Session)
end

Expand Down
2 changes: 1 addition & 1 deletion test/unit/garb/management/segment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SegmentTest < MiniTest::Unit::TestCase

context "A Segment" do
setup do
entry = MultiJson.load(read_fixture("ga_segment_management.json"))["items"].first
entry = JSON.parse(read_fixture('ga_segment_management.json'))['items'].first
Comment thread
Sija marked this conversation as resolved.
Resolved
@segment = Segment.new(entry, Session)
end

Expand Down
2 changes: 1 addition & 1 deletion test/unit/garb/management/web_property_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class WebPropertyTest < MiniTest::Unit::TestCase

context "a WebProperty" do
setup do
entry = MultiJson.load(read_fixture("ga_webproperty_management.json"))["items"].first
entry = JSON.parse(read_fixture('ga_webproperty_management.json'))['items'].first
Comment thread
Sija marked this conversation as resolved.
Resolved

@web_property = WebProperty.new(entry, Session)
end
Expand Down
2 changes: 1 addition & 1 deletion test/unit/garb/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ModelTest < MiniTest::Unit::TestCase

context "with a profile" do
setup do
entry = MultiJson.load(read_fixture('ga_profile_management.json'))['items'].first
entry = JSON.parse(read_fixture('ga_profile_management.json'))['items'].first
Comment thread
Sija marked this conversation as resolved.
Resolved

@profile = Garb::Management::Profile.new(entry, Session)
end
Expand Down
2 changes: 1 addition & 1 deletion test/unit/garb/report_response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ReportResponseTest < MiniTest::Unit::TestCase

should "return an empty array if there are no results" do
response = ReportResponse.new('result json')
MultiJson.stubs(:load).with('result json').returns({'rows' => []})
JSON.stubs(:parse).with('result json').returns('rows' => [])

assert_equal [], response.results.to_a
end
Expand Down