Class: Omnitrack::Adapters::GoogleAnalytics

Inherits:
Base
  • Object
show all
Defined in:
lib/omnitrack/adapters/google_analytics.rb

Overview

Google Analytics 4 — Measurement Protocol (server-side).

Required config:

measurement_id   — e.g. "G-XXXXXXXXXX"
api_secret       — Measurement Protocol API secret (from GA4 admin)

Optional config:

debug_mode       — when true, uses the /debug/mp/collect endpoint

Constant Summary collapse

MP_ENDPOINT =
"https://www.google-analytics.com/mp/collect"
MP_DEBUG_ENDPOINT =
"https://www.google-analytics.com/debug/mp/collect"

Instance Attribute Summary

Attributes inherited from Base

#config, #logger

Instance Method Summary collapse

Methods inherited from Base

#enabled?, inherited, #initialize, #name

Constructor Details

This class inherits a constructor from Omnitrack::Adapters::Base

Instance Method Details

#identify_user(user_data = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/omnitrack/adapters/google_analytics.rb', line 32

def identify_user(user_data = {})
  # GA4 uses client_id / user_id rather than a separate identify call
  ctx = Omnitrack::Context.current
  ctx&.merge!(ga_user_id: user_data[:external_id] || user_data[:email])

  Omnitrack::Result.success(
    adapter:  name,
    metadata: { note: "GA4 user_id stored in context" }
  )
end

#track_conversion(data = {}) ⇒ Object



28
29
30
# File 'lib/omnitrack/adapters/google_analytics.rb', line 28

def track_conversion(data = {})
  track_event(data.fetch(:event_name, "conversion"), data)
end

#track_event(event_name, payload = {}) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/omnitrack/adapters/google_analytics.rb', line 20

def track_event(event_name, payload = {})
  safe_execute(event_name) do
    body = build_event_body(event_name, payload)
    response = http_post(endpoint_url, body: body)
    Omnitrack::Result.success(adapter: name, data: { sent: body })
  end
end