Sha256: f5fe4d24a0e7d0c6d72609d4fd7971aa65374e8e42bf1c9a532ce8baa6bc6260

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

class Rack::Tracker::GoogleTagManager < Rack::Tracker::Handler

  class Push < OpenStruct
    def write
      to_h.to_json
    end
  end

  def inject(response)
    # Sub! is enough, in well formed html there's only one head or body tag.
    # Block syntax need to be used, otherwise backslashes in input will mess the output.
    # @see http://stackoverflow.com/a/4149087/518204 and https://github.com/railslove/rack-tracker/issues/50
    response.sub! %r{<head.*>} do |m|
      m.to_s << self.render_head
    end
    response.sub! %r{<body.*>} do |m|
      m.to_s << self.render_body
    end
    response
  end

  def container
    options[:container].respond_to?(:call) ? options[:container].call(env) : options[:container]
  end

  def render_head
    Tilt.new( File.join( File.dirname(__FILE__), 'template', 'google_tag_manager_head.erb') ).render(self)
  end

  def render_body
    Tilt.new( File.join( File.dirname(__FILE__), 'template', 'google_tag_manager_body.erb') ).render(self)
  end

  def self.track(name, *event)
    { name.to_s => [event.last.merge('class_name' => event.first.to_s.capitalize)] }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-tracker-1.4.0 lib/rack/tracker/google_tag_manager/google_tag_manager.rb
rack-tracker-1.3.1 lib/rack/tracker/google_tag_manager/google_tag_manager.rb