google_analytics_v4_api 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 00e78b59df928bfa8edea30095716394a73902e8b49e67dbf80d43a50bd0abae
4
- data.tar.gz: ebb7c0c7726fcfd28692959614d11d5e47c5b35f468f353df1b157e6c6516023
3
+ metadata.gz: 5828f263cac248518f859fccee88c665c9b69ce3f34deaa4a3ffc2f2d00b8048
4
+ data.tar.gz: 1931b205744ff560afad9fb9b62666ef334b2cbda04551618bab5b5bced048dc
5
5
  SHA512:
6
- metadata.gz: 9cf3584d6552e6c46849ad878dc3a987d5b835d198f138a4f8125722e5c50b47466adc337999235adf8c1ec8ba4a5fa3ae70b454209b838e860da2c9e156938f
7
- data.tar.gz: f12a6255bda5ce39749cd152f515d8fbc6e3fa99c3b5ae087dd356f582fdcff45b73ac49c278ec184dc7933df208e84353166bd2dec019d8cd812bb614af5f53
6
+ metadata.gz: 1647ea49bf56b68d0303c4730e3cdcdda21bc97ab88f59ab3e55f3c10e5adffbede326db4c9a3496f71f637b7ba909f9de0fed3c8e7010912c359c90d1edfeca
7
+ data.tar.gz: b20b7207cb4d6575a3fb94f4dbb1a3688e2ca25b3d7a30fdff6029eaed96cfd6dfa919f0cb71b9a36fbf8db40212beefdb311c7b17e28a5091a34dd37c50cf03
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- google_analytics_v4_api (0.0.1)
4
+ google_analytics_v4_api (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Google Analytics v4 API Ruby Gem
2
2
  This is a simple wrapper to interact with the Google Analytics v4 API (currently in beta) with Ruby.
3
- It's based on the [API guide](https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1beta)
3
+ It's based on the [Admin API guide](https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1beta) and the
4
+ [Reports API guide](https://developers.google.com/analytics/devguides/reporting/data/v1/basics?authuser=1#report_response)
4
5
 
5
6
  ## Usage
6
7
  Add this gem to your Gemfile:
@@ -13,14 +14,19 @@ You will need a way to get a user's valid (and fresh) token (I personally use th
13
14
  ```rb
14
15
  client = GoogleAnalyticsV4Api::Client.new(token)
15
16
  # List all the accounts
16
- client.accounts
17
+ accounts = client.accounts
17
18
  # Or get one particular account
18
- client.account("account/24696xxx")
19
+ account = client.account("account/24696xxx")
19
20
 
20
21
  # List all the properties for a particular account
21
- client.properties("accounts/24596xxx")
22
+ properties = client.properties("accounts/24596xxx")
22
23
  # Or get one particular property
23
- client.property("properties/33783xxx")
24
+ property = client.property("properties/33783xxx")
25
+
26
+ # Or simply call the properties for an Account object
27
+ properties = account.properties
28
+ # Or one particular property
29
+ property = account.property("properties/33783xxx")
24
30
  ```
25
31
 
26
32
  The rest is still being defined.
@@ -6,19 +6,24 @@ module GoogleAnalyticsV4Api
6
6
  class Account
7
7
  attr_accessor :name, :createTime, :updateTime, :displayName, :regionCode
8
8
 
9
- def initialize(attributes = {})
9
+ def initialize(client, attributes = {})
10
+ @client = client
10
11
  attributes.each do |k, v|
11
12
  self.send("#{k}=", v)
12
13
  end
13
14
  end
14
15
 
15
16
  def properties
17
+ @client.properties(name)
18
+ end
16
19
 
20
+ def property(property_name)
21
+ @client.property(property_name)
17
22
  end
18
23
 
19
- def self.parse_list(body)
24
+ def self.parse_list(client, body)
20
25
  JSON.parse(body)["accounts"].map do |attrs|
21
- GoogleAnalyticsV4Api::Account.new(attrs)
26
+ GoogleAnalyticsV4Api::Account.new(client, attrs)
22
27
  end
23
28
  end
24
29
  end
@@ -1,20 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
- require "net/http"
5
- require "uri"
6
-
7
3
  module GoogleAnalyticsV4Api
8
4
  class Client
9
5
 
10
- BASE_URL = "https://analyticsadmin.googleapis.com/v1beta"
11
-
12
6
  def initialize(access_token)
13
7
  @access_token = access_token
14
8
  end
15
9
 
16
10
  def accounts
17
- @accounts ||= GoogleAnalyticsV4Api::Account.parse_list get("/accounts").body
11
+ @accounts ||= GoogleAnalyticsV4Api::Account.parse_list(self, get("/accounts"))
18
12
  end
19
13
 
20
14
  def account(account_name)
@@ -22,7 +16,10 @@ module GoogleAnalyticsV4Api
22
16
  end
23
17
 
24
18
  def properties(account_name)
25
- @properties ||= GoogleAnalyticsV4Api::Property.parse_list get("/properties", { filter: "parent:#{account_name}"}).body
19
+ @properties ||= Hash.new do |h, key|
20
+ h[key] = GoogleAnalyticsV4Api::Property.parse_list(self, get("/properties", { filter: "parent:#{key}"}))
21
+ end
22
+ @properties[account_name]
26
23
  end
27
24
 
28
25
  def property(property_name)
@@ -31,25 +28,13 @@ module GoogleAnalyticsV4Api
31
28
  return property unless property.nil?
32
29
  end
33
30
 
34
- GoogleAnalyticsV4Api::Property.parse get("/#{property_name}").body
31
+ GoogleAnalyticsV4Api::Property.parse(self, get("/#{property_name}"))
35
32
  end
36
33
 
37
34
  private
38
35
  def get(path, params = {})
39
- url = "#{BASE_URL}#{path}"
40
- url += "?#{URI.encode_www_form params}" unless params.empty?
41
- uri = URI(url)
42
-
43
- response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
44
- request = Net::HTTP::Get.new uri
45
- request["Authorization"] = "Bearer #{@access_token}"
46
- request["Content-Type"] = "application/json"
47
-
48
- http.request request
49
- end
50
- raise GoogleAnalyticsV4Api::Error.new(response) unless response.is_a?(Net::HTTPSuccess)
51
-
52
- response
36
+ Request.get(access_token: @access_token, path: path, params: params).body
53
37
  end
38
+
54
39
  end
55
40
  end
@@ -6,21 +6,25 @@ module GoogleAnalyticsV4Api
6
6
  class Property
7
7
  attr_accessor :name, :propertyType, :createTime, :updateTime, :parent, :displayName, :industryCategory, :timeZone, :currencyCode, :serviceLevel, :deleteTime, :expireTime, :account
8
8
 
9
- def initialize(attributes = {})
9
+ def initialize(client, attributes = {})
10
+ @client = client
10
11
  attributes.each do |k, v|
11
12
  self.send("#{k}=", v)
12
13
  end
13
14
  end
14
15
 
15
- def self.parse_list(body)
16
+ def account
17
+ @client.account(parent)
18
+ end
19
+
20
+ def self.parse_list(client, body)
16
21
  JSON.parse(body)["properties"].map do |attrs|
17
- GoogleAnalyticsV4Api::Property.new(attrs)
22
+ GoogleAnalyticsV4Api::Property.new(client, attrs)
18
23
  end
19
24
  end
20
25
 
21
- def self.parse(body)
22
- GoogleAnalyticsV4Api::Property.new(JSON.parse(body))
26
+ def self.parse(client, body)
27
+ GoogleAnalyticsV4Api::Property.new(client, JSON.parse(body))
23
28
  end
24
-
25
29
  end
26
30
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+
6
+ module GoogleAnalyticsV4Api
7
+ class Request
8
+
9
+ ADMIN_URL = "https://analyticsadmin.googleapis.com/v1beta"
10
+ DATA_URL = "https://analyticsdata.googleapis.com/v1beta/properties/{{PROPERTY_ID}}:runReport"
11
+
12
+ def self.get(access_token:, url: nil, path:, params: {})
13
+ url = "#{url || ADMIN_URL}#{path}"
14
+ url += "?#{URI.encode_www_form params}" unless params.empty?
15
+ uri = URI(url)
16
+
17
+ response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
18
+ request = Net::HTTP::Get.new uri
19
+ request["Authorization"] = "Bearer #{access_token}"
20
+ request["Content-Type"] = "application/json"
21
+
22
+ http.request request
23
+ end
24
+ raise GoogleAnalyticsV4Api::Error.new(response) unless response.is_a?(Net::HTTPSuccess)
25
+
26
+ response
27
+ end
28
+
29
+ def self.post(access_token:, url: nil, path:, params: {})
30
+
31
+ end
32
+
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module GoogleAnalyticsV4Api
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -4,5 +4,6 @@ module GoogleAnalyticsV4Api
4
4
  autoload :Client, "google_analytics_v4_api/client"
5
5
  autoload :Account, "google_analytics_v4_api/account"
6
6
  autoload :Property, "google_analytics_v4_api/property"
7
+ autoload :Request, "google_analytics_v4_api/request"
7
8
  autoload :Error, "google_analytics_v4_api/error"
8
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_analytics_v4_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gustavo Garcia
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-20 00:00:00.000000000 Z
11
+ date: 2023-01-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A very humble wrapper for the Google Analytics V4 API to be used in conjuntion
14
14
  with OAuth2
@@ -33,6 +33,7 @@ files:
33
33
  - lib/google_analytics_v4_api/client.rb
34
34
  - lib/google_analytics_v4_api/error.rb
35
35
  - lib/google_analytics_v4_api/property.rb
36
+ - lib/google_analytics_v4_api/request.rb
36
37
  - lib/google_analytics_v4_api/version.rb
37
38
  homepage: https://github.com/dailytics/google_analytics_v4_api
38
39
  licenses: