google_analytics_v4_api 0.0.2 → 0.0.3

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: 5828f263cac248518f859fccee88c665c9b69ce3f34deaa4a3ffc2f2d00b8048
4
- data.tar.gz: 1931b205744ff560afad9fb9b62666ef334b2cbda04551618bab5b5bced048dc
3
+ metadata.gz: 516d5859258748f1f4749df54b064cc3caf1b9707028041fd0db0084a75d719d
4
+ data.tar.gz: 0030de2106fff16d2ad13e7feb9c98af718fa0c55b04c30a9eecd462ff8df08a
5
5
  SHA512:
6
- metadata.gz: 1647ea49bf56b68d0303c4730e3cdcdda21bc97ab88f59ab3e55f3c10e5adffbede326db4c9a3496f71f637b7ba909f9de0fed3c8e7010912c359c90d1edfeca
7
- data.tar.gz: b20b7207cb4d6575a3fb94f4dbb1a3688e2ca25b3d7a30fdff6029eaed96cfd6dfa919f0cb71b9a36fbf8db40212beefdb311c7b17e28a5091a34dd37c50cf03
6
+ metadata.gz: 3bee4ad056696d0829191e46f6187c0d5a29cfeaebc5ad7fba3ea3b0766aa7d004b1b99ec26f854f4f7cf6d636c94f1f3b097e2ad5991413e7507be3e545443c
7
+ data.tar.gz: 060b03b7c94203270e627dcc35ae8e1262ccbb467040b5221c86518c4815b16a5239d3c77699ea43a8b5b511160d948143a4069aaa3b67b8af8d398e14ef4be6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- google_analytics_v4_api (0.0.2)
4
+ google_analytics_v4_api (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,6 @@
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 [Admin API guide](https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1beta) and the
3
+ It's based on the [Admin API guide](https://developers.google.com/analytics/devguides/config/admin/v1/rest) and the
4
4
  [Reports API guide](https://developers.google.com/analytics/devguides/reporting/data/v1/basics?authuser=1#report_response)
5
5
 
6
6
  ## Usage
@@ -11,12 +11,13 @@ gem 'google_analytics_v4_api'
11
11
 
12
12
  You will need a way to get a user's valid (and fresh) token (I personally use the `gem omnioauth`), and then:
13
13
 
14
+ ### Management API
14
15
  ```rb
15
16
  client = GoogleAnalyticsV4Api::Client.new(token)
16
17
  # List all the accounts
17
18
  accounts = client.accounts
18
19
  # Or get one particular account
19
- account = client.account("account/24696xxx")
20
+ account = client.account("accounts/24696xxx")
20
21
 
21
22
  # List all the properties for a particular account
22
23
  properties = client.properties("accounts/24596xxx")
@@ -29,8 +30,60 @@ properties = account.properties
29
30
  property = account.property("properties/33783xxx")
30
31
  ```
31
32
 
32
- The rest is still being defined.
33
+ ### Data API
34
+ ```rb
35
+ filter = {
36
+ "andGroup": {
37
+ "expressions": [
38
+ {
39
+ "filter": {
40
+ "fieldName": "countryId",
41
+ "stringFilter": {
42
+ "value": "CL"
43
+ }
44
+ }
45
+ },
46
+ {
47
+ "filter": {
48
+ "fieldName": "pagePath",
49
+ "stringFilter": {
50
+ "matchType": "CONTAINS",
51
+ "value": "events",
52
+ "caseSensitive": false
53
+ }
54
+ }
55
+ }
56
+ ]
57
+ }
58
+ }
59
+ report = GoogleAnalyticsV4Api::Report.new(
60
+ dimensions: ['pagePath', 'countryId'],
61
+ metrics: ['sessions', 'screenPageViews'],
62
+ dimension_filter: filter,
63
+ start_date: Date.today - 30, #optional, 30 days ago by default
64
+ end_date: Date.today - 1) #optional, today by default
65
+
66
+ response = property.run_report(report)
67
+
68
+ # Get raw data from the response
69
+ response.raw_dimension_headers
70
+ => [{"name"=>"pagePath"}, {"name"=>"countryId"}]
71
+ response.raw_metric_headers
72
+ => [{"name"=>"sessions", "type"=>"TYPE_INTEGER"}, {"name"=>"screenPageViews", "type"=>"TYPE_INTEGER"}]
73
+ response.rows.first
74
+ => {"dimensionValues"=>[{"value"=>"/events"}, {"value"=>"CL"}], "metricValues"=>[{"value"=>"58"}, {"value"=>"78"}]}
75
+
76
+ # Or get a simplified version of the response
77
+ response.dimension_headers
78
+ => ["pagePath", "countryId"]
79
+ response.metric_headers
80
+ => ["sessions", "screenPageViews"]
81
+ response.parsed_rows.first.data
82
+ => {"pagePath"=>"/events", "countryId"=>"CL", "sessions"=>58, "screenPageViews"=>78}
33
83
 
84
+ ```
85
+ Dimensions and Metrics are available [here](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema)
86
+ Information about Filters is available [here](https://developers.google.com/analytics/devguides/reporting/data/v1/basics#dimension_filters)
34
87
 
35
88
  ## Development
36
89
 
@@ -3,6 +3,8 @@
3
3
  module GoogleAnalyticsV4Api
4
4
  class Client
5
5
 
6
+ attr_accessor :access_token
7
+
6
8
  def initialize(access_token)
7
9
  @access_token = access_token
8
10
  end
@@ -17,6 +17,11 @@ module GoogleAnalyticsV4Api
17
17
  @client.account(parent)
18
18
  end
19
19
 
20
+ def run_report(report)
21
+ response = Request.post(access_token: @client.access_token, path: "/#{name}:runReport", payload: report.to_json)
22
+ ReportResponse.new(response)
23
+ end
24
+
20
25
  def self.parse_list(client, body)
21
26
  JSON.parse(body)["properties"].map do |attrs|
22
27
  GoogleAnalyticsV4Api::Property.new(client, attrs)
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'date'
5
+
6
+ module GoogleAnalyticsV4Api
7
+ class Report
8
+ attr_accessor :date_ranges, :dimensions, :metrics, :dimension_filter
9
+
10
+ def initialize(start_date: nil, end_date: nil, dimensions: [], metrics: [], dimension_filter: {})
11
+ @date_ranges = [{ "startDate": start_date || (Date.today - 30), "endDate": end_date || Date.today }]
12
+ @dimensions = dimensions.map { |dimension| { "name": dimension } }
13
+ @metrics = metrics.map { |metric| { "name": metric } }
14
+ @dimension_filter = dimension_filter
15
+ end
16
+
17
+ def to_json
18
+ {
19
+ "dateRanges": @date_ranges,
20
+ "dimensions": @dimensions,
21
+ "metrics": @metrics,
22
+ "dimensionFilter": @dimension_filter
23
+ }.to_json
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'date'
5
+
6
+ module GoogleAnalyticsV4Api
7
+ class ReportResponse
8
+
9
+ attr_accessor :body, :raw_dimension_headers, :raw_metric_headers, :rows, :row_count, :metadata, :kind
10
+
11
+ def initialize(body)
12
+ @body = JSON.parse body
13
+ @raw_dimension_headers = @body["dimensionHeaders"]
14
+ @raw_metric_headers = @body["metricHeaders"]
15
+ @rows = @body["rows"]
16
+ @row_cont = @body["rowCount"]
17
+ @metadata = @body["metadata"]
18
+ @kind = @body["kind"]
19
+ end
20
+
21
+ def dimension_headers
22
+ @dimension_headers ||= @raw_dimension_headers.map { |header| header["name"] }
23
+ end
24
+
25
+ def metric_headers
26
+ @metric_headers ||= @raw_metric_headers.map { |header| header["name"] }
27
+ end
28
+
29
+ def parsed_rows
30
+ @rows.map do |row|
31
+ ReportResponseRow.new(row, @raw_dimension_headers, @raw_metric_headers)
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'date'
5
+
6
+ module GoogleAnalyticsV4Api
7
+ class ReportResponseRow
8
+
9
+ attr_accessor :data
10
+
11
+ def initialize(row, raw_dimension_headers, raw_metric_headers)
12
+ # "dimensionHeaders"=>[{"name"=>"pagePath"}, {"name"=>"countryId"}],
13
+ # "metricHeaders"=>[{"name"=>"sessions", "type"=>"TYPE_INTEGER"}, {"name"=>"screenPageViews", "type"=>"TYPE_INTEGER"}],
14
+ # a row: {"dimensionValues"=>[{"value"=>"/events"}, {"value"=>"CL"}], "metricValues"=>[{"value"=>"62"}, {"value"=>"82"}]}
15
+ @raw_row = row
16
+ @data = {}
17
+ raw_dimension_headers.each_with_index do |dimension_header, index|
18
+ @data[dimension_header["name"]] = row["dimensionValues"][index]["value"]
19
+ end
20
+ raw_metric_headers.each_with_index do |metric_header, index|
21
+ @data[metric_header["name"]] = cast(row["metricValues"][index]["value"], metric_header["type"])
22
+ end
23
+ end
24
+
25
+ def cast(value, type)
26
+ words = type.split("_")
27
+ return value unless words[0] == 'TYPE'
28
+ return value.to_i if words[1] == "INTEGER"
29
+ return value.to_f if words[1].in? %w(FLOAT SECONDS MILLISECONDS MINUTES HOURS STANDARD CURRENCY FEET MILES METERS KILOMETERS)
30
+
31
+ value
32
+ end
33
+
34
+ end
35
+ end
@@ -7,10 +7,10 @@ module GoogleAnalyticsV4Api
7
7
  class Request
8
8
 
9
9
  ADMIN_URL = "https://analyticsadmin.googleapis.com/v1beta"
10
- DATA_URL = "https://analyticsdata.googleapis.com/v1beta/properties/{{PROPERTY_ID}}:runReport"
10
+ DATA_URL = "https://analyticsdata.googleapis.com/v1beta"
11
11
 
12
- def self.get(access_token:, url: nil, path:, params: {})
13
- url = "#{url || ADMIN_URL}#{path}"
12
+ def self.get(access_token:, url: ADMIN_URL, path:, params: {})
13
+ url = "#{url}#{path}"
14
14
  url += "?#{URI.encode_www_form params}" unless params.empty?
15
15
  uri = URI(url)
16
16
 
@@ -26,8 +26,19 @@ module GoogleAnalyticsV4Api
26
26
  response
27
27
  end
28
28
 
29
- def self.post(access_token:, url: nil, path:, params: {})
29
+ def self.post(access_token:, url: DATA_URL, path:, payload: nil)
30
+ url = URI("#{url}#{path}")
30
31
 
32
+ https = Net::HTTP.new(url.host, url.port)
33
+ https.use_ssl = true
34
+
35
+ request = Net::HTTP::Post.new(url)
36
+ request["Authorization"] = "Bearer #{access_token}"
37
+ request["Content-Type"] = "application/javascript"
38
+ request.body = payload
39
+
40
+ response = https.request(request)
41
+ response.read_body
31
42
  end
32
43
 
33
44
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleAnalyticsV4Api
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -5,5 +5,8 @@ module GoogleAnalyticsV4Api
5
5
  autoload :Account, "google_analytics_v4_api/account"
6
6
  autoload :Property, "google_analytics_v4_api/property"
7
7
  autoload :Request, "google_analytics_v4_api/request"
8
+ autoload :Report, "google_analytics_v4_api/report"
9
+ autoload :ReportResponse, "google_analytics_v4_api/report_response"
10
+ autoload :ReportResponseRow, "google_analytics_v4_api/report_response_row"
8
11
  autoload :Error, "google_analytics_v4_api/error"
9
12
  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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gustavo Garcia
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-07 00:00:00.000000000 Z
11
+ date: 2023-01-09 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,9 @@ 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/report.rb
37
+ - lib/google_analytics_v4_api/report_response.rb
38
+ - lib/google_analytics_v4_api/report_response_row.rb
36
39
  - lib/google_analytics_v4_api/request.rb
37
40
  - lib/google_analytics_v4_api/version.rb
38
41
  homepage: https://github.com/dailytics/google_analytics_v4_api