Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save RichStone/62fad2d7efba4536e306c8ed0234fc4f to your computer and use it in GitHub Desktop.

Select an option

Save RichStone/62fad2d7efba4536e306c8ed0234fc4f to your computer and use it in GitHub Desktop.
Testing Google Analytics events
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
require 'time'
# Configuration
measurement_id = 'G-5DCEF2GTP6'
api_secret = 'ivlym-UzRG--OhBZ9h7P9g'
client_id = '182288249.1747727040'
# Generate current timestamp in microseconds
timestamp_micros = (Time.now.to_f * 1_000_000).to_i
# Build the request body
request_body = {
client_id: client_id,
timestamp_micros: timestamp_micros,
events: [
{
name: 'generate_lead',
params: {
currency: 'USD',
value: 0,
lead_source: 'https://funnels.myclickfunnels.test/opt'
}
}
]
}
# Create the URI for the request
uri = URI.parse("https://www.google-analytics.com/mp/collect?measurement_id=#{measurement_id}&api_secret=#{api_secret}")
# Create and configure the HTTP request
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request.body = request_body.to_json
puts "Sending GA4 event with payload:"
puts JSON.pretty_generate(request_body)
puts "\nTimestamp: #{timestamp_micros} (#{Time.at(timestamp_micros / 1_000_000.0)})"
puts "\nSending request to: #{uri}"
# Send the request and get the response
response = http.request(request)
puts "\nResponse status code: #{response.code}"
puts "Response headers:"
response.each_header do |key, value|
puts " #{key}: #{value}"
end
puts "\nResponse body:"
puts response.body.to_s
# Also show the debug validation
debug_uri = URI.parse("https://www.google-analytics.com/debug/mp/collect?measurement_id=#{measurement_id}&api_secret=#{api_secret}")
debug_http = Net::HTTP.new(debug_uri.host, debug_uri.port)
debug_http.use_ssl = true
debug_request = Net::HTTP::Post.new(debug_uri)
debug_request['Content-Type'] = 'application/json'
debug_request.body = request_body.to_json
puts "\n\nSending validation request to: #{debug_uri}"
debug_response = debug_http.request(debug_request)
puts "\nValidation response status code: #{debug_response.code}"
puts "Validation response:"
puts JSON.pretty_generate(JSON.parse(debug_response.body))
@RichStone
Copy link
Author

Successful output:

Sending GA4 event with payload:
{
  "client_id": "182288249.1747727040",
  "timestamp_micros": 1747735506284953,
  "events": [
    {
      "name": "generate_lead",
      "params": {
        "currency": "USD",
        "value": 0,
        "lead_source": "https://funnels.myclickfunnels.test/opt"
      }
    }
  ]
}

Timestamp: 1747735506284953 (2025-05-20 12:05:06 +0200)

Sending request to: https://www.google-analytics.com/mp/collect?measurement_id=G-5DCEF2GTP6&api_secret=ivlym-UzRG--OhBZ9h7P9g

Response status code: 204
Response headers:
  date: Tue, 20 May 2025 10:05:06 GMT
  pragma: no-cache
  expires: Fri, 01 Jan 1990 00:00:00 GMT
  cache-control: no-cache, no-store, must-revalidate
  last-modified: Sun, 17 May 1998 03:00:00 GMT
  content-type: text/plain
  cross-origin-resource-policy: cross-origin
  content-security-policy-report-only: script-src 'none'; form-action 'none'; frame-src 'none'; report-uri https://csp.withgoogle.com/csp/scaffolding/ascnsrsggc:61:0
  cross-origin-opener-policy-report-only: same-origin; report-to=ascnsrsggc:61:0
  report-to: {"group":"ascnsrsggc:61:0","max_age":2592000,"endpoints":[{"url":"https://csp.withgoogle.com/csp/report-to/scaffolding/ascnsrsggc:61:0"}],}
  server: Golfe2
  content-length: 0
  alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
  connection: close

Response body:



Sending validation request to: https://www.google-analytics.com/debug/mp/collect?measurement_id=G-5DCEF2GTP6&api_secret=ivlym-UzRG--OhBZ9h7P9g

Validation response status code: 200
Validation response:
{
  "validationMessages": [

  ]
}

Failed output:

Sending GA4 event with payload:
{
  "timestamp_micros": 1747735571875027,
  "events": [
    {
      "name": "generate_lead",
      "params": {
        "currency": "USD",
        "value": 0,
        "lead_source": "https://funnels.myclickfunnels.test/opt"
      }
    }
  ]
}

Timestamp: 1747735571875027 (2025-05-20 12:06:11 +0200)

Sending request to: https://www.google-analytics.com/mp/collect?measurement_id=G-5DCEF2GTP6&api_secret=ivlym-UzRG--OhBZ9h7P9g

Response status code: 204
Response headers:
  date: Tue, 20 May 2025 10:06:11 GMT
  pragma: no-cache
  expires: Fri, 01 Jan 1990 00:00:00 GMT
  cache-control: no-cache, no-store, must-revalidate
  last-modified: Sun, 17 May 1998 03:00:00 GMT
  content-type: text/plain
  cross-origin-resource-policy: cross-origin
  content-security-policy-report-only: script-src 'none'; form-action 'none'; frame-src 'none'; report-uri https://csp.withgoogle.com/csp/scaffolding/ascnsrsggc:61:0
  cross-origin-opener-policy-report-only: same-origin; report-to=ascnsrsggc:61:0
  report-to: {"group":"ascnsrsggc:61:0","max_age":2592000,"endpoints":[{"url":"https://csp.withgoogle.com/csp/report-to/scaffolding/ascnsrsggc:61:0"}],}
  server: Golfe2
  content-length: 0
  alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
  connection: close

Response body:



Sending validation request to: https://www.google-analytics.com/debug/mp/collect?measurement_id=G-5DCEF2GTP6&api_secret=ivlym-UzRG--OhBZ9h7P9g

Validation response status code: 200
Validation response:
{
  "validationMessages": [
    {
      "fieldPath": "client_id",
      "description": "Measurement requires a client_id.",
      "validationCode": "VALUE_REQUIRED"
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment