ga4-rails 0.1.3 → 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: 88422fef26dbe9e169835763cb45563b359efba57d20e0bd02daa9b63f68fc5e
4
- data.tar.gz: 927a597128d23c71243f9f1ca01b177fcf899452468d73d42cb69c66b72a90cf
3
+ metadata.gz: ccebb95c15585bdcb7bbaae021ef88925592dfab178fa2d3aea4c7af630e4b7b
4
+ data.tar.gz: 3f905d8b8e986f6af1d16122c0312449995fbb792963f64e2f545d2a5141f52a
5
5
  SHA512:
6
- metadata.gz: 84af038bc9f60d892334de1784b189ffd58716c2b8977a73dd9d87016497a32b8c0949570ed20e84a0d8709666cf7bb7761848c6748d60772af267d0018ebbad
7
- data.tar.gz: 1f1dba9e79e87d27d03526d1c791af863c71ce88408d4cb8b7acc34218d268587e0569c4b105c45950a4f31f5f85ae5c781a731ed37d67ca77cfd302f62b2e0f
6
+ metadata.gz: 87996522ee691d58d75a51586211e97530c860d4e4bb8ad6c94fcff2ae932e39df39f2ea340b662b4832c7a7fe57768fe2fc9449df74b7e16d6f0adbdaba20c5
7
+ data.tar.gz: 01021a0c232f81948d53c461f49bfc91a17b2153a441b0c30dd228f5bcafde2434241889d9ede8051cc6d88641ec5c430e5e0f431d58b7dc8676f4c552376802
data/.rubocop.yml CHANGED
@@ -491,7 +491,7 @@ Layout/TrailingEmptyLines:
491
491
  Naming/FileName:
492
492
  # File names listed in `AllCops:Include` are excluded by default. Add extra
493
493
  # excludes here.
494
- Exclude: []
494
+ Exclude: ['lib/ga4-rails.rb']
495
495
  # When `true`, requires that each source file should define a class or module
496
496
  # with a name which matches the file name (converted to ... case).
497
497
  # It further expects it to be nested inside modules which match the names
@@ -1429,6 +1429,8 @@ Lint/UnusedMethodArgument:
1429
1429
  AllowUnusedKeywordArguments: false
1430
1430
  IgnoreEmptyMethods: true
1431
1431
 
1432
+ Lint/MissingSuper:
1433
+ Enabled: false
1432
1434
  #################### Performance ###########################
1433
1435
 
1434
1436
  # Performance/DoubleStartEndWith:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2] - 2023-02-01
4
+
5
+ - Add wrapper for Google Analytics Data API
6
+ - Add model to fetch results for better framework integration
7
+
3
8
  ## [0.1.0] - 2022-12-27
4
9
 
5
10
  - Initial release
@@ -7,5 +12,5 @@
7
12
  ## [0.1.1] - 2022-12-29
8
13
  - Implement basic wrapper for Google Analytics Admin API
9
14
 
10
- ## [0.1.3] - 2023-01-03
15
+ ## [0.1.4] - 2023-01-03
11
16
  - Clean up the repository
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ga4-rails (0.1.2)
4
+ ga4-rails (0.1.4)
5
5
  google-apis-analyticsadmin_v1alpha
6
6
  google-apis-analyticsdata_v1beta
7
7
 
@@ -13,15 +13,15 @@ GEM
13
13
  ast (2.4.2)
14
14
  declarative (0.0.20)
15
15
  diff-lcs (1.5.0)
16
- faraday (2.7.2)
16
+ faraday (2.7.4)
17
17
  faraday-net_http (>= 2.0, < 3.1)
18
18
  ruby2_keywords (>= 0.0.4)
19
19
  faraday-net_http (3.0.2)
20
20
  google-apis-analyticsadmin_v1alpha (0.44.0)
21
21
  google-apis-core (>= 0.9.1, < 2.a)
22
- google-apis-analyticsdata_v1beta (0.21.0)
22
+ google-apis-analyticsdata_v1beta (0.22.0)
23
23
  google-apis-core (>= 0.9.1, < 2.a)
24
- google-apis-core (0.9.2)
24
+ google-apis-core (0.9.5)
25
25
  addressable (~> 2.5, >= 2.5.1)
26
26
  googleauth (>= 0.16.2, < 2.a)
