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

  snippet.html_safe
end