ga4-events 1.0.1
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop/rspec.yml +46 -0
- data/.rubocop.yml +44 -0
- data/.rubocop_todo.yml +8 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +18 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +363 -0
- data/Rakefile +15 -0
- data/lib/ga4/events/client.rb +171 -0
- data/lib/ga4/events/configuration.rb +41 -0
- data/lib/ga4/events/event.rb +77 -0
- data/lib/ga4/events/response.rb +44 -0
- data/lib/ga4/events/version.rb +7 -0
- data/lib/ga4/events.rb +44 -0
- data/sig/ga4/events.rbs +6 -0
- metadata +308 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8e265b24049bf4bbd6f6498eef77be0fb984c2aaeea656ef3b0da835501ee7f2
|
|
4
|
+
data.tar.gz: 12286e02db2c55ea9c21eb11300983e62d96fdd69c78bb05bbb6c05319c8c0ee
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 719b7d1ffde817b1996321e4a354cffd02571ca2753cbcc079047fee419184eb4c0d2243293975d5b3bfbcc45bb3742686c2c2b0649e71ffb66935119dab065a
|
|
7
|
+
data.tar.gz: 788498e51afde0255c033ec6b4b348e9a3189169bc8757b5ccbcc1845772db04a32df3eac2711fa6ac66b8bf2ada2f25ee50cadf6d681f2d59e053058dac18e7
|
data/.rspec
ADDED
data/.rubocop/rspec.yml
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require:
|
|
2
|
+
- rubocop-rspec
|
|
3
|
+
|
|
4
|
+
# Disable all cops by default,
|
|
5
|
+
# only enable those defined explcitly in this configuration file
|
|
6
|
+
RSpec:
|
|
7
|
+
Enabled: false
|
|
8
|
+
|
|
9
|
+
RSpec/Focus:
|
|
10
|
+
Enabled: true
|
|
11
|
+
|
|
12
|
+
RSpec/EmptyExampleGroup:
|
|
13
|
+
Enabled: true
|
|
14
|
+
|
|
15
|
+
RSpec/EmptyLineAfterExampleGroup:
|
|
16
|
+
Enabled: true
|
|
17
|
+
|
|
18
|
+
RSpec/EmptyLineAfterFinalLet:
|
|
19
|
+
Enabled: true
|
|
20
|
+
|
|
21
|
+
RSpec/EmptyLineAfterHook:
|
|
22
|
+
Enabled: true
|
|
23
|
+
|
|
24
|
+
RSpec/EmptyLineAfterSubject:
|
|
25
|
+
Enabled: true
|
|
26
|
+
|
|
27
|
+
RSpec/HookArgument:
|
|
28
|
+
Enabled: true
|
|
29
|
+
|
|
30
|
+
RSpec/HooksBeforeExamples:
|
|
31
|
+
Enabled: true
|
|
32
|
+
|
|
33
|
+
RSpec/ImplicitExpect:
|
|
34
|
+
Enabled: true
|
|
35
|
+
|
|
36
|
+
RSpec/IteratedExpectation:
|
|
37
|
+
Enabled: true
|
|
38
|
+
|
|
39
|
+
RSpec/LetBeforeExamples:
|
|
40
|
+
Enabled: true
|
|
41
|
+
|
|
42
|
+
RSpec/MissingExampleGroupArgument:
|
|
43
|
+
Enabled: true
|
|
44
|
+
|
|
45
|
+
RSpec/ReceiveCounts:
|
|
46
|
+
Enabled: true
|
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# We want Exclude directives from different
|
|
2
|
+
# config files to get merged, not overwritten
|
|
3
|
+
inherit_mode:
|
|
4
|
+
merge:
|
|
5
|
+
- Exclude
|
|
6
|
+
|
|
7
|
+
require:
|
|
8
|
+
# Standard's config uses custom cops,
|
|
9
|
+
# so it must be loaded along with custom Standard gems
|
|
10
|
+
- standard
|
|
11
|
+
- standard-custom
|
|
12
|
+
- standard-performance
|
|
13
|
+
# rubocop-performance is required when using Performance cops
|
|
14
|
+
- rubocop-performance
|
|
15
|
+
- rubocop-rake
|
|
16
|
+
|
|
17
|
+
inherit_gem:
|
|
18
|
+
standard: config/base.yml
|
|
19
|
+
# You can also choose a Ruby-version-specific config
|
|
20
|
+
# standard: config/ruby-3.0.yml
|
|
21
|
+
# Standard plugins must be loaded separately (since v1.28.0)
|
|
22
|
+
standard-performance: config/base.yml
|
|
23
|
+
standard-custom: config/base.yml
|
|
24
|
+
|
|
25
|
+
inherit_from:
|
|
26
|
+
- .rubocop/rspec.yml
|
|
27
|
+
|
|
28
|
+
AllCops:
|
|
29
|
+
TargetRubyVersion: 3.3
|
|
30
|
+
|
|
31
|
+
Style/StringLiterals:
|
|
32
|
+
Enabled: false
|
|
33
|
+
|
|
34
|
+
Layout/SpaceInsideHashLiteralBraces:
|
|
35
|
+
EnforcedStyle: space
|
|
36
|
+
|
|
37
|
+
Style/StringLiteralsInInterpolation:
|
|
38
|
+
EnforcedStyle: single_quotes
|
|
39
|
+
|
|
40
|
+
Layout/EmptyLineBetweenDefs:
|
|
41
|
+
AllowAdjacentOneLineDefs: true
|
|
42
|
+
|
|
43
|
+
Layout/ArgumentAlignment:
|
|
44
|
+
EnforcedStyle: with_first_argument
|
data/.rubocop_todo.yml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# This configuration was generated by
|
|
2
|
+
# `rubocop --auto-gen-config`
|
|
3
|
+
# on 2025-11-30 10:52:01 UTC using RuboCop version 1.62.1.
|
|
4
|
+
# The point is for the user to remove these configuration records
|
|
5
|
+
# one by one as the offenses are removed from the code base.
|
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
|
8
|
+
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
### [0.1.0] - Initial release!
|
|
4
|
+
|
|
5
|
+
- Framework-agnostic design - works with any Ruby application
|
|
6
|
+
- `GA4::Events::Client` class for sending events to GA4
|
|
7
|
+
- `GA4::Events::Event` class with built-in validation
|
|
8
|
+
- `GA4::Events::Response` class for handling API responses
|
|
9
|
+
- Batch event sending support (send multiple events in one request)
|
|
10
|
+
- Event validation according to GA4 rules (configurable)
|
|
11
|
+
- Automatic retry logic with configurable attempts and delays
|
|
12
|
+
- Debug mode using GA4 debug endpoint for testing
|
|
13
|
+
- Configurable logging with custom logger support
|
|
14
|
+
- Silent failure mode (fail_silently option)
|
|
15
|
+
- Timeout configuration for HTTP requests
|
|
16
|
+
- Convenience methods: `GA4::Events.track` and `track_batch`
|
|
17
|
+
- Comprehensive RSpec test suite with WebMock
|
|
18
|
+
- Detailed README with usage examples and best practices
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
|
8
|
+
|
|
9
|
+
## Our Standards
|
|
10
|
+
|
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
|
12
|
+
|
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
|
18
|
+
|
|
19
|
+
Examples of unacceptable behavior include:
|
|
20
|
+
|
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
22
|
+
advances of any kind
|
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
24
|
+
* Public or private harassment
|
|
25
|
+
* Publishing others' private information, such as a physical or email
|
|
26
|
+
address, without their explicit permission
|
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
28
|
+
professional setting
|
|
29
|
+
|
|
30
|
+
## Enforcement Responsibilities
|
|
31
|
+
|
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
|
33
|
+
|
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
35
|
+
|
|
36
|
+
## Scope
|
|
37
|
+
|
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
39
|
+
|
|
40
|
+
## Enforcement
|
|
41
|
+
|
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at vanuha277@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
|
43
|
+
|
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
|
45
|
+
|
|
46
|
+
## Enforcement Guidelines
|
|
47
|
+
|
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
|
49
|
+
|
|
50
|
+
### 1. Correction
|
|
51
|
+
|
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
|
53
|
+
|
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
|
55
|
+
|
|
56
|
+
### 2. Warning
|
|
57
|
+
|
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
|
59
|
+
|
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
61
|
+
|
|
62
|
+
### 3. Temporary Ban
|
|
63
|
+
|
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
|
65
|
+
|
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
67
|
+
|
|
68
|
+
### 4. Permanent Ban
|
|
69
|
+
|
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
71
|
+
|
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
|
73
|
+
|
|
74
|
+
## Attribution
|
|
75
|
+
|
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
78
|
+
|
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
|
80
|
+
|
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
|
82
|
+
|
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 loqimean
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
# GA4::Events
|
|
2
|
+
|
|
3
|
+
A framework-agnostic Ruby implementation of Google Analytics 4 (GA4) Measurement Protocol for sending events to GA4. This gem provides a simple, reliable way to track events with features like batch sending, validation, retry logic, and debug mode.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🚀 **Simple API** - Easy to use with sensible defaults
|
|
8
|
+
- 📦 **Batch Events** - Send multiple events in a single request
|
|
9
|
+
- ✅ **Validation** - Validate events before sending (optional)
|
|
10
|
+
- 🔄 **Retry Logic** - Automatic retry with configurable attempts
|
|
11
|
+
- 🐛 **Debug Mode** - Test events without affecting production data
|
|
12
|
+
- 📝 **Configurable Logging** - Built-in logger with customization options
|
|
13
|
+
- 🛡️ **Silent Failures** - Fail gracefully without breaking your application
|
|
14
|
+
- 🔌 **Zero Dependencies** - Uses only Ruby standard library
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
bundle add ga4-events
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
gem install ga4-events
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Configuration
|
|
31
|
+
|
|
32
|
+
Configure the gem in an initializer (e.g., `config/initializers/ga4.rb` for Rails):
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
require "ga4/events"
|
|
36
|
+
|
|
37
|
+
GA4::Events.configure do |config|
|
|
38
|
+
# Required: Your GA4 Measurement ID and API Secret
|
|
39
|
+
config.measurement_id = "G-XXXXXXXXXX" # or ENV["GA4_MEASUREMENT_ID"]
|
|
40
|
+
config.api_secret = "your_api_secret" # or ENV["GA4_API_SECRET"]
|
|
41
|
+
|
|
42
|
+
# Optional: Configuration options with defaults shown
|
|
43
|
+
config.validation_mode = false # Use validation endpoint for server-side validation
|
|
44
|
+
config.debug_mode = false # Inject debug_mode param for DebugView
|
|
45
|
+
config.validate_events = true # Validate events before sending
|
|
46
|
+
config.max_retries = 3 # Number of retry attempts
|
|
47
|
+
config.retry_delay = 1 # Seconds between retries
|
|
48
|
+
config.timeout = 10 # HTTP timeout in seconds
|
|
49
|
+
config.fail_silently = true # Don't raise exceptions on errors
|
|
50
|
+
|
|
51
|
+
# Optional: Custom logger (defaults to Logger.new($stdout))
|
|
52
|
+
config.logger = Rails.logger # Use Rails logger
|
|
53
|
+
config.logger.level = Logger::INFO # Set log level
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Environment Variables
|
|
58
|
+
|
|
59
|
+
You can use environment variables for configuration:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
export GA4_MEASUREMENT_ID="G-XXXXXXXXXX"
|
|
63
|
+
export GA4_API_SECRET="your_api_secret"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
### Sending a Single Event
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
# Simple event
|
|
72
|
+
GA4::Events.track("page_view")
|
|
73
|
+
|
|
74
|
+
# Event with parameters
|
|
75
|
+
GA4::Events.track("purchase", {
|
|
76
|
+
transaction_id: "T12345",
|
|
77
|
+
value: 99.99,
|
|
78
|
+
currency: "USD",
|
|
79
|
+
items: [{
|
|
80
|
+
item_id: "SKU123",
|
|
81
|
+
item_name: "Product Name"
|
|
82
|
+
}]
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
# Event with client_id and user_id
|
|
86
|
+
GA4::Events.track("login", {
|
|
87
|
+
method: "google"
|
|
88
|
+
}, client_id: "custom_client_id", user_id: "user123")
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Sending Batch Events
|
|
92
|
+
|
|
93
|
+
Send multiple events in a single request (more efficient):
|
|
94
|
+
|
|
95
|
+
```ruby
|
|
96
|
+
events = [
|
|
97
|
+
{ name: "page_view", params: { page_title: "Home" } },
|
|
98
|
+
{ name: "scroll", params: { percent_scrolled: 90 } },
|
|
99
|
+
{ name: "button_click", params: { button_name: "signup" } }
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
GA4::Events.track_batch(events, client_id: "client123")
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Using the Client Class
|
|
106
|
+
|
|
107
|
+
For more control, use the Client class directly:
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
client = GA4::Events::Client.new
|
|
111
|
+
|
|
112
|
+
# Send single event
|
|
113
|
+
response = client.send_event("add_to_cart", {
|
|
114
|
+
item_id: "SKU123",
|
|
115
|
+
quantity: 2
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
# Send batch
|
|
119
|
+
response = client.send_batch([
|
|
120
|
+
GA4::Events::Event.new("view_item", { item_id: "SKU123" }),
|
|
121
|
+
GA4::Events::Event.new("add_to_cart", { item_id: "SKU123" })
|
|
122
|
+
])
|
|
123
|
+
|
|
124
|
+
# Check response
|
|
125
|
+
if response.success?
|
|
126
|
+
puts "Events sent successfully!"
|
|
127
|
+
else
|
|
128
|
+
puts "Failed: #{response.errors.join(', ')}"
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Validation Mode
|
|
133
|
+
|
|
134
|
+
Use the validation endpoint to get server-side validation feedback from Google Analytics:
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
GA4::Events.configure do |config|
|
|
138
|
+
config.validation_mode = true # Uses GA4 validation endpoint
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
response = GA4::Events.track("test_event", { test_param: "value" })
|
|
142
|
+
|
|
143
|
+
# Validation mode returns validation messages
|
|
144
|
+
puts response.validation_messages
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Validation Mode
|
|
148
|
+
|
|
149
|
+
Use the validation endpoint to get server-side validation feedback from Google Analytics:
|
|
150
|
+
|
|
151
|
+
```ruby
|
|
152
|
+
GA4::Events.configure do |config|
|
|
153
|
+
config.validation_mode = true # Uses GA4 validation endpoint
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
response = GA4::Events.track("test_event", { test_param: "value" })
|
|
157
|
+
|
|
158
|
+
# Validation mode returns validation messages
|
|
159
|
+
puts response.validation_messages
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Debug Mode
|
|
163
|
+
|
|
164
|
+
Send events to DebugView for real-time debugging in the GA4 interface:
|
|
165
|
+
|
|
166
|
+
```ruby
|
|
167
|
+
GA4::Events.configure do |config|
|
|
168
|
+
config.debug_mode = true # Injects debug_mode=1 parameter
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
response = GA4::Events.track("test_event", { test_param: "value" })
|
|
172
|
+
|
|
173
|
+
# Events will appear in DebugView (note: may take 3-20 minutes)
|
|
174
|
+
# View at: Admin > Data display > DebugView
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**Note:** You may need to wait 3-5 or even 20 minutes to see events in DebugView.
|
|
178
|
+
|
|
179
|
+
### Event Validation
|
|
180
|
+
|
|
181
|
+
The gem validates events according to GA4 rules:
|
|
182
|
+
|
|
183
|
+
- Event names: max 40 characters
|
|
184
|
+
- Parameters: max 25 per event
|
|
185
|
+
- Parameter names: max 40 characters
|
|
186
|
+
- Parameter string values: max 100 characters
|
|
187
|
+
|
|
188
|
+
```ruby
|
|
189
|
+
# Valid event
|
|
190
|
+
event = GA4::Events::Event.new("purchase", { transaction_id: "T123" })
|
|
191
|
+
event.valid? # => true
|
|
192
|
+
|
|
193
|
+
# Invalid event (name too long)
|
|
194
|
+
event = GA4::Events::Event.new("a" * 50, {})
|
|
195
|
+
event.valid? # => false
|
|
196
|
+
event.errors # => ["Event name cannot exceed 40 characters"]
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Disable validation if needed:
|
|
200
|
+
|
|
201
|
+
```ruby
|
|
202
|
+
GA4::Events.configure do |config|
|
|
203
|
+
config.validate_events = false
|
|
204
|
+
end
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Custom Logger
|
|
208
|
+
|
|
209
|
+
Use your own logger:
|
|
210
|
+
|
|
211
|
+
```ruby
|
|
212
|
+
require "logger"
|
|
213
|
+
|
|
214
|
+
custom_logger = Logger.new("log/ga4.log")
|
|
215
|
+
custom_logger.level = Logger::DEBUG
|
|
216
|
+
|
|
217
|
+
GA4::Events.configure do |config|
|
|
218
|
+
config.logger = custom_logger
|
|
219
|
+
end
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Or disable logging:
|
|
223
|
+
|
|
224
|
+
```ruby
|
|
225
|
+
GA4::Events.configure do |config|
|
|
226
|
+
config.logger = nil
|
|
227
|
+
end
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Error Handling
|
|
231
|
+
|
|
232
|
+
By default, errors are handled silently and logged:
|
|
233
|
+
|
|
234
|
+
```ruby
|
|
235
|
+
# With fail_silently = true (default)
|
|
236
|
+
response = GA4::Events.track("test")
|
|
237
|
+
response.success? # => false
|
|
238
|
+
response.errors # => ["Error details"]
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Raise exceptions on errors:
|
|
242
|
+
|
|
243
|
+
```ruby
|
|
244
|
+
GA4::Events.configure do |config|
|
|
245
|
+
config.fail_silently = false
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
begin
|
|
249
|
+
GA4::Events.track("test")
|
|
250
|
+
rescue GA4::Events::Error => e
|
|
251
|
+
puts "Error: #{e.message}"
|
|
252
|
+
end
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Retry Logic
|
|
256
|
+
|
|
257
|
+
Failed requests are automatically retried:
|
|
258
|
+
|
|
259
|
+
```ruby
|
|
260
|
+
GA4::Events.configure do |config|
|
|
261
|
+
config.max_retries = 5 # Try up to 5 times
|
|
262
|
+
config.retry_delay = 2 # Wait 2 seconds between retries
|
|
263
|
+
end
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Advanced Usage
|
|
267
|
+
|
|
268
|
+
### Client ID Generation
|
|
269
|
+
|
|
270
|
+
The gem automatically generates a UUID for `client_id` if not provided. For consistent user tracking, provide your own:
|
|
271
|
+
|
|
272
|
+
```ruby
|
|
273
|
+
# Use a hashed user identifier
|
|
274
|
+
require "digest"
|
|
275
|
+
|
|
276
|
+
client_id = Digest::SHA256.hexdigest("user_email@example.com")
|
|
277
|
+
GA4::Events.track("login", {}, client_id: client_id)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### Per-Request Configuration
|
|
281
|
+
|
|
282
|
+
Create multiple clients with different configurations:
|
|
283
|
+
|
|
284
|
+
```ruby
|
|
285
|
+
# Production config
|
|
286
|
+
prod_config = GA4::Events::Configuration.new
|
|
287
|
+
prod_config.measurement_id = "G-PROD"
|
|
288
|
+
prod_config.api_secret = "prod_secret"
|
|
289
|
+
|
|
290
|
+
# Test config
|
|
291
|
+
test_config = GA4::Events::Configuration.new
|
|
292
|
+
test_config.measurement_id = "G-TEST"
|
|
293
|
+
test_config.api_secret = "test_secret"
|
|
294
|
+
test_config.debug_mode = true
|
|
295
|
+
|
|
296
|
+
# Use different configs
|
|
297
|
+
prod_client = GA4::Events::Client.new(prod_config)
|
|
298
|
+
test_client = GA4::Events::Client.new(test_config)
|
|
299
|
+
|
|
300
|
+
prod_client.send_event("purchase", { value: 99.99 })
|
|
301
|
+
test_client.send_event("test_event", { test: true })
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## Common Event Types
|
|
305
|
+
|
|
306
|
+
### E-commerce Events
|
|
307
|
+
|
|
308
|
+
```ruby
|
|
309
|
+
# Purchase
|
|
310
|
+
GA4::Events.track("purchase", {
|
|
311
|
+
transaction_id: "T123",
|
|
312
|
+
value: 99.99,
|
|
313
|
+
currency: "USD",
|
|
314
|
+
tax: 8.00,
|
|
315
|
+
shipping: 5.00
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
# Add to cart
|
|
319
|
+
GA4::Events.track("add_to_cart", {
|
|
320
|
+
currency: "USD",
|
|
321
|
+
value: 49.99,
|
|
322
|
+
items: [{
|
|
323
|
+
item_id: "SKU123",
|
|
324
|
+
item_name: "Product Name",
|
|
325
|
+
price: 49.99,
|
|
326
|
+
quantity: 1
|
|
327
|
+
}]
|
|
328
|
+
})
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### User Engagement
|
|
332
|
+
|
|
333
|
+
```ruby
|
|
334
|
+
# Page view
|
|
335
|
+
GA4::Events.track("page_view", {
|
|
336
|
+
page_title: "Home",
|
|
337
|
+
page_location: "https://example.com/",
|
|
338
|
+
page_path: "/"
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
# User engagement
|
|
342
|
+
GA4::Events.track("user_engagement", {
|
|
343
|
+
engagement_time_msec: 5000
|
|
344
|
+
})
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
## Development
|
|
348
|
+
|
|
349
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
350
|
+
|
|
351
|
+
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).
|
|
352
|
+
|
|
353
|
+
## Contributing
|
|
354
|
+
|
|
355
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/the-rubies-way/ga4-events. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/the-rubies-way/ga4-events/blob/main/CODE_OF_CONDUCT.md).
|
|
356
|
+
|
|
357
|
+
## License
|
|
358
|
+
|
|
359
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
360
|
+
|
|
361
|
+
## Code of Conduct
|
|
362
|
+
|
|
363
|
+
Everyone interacting in the GA4::Events project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/the-rubies-way/ga4-events/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rspec/core/rake_task"
|
|
5
|
+
|
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
+
|
|
8
|
+
require "rubocop/rake_task"
|
|
9
|
+
|
|
10
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
|
11
|
+
task.patterns = ["lib/**/*.rb", "spec/**/*.rb"]
|
|
12
|
+
task.fail_on_error = true
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
task default: [:spec, :rubocop]
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class GA4::Events::Client
|
|
4
|
+
attr_reader :config
|
|
5
|
+
|
|
6
|
+
def initialize(config = nil)
|
|
7
|
+
@config = config || GA4::Events.configuration
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Send a single event
|
|
11
|
+
def send_event(name, params = {}, client_id: nil, user_id: nil)
|
|
12
|
+
event = GA4::Events::Event.new(name, params)
|
|
13
|
+
|
|
14
|
+
send_batch([event], client_id: client_id, user_id: user_id)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Send multiple events in a batch
|
|
18
|
+
def send_batch(events, client_id: nil, user_id: nil)
|
|
19
|
+
config.validate!
|
|
20
|
+
|
|
21
|
+
# Convert events to Event objects if needed
|
|
22
|
+
event_objects = events.map do |event|
|
|
23
|
+
event.is_a?(GA4::Events::Event) ? event : GA4::Events::Event.new(event[:name], event[:params] || {})
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Validate events if enabled
|
|
27
|
+
if config.validate_events
|
|
28
|
+
invalid_events = event_objects.reject(&:valid?)
|
|
29
|
+
|
|
30
|
+
unless invalid_events.empty?
|
|
31
|
+
error_messages = invalid_events.flat_map(&:errors)
|
|
32
|
+
|
|
33
|
+
handle_error("Event validation failed: #{error_messages.join(', ')}")
|
|
34
|
+
|
|
35
|
+
return GA4::Events::Response.new(400, {}, error_messages)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
client_id ||= generate_client_id
|
|
40
|
+
payload = build_payload(event_objects, client_id, user_id)
|
|
41
|
+
|
|
42
|
+
# Send with retry logic
|
|
43
|
+
send_with_retry(payload)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def build_payload(events, client_id, user_id)
|
|
49
|
+
payload = {
|
|
50
|
+
client_id: client_id,
|
|
51
|
+
events: events.map { |event| event.to_h(debug_mode: config.debug_mode) }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
payload[:user_id] = user_id if user_id
|
|
55
|
+
|
|
56
|
+
payload
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def send_with_retry(payload)
|
|
60
|
+
attempts = 0
|
|
61
|
+
last_error = nil
|
|
62
|
+
|
|
63
|
+
loop do
|
|
64
|
+
attempts += 1
|
|
65
|
+
|
|
66
|
+
begin
|
|
67
|
+
response = perform_request(payload)
|
|
68
|
+
|
|
69
|
+
log_response(response, payload)
|
|
70
|
+
|
|
71
|
+
return response
|
|
72
|
+
rescue => e
|
|
73
|
+
last_error = e
|
|
74
|
+
|
|
75
|
+
log_error("Attempt #{attempts} failed: #{e.message}")
|
|
76
|
+
|
|
77
|
+
if attempts >= config.max_retries
|
|
78
|
+
handle_error("Max retries (#{config.max_retries}) exceeded: #{e.message}")
|
|
79
|
+
|
|
80
|
+
return GA4::Events::Response.new(0, {}, [e.message])
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
sleep(config.retry_delay)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def perform_request(payload)
|
|
89
|
+
uri = build_uri
|
|
90
|
+
http = create_http_client(uri)
|
|
91
|
+
request = create_request(uri, payload)
|
|
92
|
+
|
|
93
|
+
http_response = http.request(request)
|
|
94
|
+
|
|
95
|
+
body = parse_response_body(http_response.body)
|
|
96
|
+
errors = (http_response.code.to_i >= 400) ? [http_response.message] : []
|
|
97
|
+
|
|
98
|
+
GA4::Events::Response.new(http_response.code.to_i, body, errors)
|
|
99
|
+
rescue => e
|
|
100
|
+
raise e unless config.fail_silently
|
|
101
|
+
|
|
102
|
+
GA4::Events::Response.new(0, {}, [e.message])
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def build_uri
|
|
106
|
+
uri = URI.parse(config.endpoint)
|
|
107
|
+
uri.query = URI.encode_www_form(
|
|
108
|
+
measurement_id: config.measurement_id,
|
|
109
|
+
api_secret: config.api_secret
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
uri
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def create_http_client(uri)
|
|
116
|
+
Net::HTTP.new(uri.host, uri.port).tap do |http|
|
|
117
|
+
http.use_ssl = true
|
|
118
|
+
http.read_timeout = config.timeout
|
|
119
|
+
http.open_timeout = config.timeout
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def create_request(uri, payload)
|
|
124
|
+
Net::HTTP::Post.new(uri.request_uri).tap do |request|
|
|
125
|
+
request["Content-Type"] = "application/json"
|
|
126
|
+
request.body = JSON.generate(payload)
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def parse_response_body(body)
|
|
131
|
+
return {} if body.nil? || body.empty?
|
|
132
|
+
|
|
133
|
+
JSON.parse(body)
|
|
134
|
+
rescue JSON::ParserError
|
|
135
|
+
{ raw: body }
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def generate_client_id
|
|
139
|
+
# ? might be the DebugView doesn't like when client_id is something like UUID, but with string like "debug" it's working
|
|
140
|
+
if config.debug_mode
|
|
141
|
+
"debug"
|
|
142
|
+
else
|
|
143
|
+
SecureRandom.uuid
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def log_response(response, payload)
|
|
148
|
+
return unless config.logger
|
|
149
|
+
|
|
150
|
+
if response.success?
|
|
151
|
+
config.logger.info("GA4 Event sent successfully")
|
|
152
|
+
|
|
153
|
+
if config.validation_mode || config.debug_mode
|
|
154
|
+
config.logger.debug("Payload: #{payload}")
|
|
155
|
+
config.logger.debug("Response: #{response.to_h}")
|
|
156
|
+
end
|
|
157
|
+
else
|
|
158
|
+
config.logger.warn("GA4 Event failed: #{response}")
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def log_error(message)
|
|
163
|
+
config.logger&.error(message)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def handle_error(message)
|
|
167
|
+
log_error(message)
|
|
168
|
+
|
|
169
|
+
raise GA4::Events::Error, message unless config.fail_silently
|
|
170
|
+
end
|
|
171
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class GA4::Events::Configuration
|
|
4
|
+
attr_accessor :measurement_id, :api_secret, :logger, :validation_mode, :debug_mode,
|
|
5
|
+
:validate_events, :max_retries, :retry_delay, :timeout,
|
|
6
|
+
:fail_silently
|
|
7
|
+
|
|
8
|
+
# GA4 API endpoint
|
|
9
|
+
# https://developers.google.com/analytics/devguides/collection/protocol/ga4/validating-events?client_type=firebase#send_events_for_validation
|
|
10
|
+
GA4_ENDPOINT = "https://www.google-analytics.com/mp/collect"
|
|
11
|
+
GA4_VALIDATION_ENDPOINT = "https://www.google-analytics.com/debug/mp/collect"
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
@measurement_id = ENV["GA4_MEASUREMENT_ID"]
|
|
15
|
+
@api_secret = ENV["GA4_API_SECRET"]
|
|
16
|
+
@logger = Logger.new($stdout)
|
|
17
|
+
@logger.level = Logger::WARN
|
|
18
|
+
@validation_mode = false # Send to validation endpoint for server-side validation
|
|
19
|
+
@debug_mode = false # Inject debug_mode param to show events in DebugView
|
|
20
|
+
@validate_events = true
|
|
21
|
+
@max_retries = 3
|
|
22
|
+
@retry_delay = 1 # seconds
|
|
23
|
+
@timeout = 10 # seconds
|
|
24
|
+
@fail_silently = true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def endpoint
|
|
28
|
+
validation_mode ? GA4_VALIDATION_ENDPOINT : GA4_ENDPOINT
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def valid?
|
|
32
|
+
!measurement_id.nil? && !measurement_id.empty? &&
|
|
33
|
+
!api_secret.nil? && !api_secret.empty?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def validate!
|
|
37
|
+
unless valid?
|
|
38
|
+
raise GA4::Events::ConfigurationError, "measurement_id and api_secret must be configured"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# https://support.google.com/analytics/answer/9267744
|
|
4
|
+
class GA4::Events::Event
|
|
5
|
+
attr_reader :name, :params
|
|
6
|
+
|
|
7
|
+
MAX_EVENT_NAME_LENGTH = 40
|
|
8
|
+
MAX_PARAMS_COUNT = 25
|
|
9
|
+
MAX_PARAM_NAME_LENGTH = 40
|
|
10
|
+
MAX_PARAM_VALUE_LENGTH = 100
|
|
11
|
+
|
|
12
|
+
def initialize(name, params = {})
|
|
13
|
+
@name = name.to_s
|
|
14
|
+
@params = params
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def valid?
|
|
18
|
+
errors.empty?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def errors
|
|
22
|
+
# ? do we really need memoization here?
|
|
23
|
+
@errors ||= validate_event
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_h(debug_mode: false)
|
|
27
|
+
{
|
|
28
|
+
name: name,
|
|
29
|
+
params: processed_params(debug_mode)
|
|
30
|
+
}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def processed_params(debug_mode)
|
|
36
|
+
return params unless debug_mode
|
|
37
|
+
|
|
38
|
+
# Inject debug_mode param if requested
|
|
39
|
+
(params || {}).dup.tap do |event_params|
|
|
40
|
+
event_params["debug_mode"] = "1"
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def validate_event
|
|
45
|
+
errors = []
|
|
46
|
+
|
|
47
|
+
# Validate event name
|
|
48
|
+
if name.nil? || name.empty?
|
|
49
|
+
errors << "Event name cannot be empty"
|
|
50
|
+
elsif name.length > MAX_EVENT_NAME_LENGTH
|
|
51
|
+
errors << "Event name cannot exceed #{MAX_EVENT_NAME_LENGTH} characters"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Validate params
|
|
55
|
+
if params.is_a?(Hash)
|
|
56
|
+
if params.size > MAX_PARAMS_COUNT
|
|
57
|
+
errors << "Event cannot have more than #{MAX_PARAMS_COUNT} parameters"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
params.each do |key, value|
|
|
61
|
+
param_name = key.to_s
|
|
62
|
+
|
|
63
|
+
if param_name.length > MAX_PARAM_NAME_LENGTH
|
|
64
|
+
errors << "Parameter name '#{param_name}' exceeds #{MAX_PARAM_NAME_LENGTH} characters"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
if value.is_a?(String) && value.length > MAX_PARAM_VALUE_LENGTH
|
|
68
|
+
errors << "Parameter '#{param_name}' value exceeds #{MAX_PARAM_VALUE_LENGTH} characters"
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
elsif !params.nil?
|
|
72
|
+
errors << "Event params must be a Hash"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
errors
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class GA4::Events::Response
|
|
4
|
+
attr_reader :status_code, :body, :errors
|
|
5
|
+
|
|
6
|
+
def initialize(status_code, body, errors = [])
|
|
7
|
+
@status_code = status_code
|
|
8
|
+
@body = body
|
|
9
|
+
@errors = errors
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def success?
|
|
13
|
+
[200, 204].include?(status_code)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def failure?
|
|
17
|
+
!success?
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def validation_messages
|
|
21
|
+
return [] unless body.is_a?(Hash)
|
|
22
|
+
|
|
23
|
+
body.dig("validationMessages") || []
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_h
|
|
27
|
+
{
|
|
28
|
+
status_code: status_code,
|
|
29
|
+
success: success?,
|
|
30
|
+
body: body,
|
|
31
|
+
errors: errors,
|
|
32
|
+
validation_messages: validation_messages
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def to_s
|
|
37
|
+
if success?
|
|
38
|
+
"Success (#{status_code})"
|
|
39
|
+
else
|
|
40
|
+
error_msg = errors.join(", ")
|
|
41
|
+
"Failure (#{status_code}): #{error_msg}"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/ga4/events.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "json"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
require "logger"
|
|
7
|
+
|
|
8
|
+
module GA4
|
|
9
|
+
module Events
|
|
10
|
+
class Error < StandardError; end
|
|
11
|
+
class ConfigurationError < Error; end
|
|
12
|
+
class ValidationError < Error; end
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
def configuration
|
|
16
|
+
@configuration ||= Configuration.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def configure
|
|
20
|
+
yield(configuration)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Convenience method to send a single event
|
|
24
|
+
def track(name, params = {}, client_id: nil, user_id: nil)
|
|
25
|
+
client = Client.new
|
|
26
|
+
|
|
27
|
+
client.send_event(name, params, client_id: client_id, user_id: user_id)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Convenience method to send batch events
|
|
31
|
+
def track_batch(events, client_id: nil, user_id: nil)
|
|
32
|
+
client = Client.new
|
|
33
|
+
|
|
34
|
+
client.send_batch(events, client_id: client_id, user_id: user_id)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def reset_configuration!
|
|
38
|
+
@configuration = Configuration.new
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
Dir[File.join(__dir__, "events/**/*.rb")].sort.each { |file| require file }
|
data/sig/ga4/events.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ga4-events
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- loqimean
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-11-30 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: logger
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.7'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.7'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: json
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - '='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 2.7.6
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 2.7.6
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '13.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '13.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: standard
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - '='
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '1.34'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - '='
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '1.34'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: vcr
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '6.0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '6.0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: webmock
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '3.0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - "~>"
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '3.0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: pry
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - ">="
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '0'
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: irb
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - '='
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: 1.6.3
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - '='
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: 1.6.3
|
|
139
|
+
- !ruby/object:Gem::Dependency
|
|
140
|
+
name: rdoc
|
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - ">="
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: '0'
|
|
146
|
+
type: :development
|
|
147
|
+
prerelease: false
|
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - ">="
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: '0'
|
|
153
|
+
- !ruby/object:Gem::Dependency
|
|
154
|
+
name: erb
|
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - '='
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: 2.2.3
|
|
160
|
+
type: :development
|
|
161
|
+
prerelease: false
|
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
163
|
+
requirements:
|
|
164
|
+
- - '='
|
|
165
|
+
- !ruby/object:Gem::Version
|
|
166
|
+
version: 2.2.3
|
|
167
|
+
- !ruby/object:Gem::Dependency
|
|
168
|
+
name: simplecov
|
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
|
170
|
+
requirements:
|
|
171
|
+
- - ">="
|
|
172
|
+
- !ruby/object:Gem::Version
|
|
173
|
+
version: '0'
|
|
174
|
+
type: :development
|
|
175
|
+
prerelease: false
|
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
177
|
+
requirements:
|
|
178
|
+
- - ">="
|
|
179
|
+
- !ruby/object:Gem::Version
|
|
180
|
+
version: '0'
|
|
181
|
+
- !ruby/object:Gem::Dependency
|
|
182
|
+
name: rubocop
|
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
|
184
|
+
requirements:
|
|
185
|
+
- - '='
|
|
186
|
+
- !ruby/object:Gem::Version
|
|
187
|
+
version: '1.61'
|
|
188
|
+
type: :development
|
|
189
|
+
prerelease: false
|
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
191
|
+
requirements:
|
|
192
|
+
- - '='
|
|
193
|
+
- !ruby/object:Gem::Version
|
|
194
|
+
version: '1.61'
|
|
195
|
+
- !ruby/object:Gem::Dependency
|
|
196
|
+
name: rubocop-rspec
|
|
197
|
+
requirement: !ruby/object:Gem::Requirement
|
|
198
|
+
requirements:
|
|
199
|
+
- - ">="
|
|
200
|
+
- !ruby/object:Gem::Version
|
|
201
|
+
version: '0'
|
|
202
|
+
type: :development
|
|
203
|
+
prerelease: false
|
|
204
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
205
|
+
requirements:
|
|
206
|
+
- - ">="
|
|
207
|
+
- !ruby/object:Gem::Version
|
|
208
|
+
version: '0'
|
|
209
|
+
- !ruby/object:Gem::Dependency
|
|
210
|
+
name: rubocop-performance
|
|
211
|
+
requirement: !ruby/object:Gem::Requirement
|
|
212
|
+
requirements:
|
|
213
|
+
- - ">="
|
|
214
|
+
- !ruby/object:Gem::Version
|
|
215
|
+
version: '0'
|
|
216
|
+
type: :development
|
|
217
|
+
prerelease: false
|
|
218
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
219
|
+
requirements:
|
|
220
|
+
- - ">="
|
|
221
|
+
- !ruby/object:Gem::Version
|
|
222
|
+
version: '0'
|
|
223
|
+
- !ruby/object:Gem::Dependency
|
|
224
|
+
name: rubocop-rake
|
|
225
|
+
requirement: !ruby/object:Gem::Requirement
|
|
226
|
+
requirements:
|
|
227
|
+
- - ">="
|
|
228
|
+
- !ruby/object:Gem::Version
|
|
229
|
+
version: '0'
|
|
230
|
+
type: :development
|
|
231
|
+
prerelease: false
|
|
232
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
233
|
+
requirements:
|
|
234
|
+
- - ">="
|
|
235
|
+
- !ruby/object:Gem::Version
|
|
236
|
+
version: '0'
|
|
237
|
+
- !ruby/object:Gem::Dependency
|
|
238
|
+
name: rubocop-ast
|
|
239
|
+
requirement: !ruby/object:Gem::Requirement
|
|
240
|
+
requirements:
|
|
241
|
+
- - '='
|
|
242
|
+
- !ruby/object:Gem::Version
|
|
243
|
+
version: 1.30.0
|
|
244
|
+
type: :development
|
|
245
|
+
prerelease: false
|
|
246
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
247
|
+
requirements:
|
|
248
|
+
- - '='
|
|
249
|
+
- !ruby/object:Gem::Version
|
|
250
|
+
version: 1.30.0
|
|
251
|
+
description: A simple, reliable Ruby gem for sending events to Google Analytics 4
|
|
252
|
+
(GA4) using the Measurement Protocol. Features include batch sending, event validation,
|
|
253
|
+
retry logic, debug mode, and configurable logging. Works with any Ruby application
|
|
254
|
+
- no framework dependencies required.
|
|
255
|
+
email:
|
|
256
|
+
- vanuha277@gmail.com
|
|
257
|
+
executables: []
|
|
258
|
+
extensions: []
|
|
259
|
+
extra_rdoc_files: []
|
|
260
|
+
files:
|
|
261
|
+
- ".rspec"
|
|
262
|
+
- ".rubocop.yml"
|
|
263
|
+
- ".rubocop/rspec.yml"
|
|
264
|
+
- ".rubocop_todo.yml"
|
|
265
|
+
- ".standard.yml"
|
|
266
|
+
- CHANGELOG.md
|
|
267
|
+
- CODE_OF_CONDUCT.md
|
|
268
|
+
- LICENSE.txt
|
|
269
|
+
- README.md
|
|
270
|
+
- Rakefile
|
|
271
|
+
- lib/ga4/events.rb
|
|
272
|
+
- lib/ga4/events/client.rb
|
|
273
|
+
- lib/ga4/events/configuration.rb
|
|
274
|
+
- lib/ga4/events/event.rb
|
|
275
|
+
- lib/ga4/events/response.rb
|
|
276
|
+
- lib/ga4/events/version.rb
|
|
277
|
+
- sig/ga4/events.rbs
|
|
278
|
+
homepage: https://github.com/the-rubies-way/ga4-events
|
|
279
|
+
licenses:
|
|
280
|
+
- MIT
|
|
281
|
+
metadata:
|
|
282
|
+
allowed_push_host: https://rubygems.org
|
|
283
|
+
homepage_uri: https://github.com/the-rubies-way/ga4-events
|
|
284
|
+
source_code_uri: https://github.com/the-rubies-way/ga4-events
|
|
285
|
+
changelog_uri: https://github.com/the-rubies-way/ga4-events/blob/main/CHANGELOG.md
|
|
286
|
+
bug_tracker_uri: https://github.com/the-rubies-way/ga4-events/issues
|
|
287
|
+
documentation_uri: https://github.com/the-rubies-way/ga4-events/blob/main/README.md
|
|
288
|
+
post_install_message:
|
|
289
|
+
rdoc_options: []
|
|
290
|
+
require_paths:
|
|
291
|
+
- lib
|
|
292
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
293
|
+
requirements:
|
|
294
|
+
- - ">="
|
|
295
|
+
- !ruby/object:Gem::Version
|
|
296
|
+
version: 2.6.0
|
|
297
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
298
|
+
requirements:
|
|
299
|
+
- - ">="
|
|
300
|
+
- !ruby/object:Gem::Version
|
|
301
|
+
version: '0'
|
|
302
|
+
requirements: []
|
|
303
|
+
rubygems_version: 3.4.19
|
|
304
|
+
signing_key:
|
|
305
|
+
specification_version: 4
|
|
306
|
+
summary: Framework-agnostic Ruby implementation of Google Analytics 4 Measurement
|
|
307
|
+
Protocol
|
|
308
|
+
test_files: []
|