27
27
  httpclient (>= 2.8.1, < 3.a)
@@ -45,12 +45,12 @@ GEM
45
45
  multi_json (1.15.0)
46
46
  os (1.1.4)
47
47
  parallel (1.22.1)
48
- parser (3.1.3.0)
48
+ parser (3.2.0.0)
49
49
  ast (~> 2.4.1)
50
50
  public_suffix (5.0.1)
51
51
  rainbow (3.1.1)
52
52
  rake (13.0.6)
53
- regexp_parser (2.6.1)
53
+ regexp_parser (2.6.2)
54
54
  representable (3.2.0)
55
55
  declarative (< 0.1.0)
56
56
  trailblazer-option (>= 0.1.1, < 0.2.0)
@@ -63,23 +63,23 @@ GEM
63
63
  rspec-mocks (~> 3.12.0)
64
64
  rspec-core (3.12.0)
65
65
  rspec-support (~> 3.12.0)
66
- rspec-expectations (3.12.1)
66
+ rspec-expectations (3.12.2)
67
67
  diff-lcs (>= 1.2.0, < 2.0)
68
68
  rspec-support (~> 3.12.0)
69
- rspec-mocks (3.12.1)
69
+ rspec-mocks (3.12.3)
70
70
  diff-lcs (>= 1.2.0, < 2.0)
71
71
  rspec-support (~> 3.12.0)
72
72
  rspec-support (3.12.0)
73
- rubocop (1.41.1)
73
+ rubocop (1.44.0)
74
74
  json (~> 2.3)
75
75
  parallel (~> 1.10)
76
- parser (>= 3.1.2.1)
76
+ parser (>= 3.2.0.0)
77
77
  rainbow (>= 2.2.2, < 4.0)
78
78
  regexp_parser (>= 1.8, < 3.0)
79
79
  rexml (>= 3.2.5, < 4.0)
80
- rubocop-ast (>= 1.23.0, < 2.0)
80
+ rubocop-ast (>= 1.24.1, < 2.0)
81
81
  ruby-progressbar (~> 1.7)
82
- unicode-display_width (>= 1.4.0, < 3.0)
82
+ unicode-display_width (>= 2.4.0, < 3.0)
83
83
  rubocop-ast (1.24.1)
84
84
  parser (>= 3.1.1.0)
85
85
  ruby-progressbar (1.11.0)
@@ -91,12 +91,11 @@ GEM
91
91
  multi_json (~> 1.10)
92
92
  trailblazer-option (0.1.2)
93
93
  uber (0.1.0)
94
- unicode-display_width (2.3.0)
94
+ unicode-display_width (2.4.2)
95
95
  webrick (1.7.0)
96
96
 
97
97
  PLATFORMS
98
98
  arm64-darwin-21
99
- ruby
100
99
 
101
100
  DEPENDENCIES
102
101
  ga4-rails!
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Ga4::Rails
1
+ # GA4 Rails
2
+
3
+ <img src="https://pics.clipartpng.com/midle/Triangle_Under_Construction_Sign_PNG_Clipart-588.png" height=300/>
2
4
 
3
5
  This is a gem to simplify integration of Rails and Google Analytics 4.
4
6
 
@@ -15,7 +17,53 @@ If bundler is not being used to manage dependencies, install the gem by executin
15
17
 
16
18
  ## Usage
17
19
 
18
- TODO: Write usage instructions here
20
+ ### Basic Usage
21
+
22
+ Include Ga4Rails model somewhere in your code the following way:
23
+
24
+ ```ruby
25
+ class UserAnalyticsData
26
+ include Ga4Rails::Model
27
+
28
+ metrics :new_users # Specify metrics
29
+ dimensions :browser, :country # Specify dimensions
30
+ end
31
+ ```
32
+
33
+ Filters support coming soon!
34
+
35
+ To fetch data, run the following code:
36
+
37
+ ```ruby
38
+ results = UserAnalyticsData.results(
39
+ access_token: '<access token obtained from Oauth flow'
40
+ property_id: 'properties/<id>',
41
+ start_date: 1.month.ago
42
+ end_date: Date.today
43
+ )
44
+ ```
45
+
46
+ ### Advanced usage
47
+
48
+ You can call the API methods directly if you need to.
49
+
50
+ 1. Set up the client
51
+
52
+ ```ruby
53
+ require 'ga4-rails/client'
54
+
55
+ client = Ga4Rails::Client.new(access_token: access_token) # Access token from oauth flow
56
+
57
+ ```
58
+
59
+ Then you can call methods on `data` or `admin` API directly:
60
+
61
+ ```ruby
62
+ client.admin.list_property_data_streams
63
+ client.admin.list_account_summaries
64
+
65
+ client.data.run_property_report(property_id, request)
66
+ ```
19
67
 
