Class: Integral::GoogleTagManager

Inherits:
Object
  • Object
show all
Defined in:
lib/integral/google_tag_manager.rb

Overview

Handles rendering Google Tag Manager snipper

Class Method Summary collapse

Class Method Details

.render(container_id, type = :script) ⇒ String

Render Google Tag Manager Snippet

Returns:

  • (String)

    GTM Container if ID has been supplied



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/integral/google_tag_manager.rb', line 7

def self.render(container_id, type = :script)
  # TODO: - May want to add some other check for staging environments
  return '' if !container_id.present? || !Rails.env.production?

  if type == :script
    snippet = <<-HTML
      <!-- Google Tag Manager -->
      <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
      new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
      j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
      '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
      })(window,document,'script','dataLayer','#{container_id}');</script>
      <!-- End Google Tag Manager -->
    HTML
  else
    snippet = <<-HTML
      <!-- Google Tag Manager -->
      <noscript><iframe src="//www.googletagmanager.com/ns.html?id=#{container_id}"
      height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
      <!-- End Google Tag Manager -->
    HTML
  end

  snippet.html_safe
end