class Jekyll::GoogleTagManager

Google Tag Manager tag, renders Liquid templates

Constants

PLACEHOLDER_ID
VALID_SECTIONS
VERSION

Attributes

context[RW]

Public Class Methods

new(_tag_name, text, _tokens) click to toggle source
Calls superclass method
# File lib/jekyll-google-tag-manager.rb, line 18
    def initialize(_tag_name, text, _tokens)
      super
      @text = text.strip
      message = <<~MSG
        Invalid section specified: #{@text}.
        Please specify one of the following sections: #{VALID_SECTIONS.join(',')}
      MSG
      raise InvalidSectionError, message unless VALID_SECTIONS.include?(@text)
    end

Public Instance Methods

container_id(config) click to toggle source
# File lib/jekyll-google-tag-manager.rb, line 43
def container_id(config)
  gtm_container_id = config.dig('google', 'tag_manager', 'container_id')

  if gtm_container_id.nil?
    warn_bad_config!
    return PLACEHOLDER_ID
  end

  gtm_container_id
rescue TypeError
  warn_bad_config!
  PLACEHOLDER_ID
end
info() click to toggle source
# File lib/jekyll-google-tag-manager.rb, line 73
def info
  {
    registers: context.registers,
    filters: [Jekyll::Filters]
  }
end
options() click to toggle source
# File lib/jekyll-google-tag-manager.rb, line 37
def options
  {
    'version' => Jekyll::GoogleTagManager::VERSION
  }
end
payload() click to toggle source
# File lib/jekyll-google-tag-manager.rb, line 66
def payload
  {
    'container_id' => container_id(context.registers[:site].config),
    'gtm_tag' => options
  }
end
render(context) click to toggle source
# File lib/jekyll-google-tag-manager.rb, line 28
def render(context)
  @context = context
  template.render!(payload, info)
end
template() click to toggle source
# File lib/jekyll-google-tag-manager.rb, line 33
def template
  @template ||= Liquid::Template.parse template_contents
end
template_contents() click to toggle source
# File lib/jekyll-google-tag-manager.rb, line 80
def template_contents
  @template_contents ||= begin
    File.read(template_path)
  end
end
template_path() click to toggle source
# File lib/jekyll-google-tag-manager.rb, line 86
def template_path
  @template_path ||= begin
    File.expand_path("./template-#{@text}.html", File.dirname(__FILE__))
  end
end
warn_bad_config!() click to toggle source
# File lib/jekyll-google-tag-manager.rb, line 57
def warn_bad_config!
  return if @@warning_shown

  @@warning_shown = true
  Jekyll.logger.warn('[WARNING]: jekyll-google-tag-manager')
  Jekyll.logger.warn('Your GTM container id is malformed or missing.')
  Jekyll.logger.warn("Using fallback: #{PLACEHOLDER_ID}")
end