20
68
  ## Development
21
69
 
@@ -23,6 +71,13 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
23
71
 
24
72
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
25
73
 
74
+ ## Roadmap
75
+
76
+ - [x] Implement wrapper for Google Analytics Admin API
77
+ - [x] Implement wrapper for Google Analytics Data API
78
+ - [x] Implement Rails model sync extenstion from Data API
79
+ - [ ] Implement report filters
80
+
26
81
  ## Contributing
27
82
 
28
83
  Bug reports and pull requests are welcome on GitHub at https://github.com/Dark-Sun/ga4-rails.
@@ -0,0 +1,50 @@
1
+ require 'google/apis/analyticsdata_v1beta'
2
+
3
+ # A class for interacting with the Google Analytics Data API.
4
+ #
5
+ # @attr_reader [OAuth2::AccessToken] access_token -
6
+ # the access token obtained from the Google OAuth2 flow
7
+ #
8
+ # @example
9
+ # Initialize service:
10
+ #
11
+ # service = Ga4Rails::AnalyticsData.new(access_token: access_token).service
12
+ #
13
+ # Get the metadata for a property
14
+ # service.get_property_metadata("properties/348443235/metadata")
15
+ #
16
+ # Run a report for a property
17
+ # body = {
18
+ # "date_ranges": [
19
+ # {
20
+ # "start_date": "30daysAgo",
21
+ # "end_date": "today"
22
+ # }
23
+ # ],
24
+ # "metrics": [
25
+ # { name: "totalUsers" }
26
+ # ]
27
+ # }
28
+ #
29
+ # request = Google::Apis::AnalyticsdataV1beta::RunReportRequest.new(body)
30
+ # service.run_property_report("properties/341194148", request)
31
+
32
+ module Ga4Rails
33
+ class AnalyticsData
34
+ AnalyticsData = Google::Apis::AnalyticsdataV1beta
35
+
36
+ attr_accessor :access_token
37
+
38
+ def initialize(access_token:)
39
+ @access_token = access_token
40
+ end
41
+
42
+ def service
43
+ return @service if @service
44
+
45
+ @service = AnalyticsData::AnalyticsDataService.new
46
+ @service.authorization = access_token
47
+ @service
48
+ end
49
+ end
50
+ end
@@ -1,4 +1,5 @@
1
- require_relative 'analytics_admin'
1
+ require_relative 'api/analytics_admin'
2
+ require_relative 'api/analytics_data'
2
3
 
3
4
  # A client for interacting with the Google Analytics API.
4
5
  #
@@ -27,6 +28,71 @@ class Ga4Rails::Client
27
28
  analytics_admin_service
28
29
  end
29
30
 
31
+ def data
32
+ analytics_data_service
33
+ end
34
+
35
+ # Returns an array of available metrics for a property.
36
+ #
37
+ # @param property [String] -
38
+ # the property ID
39
+ # Example: 'properties/341194148'
40
+ #
41
+ # @return [Array] -
42
+ # an array of available metrics for a property
43
+ def available_metrics(property:)
44
+ Ga4Rails::GetPropertyMetadata.call(
45
+ data_service: analytics_data_service,
46
+ property_id: property
47
+ ).metrics
48
+ end
49
+
50
+ # Returns an array of available dimensions for a property.
51
+ #
52
+ # @param property [String] -
53
+ # the property ID
54
+ # Example: 'properties/341194148'
55
+ #
56
+ # @return [Array] -
57
+ # an array of available dimensions for a property
58
+ def available_dimensions(property:)
59
+ Ga4Rails::GetPropertyMetadata.call(
60
+ data_service: analytics_data_service,
61
+ property_id: property
62
+ ).dimensions
63
+ end
64
+
65
+ # Returns property report for a given property
66
+ # https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport
67
+ #
68
+ # @param property_id [String] -
69
+ # the property ID. Example: 'properties/341194148'
70
+ #
71
+ # @param body [Hash] -
72
+ # the report request body. Example:
73
+ # body = {
74
+ # "date_ranges": [
75
+ # {
76
+ # "start_date": "30daysAgo",
77
+ # "end_date": "today"
78
+ # }
79
+ # ],
80
+ # "metrics": [
81
+ # { name: "totalUsers" }
82
+ # ]
83
+ # }
84
+ def run_property_report(property_id:, body:)
85
+ Ga4Rails::RunPropertyReport.call(
86
+ data_service: analytics_data_service,
87
+ property_id: property_id,
88
+ body: body
89
+ )
90
+ end
91
+
92
+ # TODO
93
+ def profiles
94
+ end
95
+
30
96
  private
