Class: BetterSeo::Analytics::GoogleAnalytics

Inherits:
Object
  • Object
show all
Defined in:
lib/better_seo/analytics/google_analytics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(measurement_id, anonymize_ip: false) ⇒ GoogleAnalytics

Returns a new instance of GoogleAnalytics.



10
11
12
13
# File 'lib/better_seo/analytics/google_analytics.rb', line 10

def initialize(measurement_id, anonymize_ip: false)
  @measurement_id = measurement_id
  @anonymize_ip = anonymize_ip
end

Instance Attribute Details

#anonymize_ipObject (readonly)

Returns the value of attribute anonymize_ip.



8
9
10
# File 'lib/better_seo/analytics/google_analytics.rb', line 8

def anonymize_ip
  @anonymize_ip
end

#measurement_idObject (readonly)

Returns the value of attribute measurement_id.



8
9
10
# File 'lib/better_seo/analytics/google_analytics.rb', line 8

def measurement_id
  @measurement_id
end

Instance Method Details

#ecommerce_purchase(transaction_id:, value:, currency: "USD", items: []) ⇒ Object

Track e-commerce purchase



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/better_seo/analytics/google_analytics.rb', line 56

def ecommerce_purchase(transaction_id:, value:, currency: "USD", items: [])
  params = {
    "transaction_id" => transaction_id,
    "value" => value,
    "currency" => currency,
    "items" => items.map { |item| item.transform_keys(&:to_s) }
  }

  "    gtag('event', 'purchase', \#{JSON.generate(params)});\n  JS\nend\n".strip

#to_script_tag(nonce: nil, **config) ⇒ Object

Generate GA4 script tag



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/better_seo/analytics/google_analytics.rb', line 16

def to_script_tag(nonce: nil, **config)
  nonce_attr = nonce ? %( nonce="#{escape_html(nonce)}") : ""

  config_options = { "anonymize_ip" => @anonymize_ip } if @anonymize_ip
  config_options ||= {}
  config_options.merge!(config.transform_keys(&:to_s))

  config_json = config_options.empty? ? "" : ", #{JSON.generate(config_options)}"

  "    <script async src=\"https://www.googletagmanager.com/gtag/js?id=\#{@measurement_id}\"\#{nonce_attr}></script>\n    <script\#{nonce_attr}>\n      window.dataLayer = window.dataLayer || [];\n      function gtag(){dataLayer.push(arguments);}\n      gtag('js', new Date());\n      gtag('config', '\#{@measurement_id}'\#{config_json});\n    </script>\n  HTML\nend\n".strip

#track_event(event_name, **parameters) ⇒ Object

Track custom event



37
38
39
40
41
42
43
# File 'lib/better_seo/analytics/google_analytics.rb', line 37

def track_event(event_name, **parameters)
  params_json = parameters.empty? ? "" : ", #{JSON.generate(parameters.transform_keys(&:to_s))}"

  "    gtag('event', '\#{event_name}'\#{params_json});\n  JS\nend\n".strip

#track_page_view(page_path, title: nil) ⇒ Object

Track page view



46
47
48
49
50
51
52
53
# File 'lib/better_seo/analytics/google_analytics.rb', line 46

def track_page_view(page_path, title: nil)
  params = { "page_path" => page_path }
  params["page_title"] = title if title

  "    gtag('config', '\#{@measurement_id}', \#{JSON.generate(params)});\n  JS\nend\n".strip