31
97
 
32
98
  def analytics_admin_service
@@ -34,4 +100,10 @@ class Ga4Rails::Client
34
100
  access_token: access_token
35
101
  ).service
36
102
  end
37
- end
103
+
104
+ def analytics_data_service
105
+ @analytics_data_service ||= Ga4Rails::AnalyticsData.new(
106
+ access_token: access_token
107
+ ).service
108
+ end
109
+ end
@@ -0,0 +1,19 @@
1
+ module Camelize
2
+ refine String do
3
+ def camelize(first_letter = :upper)
4
+ if first_letter == :upper
5
+ split('_').map(&:capitalize).join
6
+ else
7
+ split('_').map.with_index do |word, index|
8
+ index.zero? ? word : word.capitalize
9
+ end.join
10
+ end
11
+ end
12
+ end
13
+
14
+ refine Symbol do
15
+ def camelize(first_letter = :upper)
16
+ to_s.camelize(first_letter).to_sym
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,57 @@
1
+ module Ga4Rails::Model
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ using Camelize
8
+
9
+ DATE_FORMAT = '%Y-%m-%d'.freeze
10
+
11
+ def metrics(*metrics)
12
+ @metrics ||= metrics
13
+ end
14
+
15
+ def dimensions(*dimensions)
16
+ @dimensions ||= dimensions
17
+ end
18
+
19
+ def results(access_token:, property_id:, start_date:, end_date:, limit: 1000)
20
+ client = Ga4Rails::Client.new(access_token: access_token)
21
+
22
+ # TODO: Handle response
23
+ response = client.run_property_report(
24
+ property_id: property_id,
25
+ body: build_body(start_date: start_date, end_date: end_date, limit: limit)
26
+ ).response
27
+
28
+ beautify_response(response)
29
+ end
30
+
31
+ private
32
+
33
+ def build_body(start_date:, end_date:, limit:)
34
+ body = {}
35
+ body[:date_ranges] = [
36
+ {
37
+ start_date: start_date.strftime(DATE_FORMAT),
38
+ end_date: end_date.strftime(DATE_FORMAT)
39
+ }
40
+ ]
41
+ body[:metrics] = @metrics.map { |metric| { name: metric.camelize(:lower) } }
42
+ body[:dimensions] = @dimensions.map { |dimension| { name: dimension.camelize(:lower) } }
43
+ body[:limit] = limit
44
+
45
+ body
46
+ end
47
+
48
+ def beautify_response(response)
49
+ response.rows.map do |row|
50
+ {
51
+ dimensions: row.dimension_values.map(&:value).join(', '),
52
+ metrics: row.metric_values.map(&:value).join(', ')
53
+ }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,40 @@
1
+ # A class for running GA Data Api property report.
2
+ #
3
+ # @attr_reader [Ga4Rails::AnalyticsData] data_service -
4
+ # the data service wraper to run the report
5
+ #
6
+ # @attr_reader [String] property_id -
7
+ # the GA4 roperty ID, example: 'properties/341194148'
8
+ #
9
+ # Usage:
10
+ # metrics = Ga4Rails::GetPropertyMetadata.new(
11
+ # data_service: data_service,
12
+ # property_id: property_id
13
+ # ).metrics
14
+ # dimensions = Ga4Rails::GetPropertyMetadata.new(
15
+ # data_service: data_service,
16
+ # property_id: property_id
17
+ # ).dimensions
18
+
19
+ class Ga4Rails::GetPropertyMetadata < Ga4Rails::Request
20
+ attr_accessor :data_service, :property_id, :response
21
+
22
+ def initialize(data_service:, property_id:)
23
+ @data_service = data_service
24
+ @property_id = property_id
25
+ end
26
+
27
+ def metrics
28
+ @metrics ||= JSON.parse(response.metrics.to_json)
29
+ end
30
+
31
+ def dimensions
32
+ @dimensions ||= JSON.parse(response.dimensions.to_json)
33
+ end
34
+
35
+ def call
36
+ @response = data_service.get_property_metadata("#{property_id}/metadata")
37
+
38
+ self
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ class Ga4Rails::Request
2
+ def self.call(*args)
3
+ new(*args).call
4
+ end
5
+
6
+ def call
7
+ raise NotImplementedError
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ # A class for running GA Data Api property report.
2
+ #
3
+ # @attr_reader [Ga4Rails::AnalyticsData] data_service -
4
+ # the data service wraper to run the report
5
+ #
6
+ # @attr_reader [String] property_id -
7
+ # the GA4 roperty ID, example: 'properties/341194148'
8
+ #
9
+ # @attr_reader [String] body -
10
+ # the report request body, example:
11
+ # body = {
12
+ # "date_ranges": [
13
+ # {
14
+ # "start_date": "30daysAgo",
15
+ # "end_date": "today"
16
+ # }
17
+ # ],
18
+ # "metrics": [
19
+ # { name: "totalUsers" }
20
+ # ]
21
+ # }
22
+ #
23
+ # Usage:
24
+ # response = Ga4Rails::RunPropertyReport.new(
25
+ # data_service: data_service,
26
+ # property_id: property_id,
27
+ # body: body
28
+ # ).response
29
+
30
+ class Ga4Rails::RunPropertyReport < Ga4Rails::Request
31
+ attr_accessor :data_service, :property_id, :body, :response
32
+
33
+ def initialize(data_service:, property_id:, body:)
34
+ @data_service = data_service
35
+ @property_id = property_id
36
+ @body = body
37
+ end
38
+
39
+ def call
40
+ request = Google::Apis::AnalyticsdataV1beta::RunReportRequest.new(body)
41
+ @response = data_service.run_property_report(property_id, request)
42
+
43
+ self
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module Ga4Rails
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.2'.freeze
3
3
  end
data/lib/ga4-rails.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'ga4-rails/extensions/camelize'
2
+ require 'ga4-rails/client'
3
+ require 'ga4-rails/version'
4
+ require 'ga4-rails/api/analytics_admin'
5
+ require 'ga4-rails/api/analytics_data'
6
+ require 'ga4-rails/requests/request'
7
+ require 'ga4-rails/requests/get_property_metadata'
8
+ require 'ga4-rails/requests/run_property_report'
9
+ require 'ga4-rails/model'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ga4-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andriy Lukashchuk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-03 00:00:00.000000000 Z
11
+ date: 2023-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-apis-analyticsadmin_v1alpha
@@ -67,8 +67,15 @@ files:
67
67
  - Gemfile.lock
68
68
  - README.md
69
69
  - Rakefile
70
- - lib/ga4-rails/analytics_admin.rb
70
+ - lib/ga4-rails.rb
71
+ - lib/ga4-rails/api/analytics_admin.rb
72
+ - lib/ga4-rails/api/analytics_data.rb
71
73
  - lib/ga4-rails/client.rb
74
+ - lib/ga4-rails/extensions/camelize.rb
75
+ - lib/ga4-rails/model.rb
76
+ - lib/ga4-rails/requests/get_property_metadata.rb
77
+ - lib/ga4-rails/requests/request.rb
78
+ - lib/ga4-rails/requests/run_property_report.rb
72
79
  - lib/ga4-rails/version.rb
73
80
  - sig/ga4/rails.rbs
74
81
  homepage: http://github.com/Dark-Sun/ga4-rails
@@ -93,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
100
  - !ruby/object:Gem::Version
94
101
  version: '0'
95
102
  requirements: []
96
- rubygems_version: 3.1.6
103
+ rubygems_version: 3.4.5
97
104
  signing_key:
98
105
  specification_version: 4
99
106
  summary: Efortlessly integrate Google Analytics 4 with your Rails app